diff options
8 files changed, 90 insertions, 10 deletions
diff --git a/main/src/cgeo/geocaching/connector/AbstractConnector.java b/main/src/cgeo/geocaching/connector/AbstractConnector.java index a369a7c..5e102b9 100644 --- a/main/src/cgeo/geocaching/connector/AbstractConnector.java +++ b/main/src/cgeo/geocaching/connector/AbstractConnector.java @@ -8,6 +8,7 @@ import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.geopoint.Geopoint; import org.apache.commons.lang3.StringUtils; +import org.eclipse.jdt.annotation.NonNull; import java.util.ArrayList; import java.util.List; @@ -15,7 +16,7 @@ import java.util.List; public abstract class AbstractConnector implements IConnector { @Override - public boolean canHandle(String geocode) { + public boolean canHandle(@NonNull final String geocode) { return false; } diff --git a/main/src/cgeo/geocaching/connector/IConnector.java b/main/src/cgeo/geocaching/connector/IConnector.java index 4ac8c55..f5f70b0 100644 --- a/main/src/cgeo/geocaching/connector/IConnector.java +++ b/main/src/cgeo/geocaching/connector/IConnector.java @@ -6,6 +6,8 @@ import cgeo.geocaching.LogCacheActivity; import cgeo.geocaching.enumerations.LogType; import cgeo.geocaching.geopoint.Geopoint; +import org.eclipse.jdt.annotation.NonNull; + import java.util.List; public interface IConnector { @@ -22,7 +24,7 @@ public interface IConnector { * @param geocode * @return */ - public boolean canHandle(final String geocode); + public boolean canHandle(final @NonNull String geocode); /** * get browser URL for the given cache diff --git a/main/src/cgeo/geocaching/connector/ec/ECConnector.java b/main/src/cgeo/geocaching/connector/ec/ECConnector.java index 2edf0da..eb2f164 100644 --- a/main/src/cgeo/geocaching/connector/ec/ECConnector.java +++ b/main/src/cgeo/geocaching/connector/ec/ECConnector.java @@ -21,6 +21,7 @@ import cgeo.geocaching.settings.SettingsActivity; import cgeo.geocaching.utils.CancellableHandler; import org.apache.commons.lang3.StringUtils; +import org.eclipse.jdt.annotation.NonNull; import android.content.Context; import android.os.Handler; @@ -56,10 +57,7 @@ public class ECConnector extends AbstractConnector implements ISearchByGeocode, } @Override - public boolean canHandle(String geocode) { - if (geocode == null) { - return false; - } + public boolean canHandle(@NonNull String geocode) { return ECConnector.PATTERN_EC_CODE.matcher(geocode).matches(); } diff --git a/main/src/cgeo/geocaching/enumerations/CacheSize.java b/main/src/cgeo/geocaching/enumerations/CacheSize.java index a6f8df3..ee42c66 100644 --- a/main/src/cgeo/geocaching/enumerations/CacheSize.java +++ b/main/src/cgeo/geocaching/enumerations/CacheSize.java @@ -1,7 +1,7 @@ package cgeo.geocaching.enumerations; -import cgeo.geocaching.R; import cgeo.geocaching.CgeoApplication; +import cgeo.geocaching.R; import java.util.Collections; import java.util.HashMap; @@ -56,6 +56,28 @@ public enum CacheSize { if (resultNormalized != null) { return resultNormalized; } + return getByNumber(id); + } + + /** + * Bad GPX files can contain the container size encoded as number. + * + * @param id + * @return + */ + private static CacheSize getByNumber(final String id) { + try { + int numerical = Integer.parseInt(id); + if (numerical != 0) { + for (CacheSize size : CacheSize.values()) { + if (size.comparable == numerical) { + return size; + } + } + } + } catch (NumberFormatException e) { + // ignore, as this might be a number or not + } return UNKNOWN; } diff --git a/main/src/cgeo/geocaching/files/GPXParser.java b/main/src/cgeo/geocaching/files/GPXParser.java index 3e96291..4ebbb45 100644 --- a/main/src/cgeo/geocaching/files/GPXParser.java +++ b/main/src/cgeo/geocaching/files/GPXParser.java @@ -104,6 +104,10 @@ public abstract class GPXParser extends FileParser { */ private final Set<String> result = new HashSet<String>(100); private ProgressInputStream progressStream; + /** + * URL contained in the header of the GPX file. Used to guess where the file is coming from. + */ + protected String scriptUrl; private final class UserDataListener implements EndTextElementListener { private final int index; @@ -265,6 +269,14 @@ public abstract class GPXParser extends FileParser { final RootElement root = new RootElement(namespace, "gpx"); final Element waypoint = root.getChild(namespace, "wpt"); + root.getChild(namespace, "url").setEndTextElementListener(new EndTextElementListener() { + + @Override + public void end(String body) { + scriptUrl = body; + } + }); + // waypoint - attributes waypoint.setStartElementListener(new StartElementListener() { @@ -382,14 +394,19 @@ public abstract class GPXParser extends FileParser { } }); - // waypoint.getName() + // waypoint.name waypoint.getChild(namespace, "name").setEndTextElementListener(new EndTextElementListener() { @Override public void end(String body) { name = body; - final String content = body.trim(); + String content = body.trim(); + + // extremcaching.com manipulates the GC code by adding GC in front of ECxxx + if (StringUtils.startsWithIgnoreCase(content, "GCEC") && StringUtils.containsIgnoreCase(scriptUrl, "extremcaching")) { + content = content.substring(2); + } cache.setName(content); findGeoCode(cache.getName()); diff --git a/main/src/cgeo/geocaching/settings/Settings.java b/main/src/cgeo/geocaching/settings/Settings.java index b179e55..88d4344 100644 --- a/main/src/cgeo/geocaching/settings/Settings.java +++ b/main/src/cgeo/geocaching/settings/Settings.java @@ -312,7 +312,7 @@ public class Settings { } public static boolean isECConnectorActive() { - return getBoolean(R.string.pref_connectorECActive, true); + return getBoolean(R.string.pref_connectorECActive, false); } public static boolean isPremiumMember() { diff --git a/tests/src/cgeo/geocaching/connector/ec/ECConnectorTest.java b/tests/src/cgeo/geocaching/connector/ec/ECConnectorTest.java new file mode 100644 index 0000000..4b9ae37 --- /dev/null +++ b/tests/src/cgeo/geocaching/connector/ec/ECConnectorTest.java @@ -0,0 +1,33 @@ +package cgeo.geocaching.connector.ec; + +import cgeo.geocaching.Geocache; +import cgeo.geocaching.enumerations.CacheType; +import cgeo.geocaching.enumerations.LogType; + +import java.util.List; + +import junit.framework.TestCase; + +public class ECConnectorTest extends TestCase { + + public static void testCanHandle() throws Exception { + assertTrue(ECConnector.getInstance().canHandle("EC380")); + assertFalse(ECConnector.getInstance().canHandle("GC380")); + assertFalse("faked EC codes must be handled during the import, otherwise GCECxxxx codes belong to 2 connectors", ECConnector.getInstance().canHandle("GCEC380")); + } + + public static void testGetPossibleLogTypes() throws Exception { + final List<LogType> possibleLogTypes = ECConnector.getInstance().getPossibleLogTypes(createCache()); + assertNotNull(possibleLogTypes); + assertFalse(possibleLogTypes.isEmpty()); + assertTrue(possibleLogTypes.contains(LogType.FOUND_IT)); + } + + private static Geocache createCache() { + final Geocache geocache = new Geocache(); + geocache.setType(CacheType.TRADITIONAL); + geocache.setGeocode("EC727"); + return geocache; + } + +} diff --git a/tests/src/cgeo/geocaching/enumerations/CacheSizeTest.java b/tests/src/cgeo/geocaching/enumerations/CacheSizeTest.java index 9c3063d..2f11dfc 100644 --- a/tests/src/cgeo/geocaching/enumerations/CacheSizeTest.java +++ b/tests/src/cgeo/geocaching/enumerations/CacheSizeTest.java @@ -27,4 +27,11 @@ public class CacheSizeTest extends AndroidTestCase { assertEquals(size, CacheSize.getById(size.id.toUpperCase(Locale.US))); } } + + public static void testGetByIdNumeric() { + assertEquals(CacheSize.REGULAR, CacheSize.getById("3")); + assertEquals(CacheSize.UNKNOWN, CacheSize.getById("0")); + assertEquals(CacheSize.UNKNOWN, CacheSize.getById("9")); + assertEquals(CacheSize.UNKNOWN, CacheSize.getById("-1")); + } } |
