aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/files/LocParser.java
blob: 7a8a6740067dc4b4b304fd2470d190154d6d5f42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package cgeo.geocaching.files;

import cgeo.geocaching.ParseResult;
import cgeo.geocaching.Settings;
import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgCoord;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.geopoint.GeopointParser;
import cgeo.geocaching.utils.CancellableHandler;

import org.apache.commons.lang3.StringUtils;

import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class LocParser extends FileParser {
    private static final Pattern patternGeocode = Pattern
            .compile("name id=\"([^\"]+)\"");
    private static final Pattern patternLat = Pattern
            .compile("lat=\"([^\"]+)\"");
    private static final Pattern patternLon = Pattern
            .compile("lon=\"([^\"]+)\"");
    // premium only >>
    private static final Pattern patternDifficulty = Pattern
            .compile("<difficulty>([^<]+)</difficulty>");
    private static final Pattern patternTerrain = Pattern
            .compile("<terrain>([^<]+)</terrain>");
    private static final Pattern patternContainer = Pattern
            .compile("<container>([^<]+)</container>");
    private static final Pattern patternName = Pattern.compile("CDATA\\[([^\\]]+)\\]");

    private int listId;

    public static void parseLoc(final ParseResult parseResult, final String fileContent) {
        final Map<String, cgCoord> cidCoords = parseCoordinates(fileContent);

        // save found cache coordinates
        for (cgCache cache : parseResult.cacheList) {
            if (cidCoords.containsKey(cache.getGeocode())) {
                cgCoord coord = cidCoords.get(cache.getGeocode());

                copyCoordToCache(coord, cache);
            }
        }
    }

    private static void copyCoordToCache(final cgCoord coord, final cgCache cache) {
        cache.setCoords(coord.getCoords());
        cache.setDifficulty(coord.getDifficulty());
        cache.setTerrain(coord.getTerrain());
        cache.setSize(coord.getSize());
        cache.setGeocode(coord.getGeocode().toUpperCase());
        if (StringUtils.isBlank(cache.getName())) {
            cache.setName(coord.getName());
        }
    }

    static Map<String, cgCoord> parseCoordinates(
            final String fileContent) {
        final Map<String, cgCoord> coords = new HashMap<String, cgCoord>();
        if (StringUtils.isBlank(fileContent)) {
            return coords;
        }
        // >> premium only

        final String[] points = fileContent.split("<waypoint>");

        // parse coordinates
        for (String pointString : points) {
            final cgCoord pointCoord = new cgCoord();

            final Matcher matcherGeocode = patternGeocode.matcher(pointString);
            if (matcherGeocode.find()) {
                String geocode = matcherGeocode.group(1).trim().toUpperCase();
                pointCoord.setName(geocode);
                pointCoord.setGeocode(geocode);
            }
            final Matcher matcherName = patternName.matcher(pointString);
            if (matcherName.find()) {
                String name = matcherName.group(1).trim();
                pointCoord.setName(StringUtils.substringBeforeLast(name, " by ").trim());
                // owner = StringUtils.substringAfterLast(" by ").trim();
            }
            final Matcher matcherLat = patternLat.matcher(pointString);
            final Matcher matcherLon = patternLon.matcher(pointString);
            if (matcherLat.find() && matcherLon.find()) {
                pointCoord.setCoords(GeopointParser.parse(matcherLat.group(1).trim(), matcherLon.group(1).trim()));
            }
            final Matcher matcherDifficulty = patternDifficulty.matcher(pointString);
            if (matcherDifficulty.find()) {
                pointCoord.setDifficulty(Float.parseFloat(matcherDifficulty.group(1).trim()));
            }
            final Matcher matcherTerrain = patternTerrain.matcher(pointString);
            if (matcherTerrain.find()) {
                pointCoord.setTerrain(Float.parseFloat(matcherTerrain.group(1).trim()));
            }
            final Matcher matcherContainer = patternContainer.matcher(pointString);
            if (matcherContainer.find()) {
                final int size = Integer.parseInt(matcherContainer.group(1)
                        .trim());

                if (size == 1) {
                    pointCoord.setSize(CacheSize.NOT_CHOSEN);
                } else if (size == 2) {
                    pointCoord.setSize(CacheSize.MICRO);
                } else if (size == 3) {
                    pointCoord.setSize(CacheSize.REGULAR);
                } else if (size == 4) {
                    pointCoord.setSize(CacheSize.LARGE);
                } else if (size == 5) {
                    pointCoord.setSize(CacheSize.VIRTUAL);
                } else if (size == 6) {
                    pointCoord.setSize(CacheSize.OTHER);
                } else if (size == 8) {
                    pointCoord.setSize(CacheSize.SMALL);
                } else {
                    pointCoord.setSize(CacheSize.UNKNOWN);
                }
            }

            if (StringUtils.isNotBlank(pointCoord.getGeocode())) {
                coords.put(pointCoord.getGeocode(), pointCoord);
            }
        }

        Log.i(Settings.tag,
                "Coordinates found in .loc file: " + coords.size());
        return coords;
    }

    public LocParser(int listId) {
        this.listId = listId;
    }

    @Override
    public Collection<cgCache> parse(InputStream stream, CancellableHandler progressHandler) throws IOException, ParserException {
        // TODO: progress reporting happens during reading stream only, not during parsing
        String streamContent = readStream(stream, progressHandler).toString();
        final Map<String, cgCoord> coords = parseCoordinates(streamContent);
        final List<cgCache> caches = new ArrayList<cgCache>();
        for (Entry<String, cgCoord> entry : coords.entrySet()) {
            cgCoord coord = entry.getValue();
            if (StringUtils.isBlank(coord.getGeocode()) || StringUtils.isBlank(coord.getName())) {
                continue;
            }
            cgCache cache = new cgCache();
            cache.setReliableLatLon(true);
            copyCoordToCache(coord, cache);
            caches.add(cache);

            fixCache(cache);
            cache.setType(CacheType.UNKNOWN); // type is not given in the LOC file
            cache.setListId(listId);
            cache.setDetailed(true);
        }
        Log.i(Settings.tag, "Caches found in .loc file: " + caches.size());
        return caches;
    }
}