diff options
| -rw-r--r-- | main/src/cgeo/geocaching/Geocache.java | 5 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/LocParser.java | 33 | ||||
| -rw-r--r-- | tests/res/raw/waymarking_loc.loc | 8 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/files/LocParserTest.java | 19 |
4 files changed, 47 insertions, 18 deletions
diff --git a/main/src/cgeo/geocaching/Geocache.java b/main/src/cgeo/geocaching/Geocache.java index fd1c2ce..66b23bf 100644 --- a/main/src/cgeo/geocaching/Geocache.java +++ b/main/src/cgeo/geocaching/Geocache.java @@ -1561,9 +1561,12 @@ public class Geocache implements ICache, IWaypoint { Geocache cache; // get cache details, they may not yet be complete if (origCache != null) { + SearchResult search = null; // only reload the cache if it was already stored or doesn't have full details (by checking the description) if (origCache.isOffline() || StringUtils.isBlank(origCache.getDescription())) { - final SearchResult search = searchByGeocode(origCache.getGeocode(), null, listId, false, handler); + search = searchByGeocode(origCache.getGeocode(), null, listId, false, handler); + } + if (search != null) { cache = search.getFirstCacheFromResult(LoadFlags.LOAD_CACHE_OR_DB); } else { cache = origCache; diff --git a/main/src/cgeo/geocaching/files/LocParser.java b/main/src/cgeo/geocaching/files/LocParser.java index 873bf1b..49b9d6e 100644 --- a/main/src/cgeo/geocaching/files/LocParser.java +++ b/main/src/cgeo/geocaching/files/LocParser.java @@ -27,19 +27,13 @@ import java.util.regex.Pattern; public final class LocParser extends FileParser { + private static final String NAME_OWNER_SEPARATOR = " by "; 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 static final CacheSize[] SIZES = { @@ -82,6 +76,7 @@ public final class LocParser extends FileParser { if (StringUtils.isBlank(cache.getName())) { cache.setName(coord.getName()); } + cache.setOwnerDisplayName(coord.getOwnerDisplayName()); } static Map<String, Geocache> parseCoordinates(final String fileContent) { @@ -156,7 +151,11 @@ public final class LocParser extends FileParser { final MatcherWrapper matcherName = new MatcherWrapper(patternName, pointString); if (matcherName.find()) { final String name = matcherName.group(1).trim(); - cache.setName(StringUtils.substringBeforeLast(name, " by ").trim()); + String ownerName = StringUtils.trim(StringUtils.substringAfterLast(name, NAME_OWNER_SEPARATOR)); + if (StringUtils.isEmpty(cache.getOwnerDisplayName()) && StringUtils.isNotEmpty(ownerName)) { + cache.setOwnerDisplayName(ownerName); + } + cache.setName(StringUtils.substringBeforeLast(name, NAME_OWNER_SEPARATOR).trim()); } else { cache.setName(cache.getGeocode()); } @@ -167,20 +166,20 @@ public final class LocParser extends FileParser { cache.setCoords(parsePoint(matcherLat.group(1).trim(), matcherLon.group(1).trim())); } - final MatcherWrapper matcherDifficulty = new MatcherWrapper(patternDifficulty, pointString); + final String difficulty = StringUtils.substringBetween(pointString, "<difficulty>", "</difficulty>"); + final String terrain = StringUtils.substringBetween(pointString, "<terrain>", "</terrain>"); + final String container = StringUtils.substringBetween(pointString, "<container>", "</container"); try { - if (matcherDifficulty.find()) { - cache.setDifficulty(Float.parseFloat(matcherDifficulty.group(1).trim())); + if (StringUtils.isNotBlank(difficulty)) { + cache.setDifficulty(Float.parseFloat(difficulty.trim())); } - final MatcherWrapper matcherTerrain = new MatcherWrapper(patternTerrain, pointString); - if (matcherTerrain.find()) { - cache.setTerrain(Float.parseFloat(matcherTerrain.group(1).trim())); + if (StringUtils.isNotBlank(terrain)) { + cache.setTerrain(Float.parseFloat(terrain.trim())); } - final MatcherWrapper matcherContainer = new MatcherWrapper(patternContainer, pointString); - if (matcherContainer.find()) { - final int size = Integer.parseInt(matcherContainer.group(1).trim()); + if (StringUtils.isNotBlank(container)) { + final int size = Integer.parseInt(container.trim()); if (size >= 1 && size <= 8) { cache.setSize(SIZES[size - 1]); } diff --git a/tests/res/raw/waymarking_loc.loc b/tests/res/raw/waymarking_loc.loc new file mode 100644 index 0000000..caf3f7f --- /dev/null +++ b/tests/res/raw/waymarking_loc.loc @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="ISO-8859-1"?>
+<loc version="1.0" src="Groundspeak, Inc.">
+<waypoint>
+ <name id="WM7BK7"><![CDATA[Römerstrasse Kornwestheim by travelling]]></name>
+ <coord lat="48.856733" lon="9.197683"/>
+ <type>Waymark|Ancient Roman Civilization</type>
+ <link text="Waymark Details">http://www.waymarking.com/waymarks/WM7BK7_Rmerstrasse_Kornwestheim</link>
+</waypoint></loc>
\ No newline at end of file diff --git a/tests/src/cgeo/geocaching/files/LocParserTest.java b/tests/src/cgeo/geocaching/files/LocParserTest.java index bcc8e78..6e00b35 100644 --- a/tests/src/cgeo/geocaching/files/LocParserTest.java +++ b/tests/src/cgeo/geocaching/files/LocParserTest.java @@ -2,6 +2,7 @@ package cgeo.geocaching.files; import cgeo.geocaching.Geocache; import cgeo.geocaching.enumerations.CacheSize; +import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.geopoint.Geopoint; import cgeo.geocaching.test.AbstractResourceInstrumentationTestCase; import cgeo.geocaching.test.R; @@ -35,6 +36,7 @@ public class LocParserTest extends AbstractResourceInstrumentationTestCase { assertNotNull(cache); assertEquals("OC5952", cache.getGeocode()); assertEquals("Die Schatzinsel / treasure island", cache.getName()); + assertEquals("Die unbesiegbaren Geo - Geparden", cache.getOwnerDisplayName()); assertEquals(new Geopoint(48.85968, 9.18740), cache.getCoords()); } @@ -45,9 +47,26 @@ public class LocParserTest extends AbstractResourceInstrumentationTestCase { assertNotNull(cache); assertEquals("GC1BKP3", cache.getGeocode()); assertEquals("Die Schatzinsel / treasure island", cache.getName()); + assertEquals("Die unbesiegbaren Geo - Geparden", cache.getOwnerDisplayName()); assertEquals(new Geopoint(48.859683, 9.1874), cache.getCoords()); assertEquals(1.0f, cache.getDifficulty()); assertEquals(5.0f, cache.getTerrain()); assertEquals(CacheSize.MICRO, cache.getSize()); } + + public void testWaymarkingLoc() throws IOException, ParserException { + final List<Geocache> waymarks = readLoc(R.raw.waymarking_loc); + assertEquals(1, waymarks.size()); + final Geocache waymark = waymarks.get(0); + assertNotNull(waymark); + assertEquals("WM7BK7", waymark.getGeocode()); + assertEquals("Römerstrasse Kornwestheim", waymark.getName()); + assertEquals("travelling", waymark.getOwnerDisplayName()); + assertEquals(new Geopoint(48.856733, 9.197683), waymark.getCoords()); + // links are not yet stored for single caches + // assertEquals("http://www.waymarking.com/waymarks/WM7BK7_Rmerstrasse_Kornwestheim", waymark.getUrl()); + assertEquals(CacheSize.UNKNOWN, waymark.getSize()); + assertEquals(CacheType.UNKNOWN, waymark.getType()); + } + } |
