From 934945bddf33ec77ee77daff1add6498fe08e400 Mon Sep 17 00:00:00 2001 From: Bananeweizen Date: Sat, 8 Jun 2013 10:35:47 +0200 Subject: fix #2841: *.loc from waymarking not imported --- main/src/cgeo/geocaching/Geocache.java | 5 +++- main/src/cgeo/geocaching/files/LocParser.java | 33 +++++++++++----------- tests/res/raw/waymarking_loc.loc | 8 ++++++ tests/src/cgeo/geocaching/files/LocParserTest.java | 19 +++++++++++++ 4 files changed, 47 insertions(+), 18 deletions(-) create mode 100644 tests/res/raw/waymarking_loc.loc 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("([^<]+)"); - private static final Pattern patternTerrain = Pattern - .compile("([^<]+)"); - private static final Pattern patternContainer = Pattern - .compile("([^<]+)"); 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 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, "", ""); + final String terrain = StringUtils.substringBetween(pointString, "", ""); + final String container = StringUtils.substringBetween(pointString, "", "= 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 @@ + + + + + + Waymark|Ancient Roman Civilization + http://www.waymarking.com/waymarks/WM7BK7_Rmerstrasse_Kornwestheim + \ 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 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()); + } + } -- cgit v1.1