diff options
| -rw-r--r-- | main/src/cgeo/geocaching/connector/ConnectorFactory.java | 18 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/connector/UnknownConnector.java | 28 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/connector/opencaching/OkapiClient.java | 12 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/files/GPXParser.java | 18 | ||||
| -rw-r--r-- | tests/res/raw/no_connector.gpx | 168 | ||||
| -rw-r--r-- | tests/src/cgeo/geocaching/files/GPXParserTest.java | 26 |
6 files changed, 248 insertions, 22 deletions
diff --git a/main/src/cgeo/geocaching/connector/ConnectorFactory.java b/main/src/cgeo/geocaching/connector/ConnectorFactory.java index a264bd8..7b0326c 100644 --- a/main/src/cgeo/geocaching/connector/ConnectorFactory.java +++ b/main/src/cgeo/geocaching/connector/ConnectorFactory.java @@ -7,9 +7,9 @@ import cgeo.geocaching.connector.opencaching.OpenCachingConnector; import org.apache.commons.lang3.StringUtils; public final class ConnectorFactory { - private static final GCConnector GC_CONNECTOR = new GCConnector(); + private static final UnknownConnector UNKNOWN_CONNECTOR = new UnknownConnector(); private static final IConnector[] connectors = new IConnector[] { - GC_CONNECTOR, + new GCConnector(), new OpenCachingConnector("OpenCaching.DE", "www.opencaching.de", "OC"), new OpenCachingConnector("OpenCaching.CZ", "www.opencaching.cz", "OZ"), new ApiOpenCachingConnector("OpenCaching.CO.UK", "www.opencaching.org.uk", "OK", "arU4okouc4GEjMniE2fq"), @@ -22,7 +22,8 @@ public final class ConnectorFactory { new ApiOpenCachingConnector("OpenCaching.US", "www.opencaching.us", "OU", "pTsYAYSXFcfcRQnYE6uA"), new OXConnector(), new GeocachingAustraliaConnector(), - new GeopeitusConnector() + new GeopeitusConnector(), + UNKNOWN_CONNECTOR // the unknown connector MUST be the last one }; public static IConnector[] getConnectors() { @@ -47,21 +48,18 @@ public final class ConnectorFactory { public static IConnector getConnector(String geocode) { if (isInvalidGeocode(geocode)) { - return GC_CONNECTOR; + return UNKNOWN_CONNECTOR; } for (IConnector connector : connectors) { if (connector.canHandle(geocode)) { return connector; } } - // in case of errors, assume GC as default - return GC_CONNECTOR; + // in case of errors, take UNKNOWN + return UNKNOWN_CONNECTOR; } private static boolean isInvalidGeocode(final String geocode) { - if (StringUtils.isBlank(geocode) || geocode.length() <= 2) { - return true; - } - return false; + return StringUtils.isBlank(geocode); } } diff --git a/main/src/cgeo/geocaching/connector/UnknownConnector.java b/main/src/cgeo/geocaching/connector/UnknownConnector.java new file mode 100644 index 0000000..fce9220 --- /dev/null +++ b/main/src/cgeo/geocaching/connector/UnknownConnector.java @@ -0,0 +1,28 @@ +package cgeo.geocaching.connector; + +import cgeo.geocaching.cgCache; + +import org.apache.commons.lang3.StringUtils; + +public class UnknownConnector extends AbstractConnector implements IConnector { + + @Override + public String getName() { + return "Unknown caches"; + } + + @Override + public String getCacheUrl(cgCache cache) { + return null; // we have no url for these caches + } + + @Override + public String getHost() { + return null; // we have no host for these caches + } + + @Override + public boolean canHandle(final String geocode) { + return StringUtils.isNotBlank(geocode); + } +} diff --git a/main/src/cgeo/geocaching/connector/opencaching/OkapiClient.java b/main/src/cgeo/geocaching/connector/opencaching/OkapiClient.java index 7c35088..9869526 100644 --- a/main/src/cgeo/geocaching/connector/opencaching/OkapiClient.java +++ b/main/src/cgeo/geocaching/connector/opencaching/OkapiClient.java @@ -142,7 +142,10 @@ final public class OkapiClient { if (!uri.isAbsolute()) { final IConnector connector = ConnectorFactory.getConnector(geocode); - return "http://" + connector.getHost() + "/" + url; + final String host = connector.getHost(); + if (StringUtils.isNotBlank(host)) { + return "http://" + host + "/" + url; + } } return url; } @@ -254,7 +257,12 @@ final public class OkapiClient { return null; } - final String uri = "http://" + connector.getHost() + service; + final String host = connector.getHost(); + if (StringUtils.isBlank(host)) { + return null; + } + + final String uri = "http://" + host + service; ((ApiOpenCachingConnector) connector).addAuthentication(params); return cgBase.requestJSON(uri, params); } diff --git a/main/src/cgeo/geocaching/files/GPXParser.java b/main/src/cgeo/geocaching/files/GPXParser.java index 01929c6..c1cc1a3 100644 --- a/main/src/cgeo/geocaching/files/GPXParser.java +++ b/main/src/cgeo/geocaching/files/GPXParser.java @@ -260,12 +260,18 @@ public abstract class GPXParser extends FileParser { @Override public void end() { + // try to find geocode somewhere else if (StringUtils.isBlank(cache.getGeocode())) { - // try to find geocode somewhere else findGeoCode(name); findGeoCode(desc); findGeoCode(cmt); } + // take the name as code, if nothing else is available + if (StringUtils.isBlank(cache.getGeocode())) { + if (StringUtils.isNotBlank(name)) { + cache.setGeocode(name.trim()); + } + } if (StringUtils.isNotBlank(cache.getGeocode()) && cache.getCoords() != null @@ -772,11 +778,15 @@ public abstract class GPXParser extends FileParser { if (input == null || StringUtils.isNotBlank(cache.getGeocode())) { return; } - final Matcher matcherGeocode = patternGeocode.matcher(input); + final String trimmed = input.trim(); + final Matcher matcherGeocode = patternGeocode.matcher(trimmed); if (matcherGeocode.find()) { final String geocode = matcherGeocode.group(1); - if (ConnectorFactory.canHandle(geocode)) { - cache.setGeocode(geocode); + // a geocode should not be part of a word + if (geocode.length() == trimmed.length() || Character.isWhitespace(trimmed.charAt(geocode.length()))) { + if (ConnectorFactory.canHandle(geocode)) { + cache.setGeocode(geocode); + } } } } diff --git a/tests/res/raw/no_connector.gpx b/tests/res/raw/no_connector.gpx new file mode 100644 index 0000000..76799e3 --- /dev/null +++ b/tests/res/raw/no_connector.gpx @@ -0,0 +1,168 @@ +<?xml version="1.0" encoding="UTF-8"?>
+<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1" creator="EasyGPS 4.29" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.topografix.com/GPX/gpx_overlay/0/3 http://www.topografix.com/GPX/gpx_overlay/0/3/gpx_overlay.xsd http://www.topografix.com/GPX/gpx_modified/0/1 http://www.topografix.com/GPX/gpx_modified/0/1/gpx_modified.xsd">
+<metadata>
+<bounds minlat="33.23580184" minlon="-83.95870466" maxlat="33.24745160" maxlon="-83.94473756"/>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:25:07.006Z</time>
+</extensions>
+</metadata>
+<wpt lat="33.24308329" lon="-83.94859994">
+<time>2011-10-09T14:22:22.212Z</time>
+<name>12</name>
+<sym>Geocache</sym>
+<type>Geocache Found</type>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:23:30.512Z</time>
+<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
+<label_text>12</label_text>
+</label>
+</extensions>
+</wpt>
+<wpt lat="33.24391662" lon="-83.94701660">
+<time>2011-10-09T14:22:22.212Z</time>
+<name>14</name>
+<sym>Geocache</sym>
+<type>Geocache Found</type>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:23:45.154Z</time>
+<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
+<label_text>14</label_text>
+</label>
+</extensions>
+</wpt>
+<wpt lat="33.23580184" lon="-83.95303782">
+<time>2011-10-09T14:22:22.212Z</time>
+<name>22</name>
+<sym>Geocache</sym>
+<type>Geocache Found</type>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:23:45.154Z</time>
+<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
+<label_text>22</label_text>
+</label>
+</extensions>
+</wpt>
+<wpt lat="33.23861850" lon="-83.94748766">
+<time>2011-10-09T14:22:22.212Z</time>
+<name>24</name>
+<sym>Geocache</sym>
+<type>Geocache Found</type>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:23:45.154Z</time>
+<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
+<label_text>24</label_text>
+</label>
+</extensions>
+</wpt>
+<wpt lat="33.24745160" lon="-83.95662125">
+<time>2011-10-09T14:22:22.212Z</time>
+<name>32</name>
+<sym>Geocache</sym>
+<type>Geocache Found</type>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:23:45.154Z</time>
+<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
+<label_text>32</label_text>
+</label>
+</extensions>
+</wpt>
+<wpt lat="33.24496662" lon="-83.95683331">
+<time>2011-10-09T14:22:22.212Z</time>
+<name>34</name>
+<sym>Geocache</sym>
+<type>Geocache Found</type>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:24:25.797Z</time>
+<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
+<label_text>34</label_text>
+</label>
+</extensions>
+</wpt>
+<wpt lat="33.24423328" lon="-83.94773333">
+<time>2011-10-09T14:22:22.212Z</time>
+<name>42</name>
+<sym>Geocache</sym>
+<type>Geocache Found</type>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:23:45.154Z</time>
+<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
+<label_text>42</label_text>
+</label>
+</extensions>
+</wpt>
+<wpt lat="33.24543332" lon="-83.94983333">
+<time>2011-10-09T14:22:22.212Z</time>
+<name>44</name>
+<sym>Geocache</sym>
+<type>Geocache Found</type>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:23:45.154Z</time>
+<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
+<label_text>44</label_text>
+</label>
+</extensions>
+</wpt>
+<wpt lat="33.24029999" lon="-83.94995001">
+<time>2011-10-09T14:22:22.212Z</time>
+<name>MAIN</name>
+<cmt>10TH ANNUAL GGA GEO-CHALLENGE</cmt>
+<sym>Campground</sym>
+<type>Geocache Found</type>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:25:07.006Z</time>
+<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
+<label_text>MAIN</label_text>
+</label>
+</extensions>
+</wpt>
+<wpt lat="33.23896844" lon="-83.94473756">
+<time>2011-10-09T14:22:22.212Z</time>
+<name>WILD-1</name>
+<sym>Geocache</sym>
+<type>Geocache Found</type>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:23:45.154Z</time>
+<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
+<label_text>WILD-1</label_text>
+</label>
+</extensions>
+</wpt>
+<wpt lat="33.23768517" lon="-83.94800432">
+<time>2011-10-09T14:22:22.212Z</time>
+<name>WILD-2</name>
+<sym>Geocache</sym>
+<type>Geocache Found</type>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:23:45.154Z</time>
+<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
+<label_text>WILD-2</label_text>
+</label>
+</extensions>
+</wpt>
+<wpt lat="33.24701825" lon="-83.95870466">
+<time>2011-10-09T14:22:22.212Z</time>
+<name>WILD-3</name>
+<sym>Geocache</sym>
+<type>Geocache Found</type>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:23:45.154Z</time>
+<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
+<label_text>WILD-3</label_text>
+</label>
+</extensions>
+</wpt>
+<wpt lat="33.24601829" lon="-83.94810431">
+<time>2011-10-09T14:22:22.212Z</time>
+<name>WILD-4</name>
+<sym>Geocache</sym>
+<type>Geocache Found</type>
+<extensions>
+<time xmlns="http://www.topografix.com/GPX/gpx_modified/0/1">2011-10-09T14:24:25.797Z</time>
+<label xmlns="http://www.topografix.com/GPX/gpx_overlay/0/3">
+<label_text>WILD-4</label_text>
+</label>
+</extensions>
+</wpt>
+<extensions>
+</extensions>
+</gpx>
diff --git a/tests/src/cgeo/geocaching/files/GPXParserTest.java b/tests/src/cgeo/geocaching/files/GPXParserTest.java index c3e253a..d063dee 100644 --- a/tests/src/cgeo/geocaching/files/GPXParserTest.java +++ b/tests/src/cgeo/geocaching/files/GPXParserTest.java @@ -30,7 +30,7 @@ public class GPXParserTest extends InstrumentationTestCase { } private cgCache testGPXVersion(final int resourceId) throws IOException, ParserException { - final List<cgCache> caches = readGPX(resourceId); + final List<cgCache> caches = readGPX10(resourceId); assertNotNull(caches); assertEquals(1, caches.size()); final cgCache cache = caches.get(0); @@ -57,7 +57,7 @@ public class GPXParserTest extends InstrumentationTestCase { } public void testOC() throws IOException, ParserException { - final List<cgCache> caches = readGPX(R.raw.oc5952_gpx); + final List<cgCache> caches = readGPX10(R.raw.oc5952_gpx); final cgCache cache = caches.get(0); assertEquals("OC5952", cache.getGeocode()); assertEquals(CacheType.TRADITIONAL.id, cache.getType()); @@ -74,7 +74,7 @@ public class GPXParserTest extends InstrumentationTestCase { } public void testGc31j2h() throws IOException, ParserException { - final List<cgCache> caches = readGPX(R.raw.gc31j2h); + final List<cgCache> caches = readGPX10(R.raw.gc31j2h); assertEquals(1, caches.size()); final cgCache cache = caches.get(0); @@ -86,7 +86,7 @@ public class GPXParserTest extends InstrumentationTestCase { } public void testGc31j2hWpts() throws IOException, ParserException { - List<cgCache> caches = readGPX(R.raw.gc31j2h, R.raw.gc31j2h_wpts); + List<cgCache> caches = readGPX10(R.raw.gc31j2h, R.raw.gc31j2h_wpts); assertEquals(1, caches.size()); cgCache cache = caches.get(0); assertGc31j2h(cache); @@ -94,7 +94,7 @@ public class GPXParserTest extends InstrumentationTestCase { } public void testGc31j2hWptsWithoutCache() throws IOException, ParserException { - final List<cgCache> caches = readGPX(R.raw.gc31j2h_wpts); + final List<cgCache> caches = readGPX10(R.raw.gc31j2h_wpts); assertEquals(0, caches.size()); } @@ -174,8 +174,17 @@ public class GPXParserTest extends InstrumentationTestCase { assertEquals(8.545100, wp.getCoords().getLongitude(), 0.000001); } - private List<cgCache> readGPX(int... resourceIds) throws IOException, ParserException { + private List<cgCache> readGPX10(int... resourceIds) throws IOException, ParserException { final GPX10Parser parser = new GPX10Parser(1); + return readVersionedGPX(parser, resourceIds); + } + + private List<cgCache> readGPX11(int... resourceIds) throws IOException, ParserException { + final GPX11Parser parser = new GPX11Parser(1); + return readVersionedGPX(parser, resourceIds); + } + + private List<cgCache> readVersionedGPX(final GPXParser parser, int... resourceIds) throws IOException, ParserException { Collection<cgCache> caches = null; for (int resourceId : resourceIds) { final Resources res = getInstrumentation().getContext().getResources(); @@ -203,4 +212,9 @@ public class GPXParserTest extends InstrumentationTestCase { e.printStackTrace(); } } + + public void testSelfmadeGPXWithoutGeocodes() throws Exception { + final List<cgCache> caches = readGPX11(R.raw.no_connector); + assertEquals(13, caches.size()); + } } |
