aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/files/LocParser.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/files/LocParser.java')
-rw-r--r--main/src/cgeo/geocaching/files/LocParser.java82
1 files changed, 43 insertions, 39 deletions
diff --git a/main/src/cgeo/geocaching/files/LocParser.java b/main/src/cgeo/geocaching/files/LocParser.java
index b17b203..fe290c3 100644
--- a/main/src/cgeo/geocaching/files/LocParser.java
+++ b/main/src/cgeo/geocaching/files/LocParser.java
@@ -1,14 +1,15 @@
package cgeo.geocaching.files;
+import cgeo.geocaching.Geocache;
import cgeo.geocaching.SearchResult;
-import cgeo.geocaching.cgCache;
-import cgeo.geocaching.cgeoapplication;
+import cgeo.geocaching.cgData;
import cgeo.geocaching.enumerations.CacheSize;
import cgeo.geocaching.enumerations.CacheType;
import cgeo.geocaching.enumerations.LoadFlags;
import cgeo.geocaching.geopoint.Geopoint;
import cgeo.geocaching.utils.CancellableHandler;
import cgeo.geocaching.utils.Log;
+import cgeo.geocaching.utils.MatcherWrapper;
import org.apache.commons.lang3.StringUtils;
@@ -22,7 +23,6 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
-import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class LocParser extends FileParser {
@@ -56,7 +56,7 @@ public final class LocParser extends FileParser {
private int listId;
public static void parseLoc(final SearchResult searchResult, final String fileContent) {
- final Map<String, cgCache> cidCoords = parseCoordinates(fileContent);
+ final Map<String, Geocache> cidCoords = parseCoordinates(fileContent);
// save found cache coordinates
final HashSet<String> contained = new HashSet<String>();
@@ -65,27 +65,27 @@ public final class LocParser extends FileParser {
contained.add(geocode);
}
}
- Set<cgCache> caches = cgeoapplication.getInstance().loadCaches(contained, LoadFlags.LOAD_CACHE_OR_DB);
- for (cgCache cache : caches) {
- cgCache coord = cidCoords.get(cache.getGeocode());
+ Set<Geocache> caches = cgData.loadCaches(contained, LoadFlags.LOAD_CACHE_OR_DB);
+ for (Geocache cache : caches) {
+ Geocache coord = cidCoords.get(cache.getGeocode());
copyCoordToCache(coord, cache);
}
}
- private static void copyCoordToCache(final cgCache coord, final cgCache cache) {
+ private static void copyCoordToCache(final Geocache coord, final Geocache cache) {
cache.setCoords(coord.getCoords());
cache.setDifficulty(coord.getDifficulty());
cache.setTerrain(coord.getTerrain());
cache.setSize(coord.getSize());
- cache.setGeocode(coord.getGeocode().toUpperCase());
+ cache.setGeocode(coord.getGeocode());
cache.setReliableLatLon(true);
if (StringUtils.isBlank(cache.getName())) {
cache.setName(coord.getName());
}
}
- static Map<String, cgCache> parseCoordinates(final String fileContent) {
- final Map<String, cgCache> coords = new HashMap<String, cgCache>();
+ static Map<String, Geocache> parseCoordinates(final String fileContent) {
+ final Map<String, Geocache> coords = new HashMap<String, Geocache>();
if (StringUtils.isBlank(fileContent)) {
return coords;
}
@@ -95,7 +95,7 @@ public final class LocParser extends FileParser {
// parse coordinates
for (String pointString : points) {
- final cgCache pointCoord = parseCache(pointString);
+ final Geocache pointCoord = parseCache(pointString);
if (StringUtils.isNotBlank(pointCoord.getGeocode())) {
coords.put(pointCoord.getGeocode(), pointCoord);
}
@@ -121,17 +121,17 @@ public final class LocParser extends FileParser {
}
@Override
- public Collection<cgCache> parse(InputStream stream, CancellableHandler progressHandler) throws IOException, ParserException {
+ public Collection<Geocache> 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, cgCache> coords = parseCoordinates(streamContent);
- final List<cgCache> caches = new ArrayList<cgCache>();
- for (Entry<String, cgCache> entry : coords.entrySet()) {
- cgCache coord = entry.getValue();
+ final Map<String, Geocache> coords = parseCoordinates(streamContent);
+ final List<Geocache> caches = new ArrayList<Geocache>();
+ for (Entry<String, Geocache> entry : coords.entrySet()) {
+ Geocache coord = entry.getValue();
if (StringUtils.isBlank(coord.getGeocode()) || StringUtils.isBlank(coord.getName())) {
continue;
}
- cgCache cache = new cgCache();
+ Geocache cache = new Geocache();
cache.setReliableLatLon(true);
copyCoordToCache(coord, cache);
caches.add(cache);
@@ -140,20 +140,20 @@ public final class LocParser extends FileParser {
cache.setType(CacheType.UNKNOWN); // type is not given in the LOC file
cache.setListId(listId);
cache.setDetailed(true);
+ cache.store(null);
}
Log.i("Caches found in .loc file: " + caches.size());
return caches;
}
- public static cgCache parseCache(final String pointString) {
- final cgCache cache = new cgCache();
- final Matcher matcherGeocode = patternGeocode.matcher(pointString);
+ public static Geocache parseCache(final String pointString) {
+ final Geocache cache = new Geocache();
+ final MatcherWrapper matcherGeocode = new MatcherWrapper(patternGeocode, pointString);
if (matcherGeocode.find()) {
- final String geocode = matcherGeocode.group(1).trim().toUpperCase();
- cache.setGeocode(geocode.toUpperCase());
+ cache.setGeocode(matcherGeocode.group(1).trim());
}
- final Matcher matcherName = patternName.matcher(pointString);
+ final MatcherWrapper matcherName = new MatcherWrapper(patternName, pointString);
if (matcherName.find()) {
final String name = matcherName.group(1).trim();
cache.setName(StringUtils.substringBeforeLast(name, " by ").trim());
@@ -161,28 +161,32 @@ public final class LocParser extends FileParser {
cache.setName(cache.getGeocode());
}
- final Matcher matcherLat = patternLat.matcher(pointString);
- final Matcher matcherLon = patternLon.matcher(pointString);
+ final MatcherWrapper matcherLat = new MatcherWrapper(patternLat, pointString);
+ final MatcherWrapper matcherLon = new MatcherWrapper(patternLon, pointString);
if (matcherLat.find() && matcherLon.find()) {
cache.setCoords(parsePoint(matcherLat.group(1).trim(), matcherLon.group(1).trim()));
}
- final Matcher matcherDifficulty = patternDifficulty.matcher(pointString);
- if (matcherDifficulty.find()) {
- cache.setDifficulty(Float.parseFloat(matcherDifficulty.group(1).trim()));
- }
+ final MatcherWrapper matcherDifficulty = new MatcherWrapper(patternDifficulty, pointString);
+ try {
+ if (matcherDifficulty.find()) {
+ cache.setDifficulty(Float.parseFloat(matcherDifficulty.group(1).trim()));
+ }
- final Matcher matcherTerrain = patternTerrain.matcher(pointString);
- if (matcherTerrain.find()) {
- cache.setTerrain(Float.parseFloat(matcherTerrain.group(1).trim()));
- }
+ final MatcherWrapper matcherTerrain = new MatcherWrapper(patternTerrain, pointString);
+ if (matcherTerrain.find()) {
+ cache.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 && size <= 8) {
- cache.setSize(SIZES[size - 1]);
+ final MatcherWrapper matcherContainer = new MatcherWrapper(patternContainer, pointString);
+ if (matcherContainer.find()) {
+ final int size = Integer.parseInt(matcherContainer.group(1).trim());
+ if (size >= 1 && size <= 8) {
+ cache.setSize(SIZES[size - 1]);
+ }
}
+ } catch (NumberFormatException e) {
+ Log.e("LocParser.parseCache", e);
}
return cache;