aboutsummaryrefslogtreecommitdiffstats
path: root/src/cgeo/geocaching/files
diff options
context:
space:
mode:
Diffstat (limited to 'src/cgeo/geocaching/files')
-rw-r--r--src/cgeo/geocaching/files/FileParser.java106
-rw-r--r--src/cgeo/geocaching/files/GPX10Parser.java40
-rw-r--r--src/cgeo/geocaching/files/GPX11Parser.java40
-rw-r--r--src/cgeo/geocaching/files/LocParser.java358
4 files changed, 272 insertions, 272 deletions
diff --git a/src/cgeo/geocaching/files/FileParser.java b/src/cgeo/geocaching/files/FileParser.java
index c7f16c8..158d390 100644
--- a/src/cgeo/geocaching/files/FileParser.java
+++ b/src/cgeo/geocaching/files/FileParser.java
@@ -1,53 +1,53 @@
-package cgeo.geocaching.files;
-
-import cgeo.geocaching.cgBase;
-import cgeo.geocaching.cgCache;
-import cgeo.geocaching.cgSearch;
-
-import android.os.Handler;
-import android.os.Message;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.Date;
-
-public abstract class FileParser {
- protected static StringBuilder readFile(File file)
- throws FileNotFoundException, IOException {
- StringBuilder buffer = new StringBuilder();
- BufferedReader input = new BufferedReader(new FileReader(file));
- try {
- String line = null;
- while ((line = input.readLine()) != null) {
- buffer.append(line);
- }
- } finally {
- input.close();
- }
- return buffer;
- }
-
- static void showFinishedMessage(Handler handler, cgSearch search) {
- if (handler != null) {
- final Message msg = new Message();
- msg.obj = search.getCount();
- handler.sendMessage(msg);
- }
- }
-
- protected static void fixCache(cgCache cache) {
- cache.latitudeString = cgBase.formatLatitude(cache.coords.getLatitude(), true);
- cache.longitudeString = cgBase.formatLongitude(cache.coords.getLongitude(), true);
- if (cache.inventory != null) {
- cache.inventoryItems = cache.inventory.size();
- } else {
- cache.inventoryItems = 0;
- }
- cache.updated = new Date().getTime();
- cache.detailedUpdate = new Date().getTime();
- }
-
-}
+package cgeo.geocaching.files;
+
+import cgeo.geocaching.cgBase;
+import cgeo.geocaching.cgCache;
+import cgeo.geocaching.cgSearch;
+
+import android.os.Handler;
+import android.os.Message;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Date;
+
+public abstract class FileParser {
+ protected static StringBuilder readFile(File file)
+ throws FileNotFoundException, IOException {
+ StringBuilder buffer = new StringBuilder();
+ BufferedReader input = new BufferedReader(new FileReader(file));
+ try {
+ String line = null;
+ while ((line = input.readLine()) != null) {
+ buffer.append(line);
+ }
+ } finally {
+ input.close();
+ }
+ return buffer;
+ }
+
+ static void showFinishedMessage(Handler handler, cgSearch search) {
+ if (handler != null) {
+ final Message msg = new Message();
+ msg.obj = search.getCount();
+ handler.sendMessage(msg);
+ }
+ }
+
+ protected static void fixCache(cgCache cache) {
+ cache.latitudeString = cgBase.formatLatitude(cache.coords.getLatitude(), true);
+ cache.longitudeString = cgBase.formatLongitude(cache.coords.getLongitude(), true);
+ if (cache.inventory != null) {
+ cache.inventoryItems = cache.inventory.size();
+ } else {
+ cache.inventoryItems = 0;
+ }
+ cache.updated = new Date().getTime();
+ cache.detailedUpdate = new Date().getTime();
+ }
+
+}
diff --git a/src/cgeo/geocaching/files/GPX10Parser.java b/src/cgeo/geocaching/files/GPX10Parser.java
index be3c857..c3b95b2 100644
--- a/src/cgeo/geocaching/files/GPX10Parser.java
+++ b/src/cgeo/geocaching/files/GPX10Parser.java
@@ -1,20 +1,20 @@
-package cgeo.geocaching.files;
-
-import cgeo.geocaching.cgSearch;
-import cgeo.geocaching.cgeoapplication;
-
-import android.sax.Element;
-
-public final class GPX10Parser extends GPXParser {
-
- public GPX10Parser(cgeoapplication appIn, int listIdIn,
- cgSearch searchIn) {
- super(appIn, listIdIn, searchIn, "http://www.topografix.com/GPX/1/0", "1.0");
- }
-
- @Override
- protected Element getCacheParent(Element waypoint) {
- return waypoint;
- }
-
-}
+package cgeo.geocaching.files;
+
+import cgeo.geocaching.cgSearch;
+import cgeo.geocaching.cgeoapplication;
+
+import android.sax.Element;
+
+public final class GPX10Parser extends GPXParser {
+
+ public GPX10Parser(cgeoapplication appIn, int listIdIn,
+ cgSearch searchIn) {
+ super(appIn, listIdIn, searchIn, "http://www.topografix.com/GPX/1/0", "1.0");
+ }
+
+ @Override
+ protected Element getCacheParent(Element waypoint) {
+ return waypoint;
+ }
+
+}
diff --git a/src/cgeo/geocaching/files/GPX11Parser.java b/src/cgeo/geocaching/files/GPX11Parser.java
index 9370450..4c8960b 100644
--- a/src/cgeo/geocaching/files/GPX11Parser.java
+++ b/src/cgeo/geocaching/files/GPX11Parser.java
@@ -1,20 +1,20 @@
-package cgeo.geocaching.files;
-
-import cgeo.geocaching.cgSearch;
-import cgeo.geocaching.cgeoapplication;
-
-import android.sax.Element;
-
-public final class GPX11Parser extends GPXParser {
-
- public GPX11Parser(cgeoapplication appIn, int listIdIn,
- cgSearch searchIn) {
- super(appIn, listIdIn, searchIn, "http://www.topografix.com/GPX/1/1", "1.1");
- }
-
- @Override
- protected Element getCacheParent(Element waypoint) {
- return waypoint.getChild(namespace, "extensions");
- }
-
-}
+package cgeo.geocaching.files;
+
+import cgeo.geocaching.cgSearch;
+import cgeo.geocaching.cgeoapplication;
+
+import android.sax.Element;
+
+public final class GPX11Parser extends GPXParser {
+
+ public GPX11Parser(cgeoapplication appIn, int listIdIn,
+ cgSearch searchIn) {
+ super(appIn, listIdIn, searchIn, "http://www.topografix.com/GPX/1/1", "1.1");
+ }
+
+ @Override
+ protected Element getCacheParent(Element waypoint) {
+ return waypoint.getChild(namespace, "extensions");
+ }
+
+}
diff --git a/src/cgeo/geocaching/files/LocParser.java b/src/cgeo/geocaching/files/LocParser.java
index f631e10..0cbb297 100644
--- a/src/cgeo/geocaching/files/LocParser.java
+++ b/src/cgeo/geocaching/files/LocParser.java
@@ -1,179 +1,179 @@
-package cgeo.geocaching.files;
-
-import cgeo.geocaching.cgBase;
-import cgeo.geocaching.cgCache;
-import cgeo.geocaching.cgCacheWrap;
-import cgeo.geocaching.cgCoord;
-import cgeo.geocaching.cgSearch;
-import cgeo.geocaching.cgSettings;
-import cgeo.geocaching.cgeoapplication;
-import cgeo.geocaching.geopoint.Geopoint;
-
-import org.apache.commons.lang3.StringUtils;
-
-import android.os.Handler;
-import android.util.Log;
-
-import java.io.File;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.UUID;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-public final class LocParser extends FileParser {
- 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\\[([^\\]]+)\\]");
-
- public static void parseLoc(final cgCacheWrap caches,
- final String fileContent) {
- final Map<String, cgCoord> cidCoords = parseCoordinates(fileContent);
-
- // save found cache coordinates
- for (cgCache cache : caches.cacheList) {
- if (cidCoords.containsKey(cache.geocode)) {
- cgCoord coord = cidCoords.get(cache.geocode);
-
- copyCoordToCache(coord, cache);
- }
- }
- }
-
- private static void copyCoordToCache(final cgCoord coord, final cgCache cache) {
- cache.coords = coord.coords;
- cache.difficulty = coord.difficulty;
- cache.terrain = coord.terrain;
- cache.size = coord.size;
- cache.geocode = coord.geocode.toUpperCase();
- if (StringUtils.isBlank(cache.name)) {
- cache.name = coord.name;
- }
- }
-
- private static Map<String, cgCoord> parseCoordinates(
- final String fileContent) {
- final Map<String, cgCoord> coords = new HashMap<String, cgCoord>();
- if (StringUtils.isBlank(fileContent)) {
- return coords;
- }
- // >> premium only
-
- final String[] points = fileContent.split("<waypoint>");
-
- // parse coordinates
- for (String pointString : points) {
- final cgCoord pointCoord = new cgCoord();
-
- final Matcher matcherGeocode = patternGeocode.matcher(pointString);
- if (matcherGeocode.find()) {
- String geocode = matcherGeocode.group(1).trim().toUpperCase();
- pointCoord.name = geocode;
- pointCoord.geocode = geocode;
- }
- final Matcher matcherName = patternName.matcher(pointString);
- if (matcherName.find()) {
- String name = matcherName.group(1).trim();
- int pos = name.indexOf(" by ");
- if (pos > 0) {
- name = name.substring(0, pos).trim();
- }
- pointCoord.name = name;
- }
- final Matcher matcherLat = patternLat.matcher(pointString);
- final Matcher matcherLon = patternLon.matcher(pointString);
- if (matcherLat.find() && matcherLon.find()) {
- final Map<String, Object> tmpLat = cgBase.parseCoordinate(matcherLat.group(1).trim(), "lat");
- final Map<String, Object> tmpLon = cgBase.parseCoordinate(matcherLon.group(1).trim(), "lon");
- pointCoord.coords = new Geopoint((Double) tmpLat.get("coordinate"),
- (Double) tmpLon.get("coordinate"));
- }
- final Matcher matcherDifficulty = patternDifficulty.matcher(pointString);
- if (matcherDifficulty.find()) {
- pointCoord.difficulty = new Float(matcherDifficulty.group(1)
- .trim());
- }
- final Matcher matcherTerrain = patternTerrain.matcher(pointString);
- if (matcherTerrain.find()) {
- pointCoord.terrain = new Float(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) {
- pointCoord.size = "not chosen";
- } else if (size == 2) {
- pointCoord.size = "micro";
- } else if (size == 3) {
- pointCoord.size = "regular";
- } else if (size == 4) {
- pointCoord.size = "large";
- } else if (size == 5) {
- pointCoord.size = "virtual";
- } else if (size == 6) {
- pointCoord.size = "other";
- } else if (size == 8) {
- pointCoord.size = "small";
- } else {
- pointCoord.size = "unknown";
- }
- }
-
- if (StringUtils.isNotBlank(pointCoord.geocode)) {
- coords.put(pointCoord.geocode, pointCoord);
- }
- }
-
- Log.i(cgSettings.tag,
- "Coordinates found in .loc file: " + coords.size());
- return coords;
- }
-
- public static UUID parseLoc(cgeoapplication app, File file, int listId,
- Handler handler) {
- cgSearch search = new cgSearch();
- UUID searchId = null;
-
- try {
- Map<String, cgCoord> coords = parseCoordinates(readFile(file).toString());
- final cgCacheWrap caches = new cgCacheWrap();
- for (Entry<String, cgCoord> entry : coords.entrySet()) {
- cgCoord coord = entry.getValue();
- if (StringUtils.isBlank(coord.geocode) || StringUtils.isBlank(coord.name)) {
- continue;
- }
- cgCache cache = new cgCache();
- copyCoordToCache(coord, cache);
- caches.cacheList.add(cache);
-
- fixCache(cache);
- cache.reason = listId;
- cache.detailed = false;
-
- app.addCacheToSearch(search, cache);
- }
- caches.totalCnt = caches.cacheList.size();
- showFinishedMessage(handler, search);
- } catch (Exception e) {
- Log.e(cgSettings.tag, "cgBase.parseGPX: " + e.toString());
- }
-
- Log.i(cgSettings.tag, "Caches found in .gpx file: " + app.getCount(searchId));
-
- return search.getCurrentId();
- }
-}
+package cgeo.geocaching.files;
+
+import cgeo.geocaching.cgBase;
+import cgeo.geocaching.cgCache;
+import cgeo.geocaching.cgCacheWrap;
+import cgeo.geocaching.cgCoord;
+import cgeo.geocaching.cgSearch;
+import cgeo.geocaching.cgSettings;
+import cgeo.geocaching.cgeoapplication;
+import cgeo.geocaching.geopoint.Geopoint;
+
+import org.apache.commons.lang3.StringUtils;
+
+import android.os.Handler;
+import android.util.Log;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.UUID;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public final class LocParser extends FileParser {
+ 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\\[([^\\]]+)\\]");
+
+ public static void parseLoc(final cgCacheWrap caches,
+ final String fileContent) {
+ final Map<String, cgCoord> cidCoords = parseCoordinates(fileContent);
+
+ // save found cache coordinates
+ for (cgCache cache : caches.cacheList) {
+ if (cidCoords.containsKey(cache.geocode)) {
+ cgCoord coord = cidCoords.get(cache.geocode);
+
+ copyCoordToCache(coord, cache);
+ }
+ }
+ }
+
+ private static void copyCoordToCache(final cgCoord coord, final cgCache cache) {
+ cache.coords = coord.coords;
+ cache.difficulty = coord.difficulty;
+ cache.terrain = coord.terrain;
+ cache.size = coord.size;
+ cache.geocode = coord.geocode.toUpperCase();
+ if (StringUtils.isBlank(cache.name)) {
+ cache.name = coord.name;
+ }
+ }
+
+ private static Map<String, cgCoord> parseCoordinates(
+ final String fileContent) {
+ final Map<String, cgCoord> coords = new HashMap<String, cgCoord>();
+ if (StringUtils.isBlank(fileContent)) {
+ return coords;
+ }
+ // >> premium only
+
+ final String[] points = fileContent.split("<waypoint>");
+
+ // parse coordinates
+ for (String pointString : points) {
+ final cgCoord pointCoord = new cgCoord();
+
+ final Matcher matcherGeocode = patternGeocode.matcher(pointString);
+ if (matcherGeocode.find()) {
+ String geocode = matcherGeocode.group(1).trim().toUpperCase();
+ pointCoord.name = geocode;
+ pointCoord.geocode = geocode;
+ }
+ final Matcher matcherName = patternName.matcher(pointString);
+ if (matcherName.find()) {
+ String name = matcherName.group(1).trim();
+ int pos = name.indexOf(" by ");
+ if (pos > 0) {
+ name = name.substring(0, pos).trim();
+ }
+ pointCoord.name = name;
+ }
+ final Matcher matcherLat = patternLat.matcher(pointString);
+ final Matcher matcherLon = patternLon.matcher(pointString);
+ if (matcherLat.find() && matcherLon.find()) {
+ final Map<String, Object> tmpLat = cgBase.parseCoordinate(matcherLat.group(1).trim(), "lat");
+ final Map<String, Object> tmpLon = cgBase.parseCoordinate(matcherLon.group(1).trim(), "lon");
+ pointCoord.coords = new Geopoint((Double) tmpLat.get("coordinate"),
+ (Double) tmpLon.get("coordinate"));
+ }
+ final Matcher matcherDifficulty = patternDifficulty.matcher(pointString);
+ if (matcherDifficulty.find()) {
+ pointCoord.difficulty = new Float(matcherDifficulty.group(1)
+ .trim());
+ }
+ final Matcher matcherTerrain = patternTerrain.matcher(pointString);
+ if (matcherTerrain.find()) {
+ pointCoord.terrain = new Float(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) {
+ pointCoord.size = "not chosen";
+ } else if (size == 2) {
+ pointCoord.size = "micro";
+ } else if (size == 3) {
+ pointCoord.size = "regular";
+ } else if (size == 4) {
+ pointCoord.size = "large";
+ } else if (size == 5) {
+ pointCoord.size = "virtual";
+ } else if (size == 6) {
+ pointCoord.size = "other";
+ } else if (size == 8) {
+ pointCoord.size = "small";
+ } else {
+ pointCoord.size = "unknown";
+ }
+ }
+
+ if (StringUtils.isNotBlank(pointCoord.geocode)) {
+ coords.put(pointCoord.geocode, pointCoord);
+ }
+ }
+
+ Log.i(cgSettings.tag,
+ "Coordinates found in .loc file: " + coords.size());
+ return coords;
+ }
+
+ public static UUID parseLoc(cgeoapplication app, File file, int listId,
+ Handler handler) {
+ cgSearch search = new cgSearch();
+ UUID searchId = null;
+
+ try {
+ Map<String, cgCoord> coords = parseCoordinates(readFile(file).toString());
+ final cgCacheWrap caches = new cgCacheWrap();
+ for (Entry<String, cgCoord> entry : coords.entrySet()) {
+ cgCoord coord = entry.getValue();
+ if (StringUtils.isBlank(coord.geocode) || StringUtils.isBlank(coord.name)) {
+ continue;
+ }
+ cgCache cache = new cgCache();
+ copyCoordToCache(coord, cache);
+ caches.cacheList.add(cache);
+
+ fixCache(cache);
+ cache.reason = listId;
+ cache.detailed = false;
+
+ app.addCacheToSearch(search, cache);
+ }
+ caches.totalCnt = caches.cacheList.size();
+ showFinishedMessage(handler, search);
+ } catch (Exception e) {
+ Log.e(cgSettings.tag, "cgBase.parseGPX: " + e.toString());
+ }
+
+ Log.i(cgSettings.tag, "Caches found in .gpx file: " + app.getCount(searchId));
+
+ return search.getCurrentId();
+ }
+}