From 2a6c5e7e4e7a5d521f5b2099ac2adf46caaa30cb Mon Sep 17 00:00:00 2001 From: bananeweizen Date: Sun, 21 Aug 2011 14:27:41 +0200 Subject: refactoring of GPX parser (preparation for LOC import), resolved some name shadowing conflicts --- src/cgeo/geocaching/FileList.java | 239 +++++++++++++ src/cgeo/geocaching/FileParser.java | 52 +++ src/cgeo/geocaching/GPX10Parser.java | 17 + src/cgeo/geocaching/GPX11Parser.java | 17 + src/cgeo/geocaching/GPXParser.java | 580 +++++++++++++++++++++++++++++++ src/cgeo/geocaching/LocParser.java | 170 +++++++++ src/cgeo/geocaching/cgBase.java | 102 +----- src/cgeo/geocaching/cgData.java | 2 +- src/cgeo/geocaching/cgFileList.java | 232 ------------- src/cgeo/geocaching/cgGPXParser.java | 573 ------------------------------ src/cgeo/geocaching/cgSelectMapfile.java | 11 +- src/cgeo/geocaching/cgWaypoint.java | 4 + src/cgeo/geocaching/cgeodetail.java | 8 +- src/cgeo/geocaching/cgeogpxes.java | 23 +- src/cgeo/geocaching/cgeovisit.java | 10 +- src/cgeo/geocaching/cgeowaypoint.java | 18 +- 16 files changed, 1109 insertions(+), 949 deletions(-) create mode 100644 src/cgeo/geocaching/FileList.java create mode 100644 src/cgeo/geocaching/FileParser.java create mode 100644 src/cgeo/geocaching/GPX10Parser.java create mode 100644 src/cgeo/geocaching/GPX11Parser.java create mode 100644 src/cgeo/geocaching/GPXParser.java create mode 100644 src/cgeo/geocaching/LocParser.java delete mode 100644 src/cgeo/geocaching/cgFileList.java delete mode 100644 src/cgeo/geocaching/cgGPXParser.java (limited to 'src') diff --git a/src/cgeo/geocaching/FileList.java b/src/cgeo/geocaching/FileList.java new file mode 100644 index 0000000..545ad52 --- /dev/null +++ b/src/cgeo/geocaching/FileList.java @@ -0,0 +1,239 @@ +package cgeo.geocaching; + +import java.io.File; +import java.util.ArrayList; + +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.os.Bundle; +import android.os.Environment; +import android.os.Handler; +import android.os.Message; +import android.util.Log; +import android.widget.ArrayAdapter; +import cgeo.geocaching.activity.AbstractListActivity; + +public abstract class FileList> extends AbstractListActivity { + + private ArrayList files = new ArrayList(); + private T adapter = null; + private ProgressDialog waitDialog = null; + private loadFiles searchingThread = null; + private boolean endSearching = false; + private int listId = 1; + final private Handler changeWaitDialogHandler = new Handler() { + + @Override + public void handleMessage(Message msg) { + if (msg.obj != null && waitDialog != null) { + waitDialog.setMessage(res.getString(R.string.file_searching_in) + " " + (String) msg.obj); + } + } + }; + final private Handler loadFilesHandler = new Handler() { + + @Override + public void handleMessage(Message msg) { + try { + if (files == null || files.isEmpty()) { + if (waitDialog != null) { + waitDialog.dismiss(); + } + + showToast(res.getString(R.string.file_list_no_files)); + + finish(); + return; + } else { + if (adapter != null) { + adapter.notifyDataSetChanged(); + } + } + + if (waitDialog != null) { + waitDialog.dismiss(); + } + } catch (Exception e) { + if (waitDialog != null) { + waitDialog.dismiss(); + } + Log.e(cgSettings.tag, "cgFileList.loadFilesHandler: " + e.toString()); + } + } + }; + private String[] extensions; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setTheme(); + setContentView(R.layout.gpx); + setTitle(); + + Bundle extras = getIntent().getExtras(); + if (extras != null) { + listId = extras.getInt("list"); + } + if (listId <= 0) { + listId = 1; + } + + setAdapter(); + + waitDialog = ProgressDialog.show( + this, + res.getString(R.string.file_title_searching), + res.getString(R.string.file_searching), + true, + true, + new DialogInterface.OnCancelListener() { + public void onCancel(DialogInterface arg0) { + if (searchingThread != null && searchingThread.isAlive()) { + searchingThread.notifyEnd(); + } + if (files.isEmpty()) { + finish(); + } + } + } + ); + + endSearching = false; + searchingThread = new loadFiles(); + searchingThread.start(); + } + + @Override + public void onResume() { + super.onResume(); + + getSettings().load(); + } + + protected abstract T getAdapter(ArrayList files); + + private void setAdapter() { + if (adapter == null) { + adapter = getAdapter(files); + setListAdapter(adapter); + } + } + + /** + * Gets the base folder for file searches + * @return The folder to start the recursive search in + */ + protected abstract String[] getBaseFolders(); + + /** + * Triggers the deriving class to set the title + */ + protected abstract void setTitle(); + + private class loadFiles extends Thread { + public void notifyEnd() { + endSearching = true; + } + + @Override + public void run() { + ArrayList list = new ArrayList(); + + try { + if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { + boolean loaded = false; + for(String baseFolder : getBaseFolders()) + { + final File dir = new File(baseFolder); + + if (dir.exists() && dir.isDirectory()) { + listDir(list, dir); + if (list.size() > 0) { + loaded = true; + break; + } + } + } + if (!loaded) { + listDir(list, Environment.getExternalStorageDirectory()); + } + } else { + Log.w(cgSettings.tag, "No external media mounted."); + } + } catch (Exception e) { + Log.e(cgSettings.tag, "cgFileList.loadFiles.run: " + e.toString()); + } + + final Message msg = new Message(); + msg.obj = "loaded directories"; + changeWaitDialogHandler.sendMessage(msg); + + files.addAll(list); + list.clear(); + + loadFilesHandler.sendMessage(new Message()); + } + } + + private void listDir(ArrayList result, File directory) { + if (directory == null || !directory.isDirectory() || !directory.canRead()) { + return; + } + + final File[] files = directory.listFiles(); + + if (files != null && files.length > 0) { + for (File file : files) { + if (endSearching) { + return; + } + if (!file.canRead()) { + continue; + } + String name = file.getName(); + if (file.isFile()) { + for (String ext : extensions) { + int extLength = ext.length(); + if (name.length() > extLength && name.substring(name.length() - extLength, name.length()).equalsIgnoreCase(ext)) { + result.add(file); // add file to list + break; + } + } + + } else if (file.isDirectory()) { + if (name.charAt(0) == '.') { + continue; // skip hidden directories + } + if (name.length() > 16) { + name = name.substring(0, 14) + "..."; + } + final Message msg = new Message(); + msg.obj = name; + changeWaitDialogHandler.sendMessage(msg); + + listDir(result, file); // go deeper + } + } + } + + return; + } + + public FileList(final String extension) { + setExtensions(new String[] {extension}); + } + + public FileList(final String[] extensions) { + setExtensions(extensions); + } + + private void setExtensions(String[] extensionsIn) { + for (String extension : extensionsIn) { + if (!extension.startsWith(".")) { + extension = "." + extension; + } + } + extensions = extensionsIn; + } +} diff --git a/src/cgeo/geocaching/FileParser.java b/src/cgeo/geocaching/FileParser.java new file mode 100644 index 0000000..d67c888 --- /dev/null +++ b/src/cgeo/geocaching/FileParser.java @@ -0,0 +1,52 @@ +package cgeo.geocaching; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.Date; + +import android.os.Handler; +import android.os.Message; + +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.formatCoordinate(cache.latitude, "lat", true); + cache.longitudeString = cgBase.formatCoordinate(cache.longitude, "lon", 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/GPX10Parser.java b/src/cgeo/geocaching/GPX10Parser.java new file mode 100644 index 0000000..a1bcef4 --- /dev/null +++ b/src/cgeo/geocaching/GPX10Parser.java @@ -0,0 +1,17 @@ +package cgeo.geocaching; + +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/GPX11Parser.java b/src/cgeo/geocaching/GPX11Parser.java new file mode 100644 index 0000000..0f946b3 --- /dev/null +++ b/src/cgeo/geocaching/GPX11Parser.java @@ -0,0 +1,17 @@ +package cgeo.geocaching; + +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/GPXParser.java b/src/cgeo/geocaching/GPXParser.java new file mode 100644 index 0000000..000c745 --- /dev/null +++ b/src/cgeo/geocaching/GPXParser.java @@ -0,0 +1,580 @@ +package cgeo.geocaching; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; + +import android.os.Handler; +import android.sax.Element; +import android.sax.EndElementListener; +import android.sax.EndTextElementListener; +import android.sax.RootElement; +import android.sax.StartElementListener; +import android.text.Html; +import android.util.Log; +import android.util.Xml; + +public abstract class GPXParser extends FileParser { + + private cgSearch search = null; + private Handler handler = null; + private cgeoapplication app = null; + private int listId = 1; + private cgCache cache = new cgCache(); + private cgTrackable trackable = new cgTrackable(); + private cgLog log = new cgLog(); + private boolean htmlShort = true; + private boolean htmlLong = true; + private String type = null; + private String sym = null; + protected String namespace = null; + private ArrayList nsGCList = new ArrayList(); + private static final Pattern patternGeocode = Pattern.compile("(GC[0-9A-Z]+)", Pattern.CASE_INSENSITIVE); + private String name = null; + private String cmt = null; + private String desc = null; + private String version; + + public GPXParser(cgeoapplication appIn, int listIdIn, cgSearch searchIn, String namespaceIn, String versionIn) { + app = appIn; + listId = listIdIn; + search = searchIn; + + nsGCList.add("http://www.groundspeak.com/cache/1/1"); // PQ 1.1 + nsGCList.add("http://www.groundspeak.com/cache/1/0/1"); // PQ 1.0.1 + nsGCList.add("http://www.groundspeak.com/cache/1/0"); // PQ 1.0 + + namespace = namespaceIn; + version = versionIn; + } + + public long parse(File file, Handler handlerIn) { + handler = handlerIn; + if (file == null) { + return 0l; + } + + final RootElement root = new RootElement(namespace, "gpx"); + final Element waypoint = root.getChild(namespace, "wpt"); + + // waypoint - attributes + waypoint.setStartElementListener(new StartElementListener() { + + public void start(Attributes attrs) { + try { + if (attrs.getIndex("lat") > -1) { + cache.latitude = new Double(attrs.getValue("lat")); + } + if (attrs.getIndex("lon") > -1) { + cache.longitude = new Double(attrs.getValue("lon")); + } + } catch (Exception e) { + Log.w(cgSettings.tag, "Failed to parse waypoint's latitude and/or longitude."); + } + } + }); + + // waypoint + waypoint.setEndElementListener(new EndElementListener() { + + public void end() { + if (cache.geocode == null || cache.geocode.length() == 0) { + // try to find geocode somewhere else + String geocode = null; + Matcher matcherGeocode = null; + + if (name != null && geocode == null) { + matcherGeocode = patternGeocode.matcher(name); + while (matcherGeocode.find()) { + if (matcherGeocode.groupCount() > 0) { + geocode = matcherGeocode.group(1); + } + } + } + + if (desc != null && geocode == null) { + matcherGeocode = patternGeocode.matcher(desc); + while (matcherGeocode.find()) { + if (matcherGeocode.groupCount() > 0) { + geocode = matcherGeocode.group(1); + } + } + } + + if (cmt != null && geocode == null) { + matcherGeocode = patternGeocode.matcher(cmt); + while (matcherGeocode.find()) { + if (matcherGeocode.groupCount() > 0) { + geocode = matcherGeocode.group(1); + } + } + } + + if (geocode != null && geocode.length() > 0) { + cache.geocode = geocode; + } + + geocode = null; + matcherGeocode = null; + } + + if (cache.geocode != null && cache.geocode.length() > 0 + && cache.latitude != null && cache.longitude != null + && ((type == null && sym == null) + || (type != null && type.indexOf("geocache") > -1) + || (sym != null && sym.indexOf("geocache") > -1))) { + fixCache(cache); + cache.reason = listId; + cache.detailed = true; + + app.addCacheToSearch(search, cache); + } + + showFinishedMessage(handler, search); + + htmlShort = true; + htmlLong = true; + type = null; + sym = null; + name = null; + desc = null; + cmt = null; + + cache = null; + cache = new cgCache(); + } + }); + + // waypoint.time + waypoint.getChild(namespace, "time").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + try { + cache.hidden = cgBase.dateGPXIn.parse(body.trim()); + } catch (Exception e) { + Log.w(cgSettings.tag, "Failed to parse cache date: " + e.toString()); + } + } + }); + + // waypoint.name + waypoint.getChild(namespace, "name").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + name = body; + + final String content = Html.fromHtml(body).toString().trim(); + cache.name = content; + if (cache.name.length() > 2 && cache.name.substring(0, 2).equalsIgnoreCase("GC")) { + cache.geocode = cache.name.toUpperCase(); + } + } + }); + + // waypoint.desc + waypoint.getChild(namespace, "desc").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + desc = body; + + final String content = Html.fromHtml(body).toString().trim(); + cache.shortdesc = content; + } + }); + + // waypoint.cmt + waypoint.getChild(namespace, "cmt").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + cmt = body; + + final String content = Html.fromHtml(body).toString().trim(); + cache.description = content; + } + }); + + // waypoint.type + waypoint.getChild(namespace, "type").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + final String[] content = body.split("\\|"); + if (content.length > 0) { + type = content[0].toLowerCase().trim(); + } + } + }); + + // waypoint.sym + waypoint.getChild(namespace, "sym").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + body = body.toLowerCase(); + sym = body; + if (body.indexOf("geocache") != -1 && body.indexOf("found") != -1) { + cache.found = true; + } + } + }); + + // for GPX 1.0, cache info comes from waypoint node (so called private children, + // for GPX 1.1 from extensions node + final Element cacheParent = getCacheParent(waypoint); + + for (String nsGC : nsGCList) { + // waypoints.cache + final Element gcCache = cacheParent.getChild(nsGC, "cache"); + + gcCache.setStartElementListener(new StartElementListener() { + + public void start(Attributes attrs) { + try { + if (attrs.getIndex("id") > -1) { + cache.cacheid = attrs.getValue("id"); + } + if (attrs.getIndex("archived") > -1) { + cache.archived = attrs.getValue("archived").equalsIgnoreCase("true"); + } + if (attrs.getIndex("available") > -1) { + cache.disabled = !attrs.getValue("available").equalsIgnoreCase("true"); + } + } catch (Exception e) { + Log.w(cgSettings.tag, "Failed to parse cache attributes."); + } + } + }); + + // waypoint.cache.name + gcCache.getChild(nsGC, "name").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + final String content = Html.fromHtml(body).toString().trim(); + cache.name = validate(content); + } + }); + + // waypoint.cache.owner + gcCache.getChild(nsGC, "owner").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + final String content = Html.fromHtml(body).toString().trim(); + cache.owner = validate(content); + } + }); + + // waypoint.cache.type + gcCache.getChild(nsGC, "type").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + String parsedString = validate(body.toLowerCase()); + setType(parsedString); + } + }); + + // waypoint.cache.container + gcCache.getChild(nsGC, "container").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + final String content = body.toLowerCase(); + cache.size = validate(content); + } + }); + + // waypoint.cache.difficulty + gcCache.getChild(nsGC, "difficulty").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + try { + cache.difficulty = new Float(body); + } catch (Exception e) { + Log.w(cgSettings.tag, "Failed to parse difficulty: " + e.toString()); + } + } + }); + + // waypoint.cache.terrain + gcCache.getChild(nsGC, "terrain").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + try { + cache.terrain = new Float(body); + } catch (Exception e) { + Log.w(cgSettings.tag, "Failed to parse terrain: " + e.toString()); + } + } + }); + + // waypoint.cache.country + gcCache.getChild(nsGC, "country").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + if (cache.location == null || cache.location.length() == 0) { + cache.location = validate(body.trim()); + } else { + cache.location = cache.location + ", " + body.trim(); + } + } + }); + + // waypoint.cache.state + gcCache.getChild(nsGC, "state").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + if (cache.location == null || cache.location.length() == 0) { + cache.location = validate(body.trim()); + } else { + cache.location = body.trim() + ", " + cache.location; + } + } + }); + + // waypoint.cache.encoded_hints + gcCache.getChild(nsGC, "encoded_hints").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + cache.hint = validate(body.trim()); + } + }); + + // waypoint.cache.short_description + gcCache.getChild(nsGC, "short_description").setStartElementListener(new StartElementListener() { + + public void start(Attributes attrs) { + try { + if (attrs.getIndex("html") > -1) { + final String at = attrs.getValue("html"); + if (at.equalsIgnoreCase("false")) { + htmlShort = false; + } + } + } catch (Exception e) { + // nothing + } + } + }); + + gcCache.getChild(nsGC, "short_description").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + if (!htmlShort) { + cache.shortdesc = Html.fromHtml(body).toString(); + } else { + cache.shortdesc = body; + } + } + }); + + // waypoint.cache.long_description + gcCache.getChild(nsGC, "long_description").setStartElementListener(new StartElementListener() { + + public void start(Attributes attrs) { + try { + if (attrs.getIndex("html") > -1) { + if (attrs.getValue("html").equalsIgnoreCase("false")) { + htmlLong = false; + } + } + } catch (Exception e) { + // nothing + } + } + }); + + gcCache.getChild(nsGC, "long_description").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + if (htmlLong == false) { + cache.description = Html.fromHtml(body).toString().trim(); + } else { + cache.description = body; + } + } + }); + + // waypoint.cache.travelbugs + final Element gcTBs = gcCache.getChild(nsGC, "travelbugs"); + + // waypoint.cache.travelbugs.travelbug + gcTBs.getChild(nsGC, "travelbug").setStartElementListener(new StartElementListener() { + + public void start(Attributes attrs) { + trackable = new cgTrackable(); + + try { + if (attrs.getIndex("ref") > -1) { + trackable.geocode = attrs.getValue("ref").toUpperCase(); + } + } catch (Exception e) { + // nothing + } + } + }); + + // waypoint.cache.travelbug + final Element gcTB = gcTBs.getChild(nsGC, "travelbug"); + + gcTB.setEndElementListener(new EndElementListener() { + + public void end() { + if (trackable.geocode != null && trackable.geocode.length() > 0 && trackable.name != null && trackable.name.length() > 0) { + if (cache.inventory == null) + cache.inventory = new ArrayList(); + cache.inventory.add(trackable); + } + } + }); + + // waypoint.cache.travelbugs.travelbug.name + gcTB.getChild(nsGC, "name").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + String content = Html.fromHtml(body).toString(); + trackable.name = content; + } + }); + + // waypoint.cache.logs + final Element gcLogs = gcCache.getChild(nsGC, "logs"); + + // waypoint.cache.log + final Element gcLog = gcLogs.getChild(nsGC, "log"); + + gcLog.setStartElementListener(new StartElementListener() { + + public void start(Attributes attrs) { + log = new cgLog(); + + try { + if (attrs.getIndex("id") > -1) { + log.id = Integer.parseInt(attrs.getValue("id")); + } + } catch (Exception e) { + // nothing + } + } + }); + + gcLog.setEndElementListener(new EndElementListener() { + + public void end() { + if (log.log != null && log.log.length() > 0) { + if (cache.logs == null) + cache.logs = new ArrayList(); + cache.logs.add(log); + } + } + }); + + // waypoint.cache.logs.log.date + gcLog.getChild(nsGC, "date").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + try { + log.date = cgBase.dateGPXIn.parse(body.trim()).getTime(); + } catch (Exception e) { + Log.w(cgSettings.tag, "Failed to parse log date: " + e.toString()); + } + } + }); + + // waypoint.cache.logs.log.type + gcLog.getChild(nsGC, "type").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + final String content = body.trim().toLowerCase(); + if (cgBase.logTypes0.containsKey(content)) { + log.type = cgBase.logTypes0.get(content); + } else { + log.type = 4; + } + } + }); + + // waypoint.cache.logs.log.finder + gcLog.getChild(nsGC, "finder").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + String content = Html.fromHtml(body).toString(); + log.author = content; + } + }); + + // waypoint.cache.logs.log.finder + gcLog.getChild(nsGC, "text").setEndTextElementListener(new EndTextElementListener() { + + public void end(String body) { + String content = Html.fromHtml(body).toString(); + log.log = content; + } + }); + } + FileInputStream fis = null; + boolean parsed = false; + try { + fis = new FileInputStream(file); + } catch (FileNotFoundException e) { + Log.e(cgSettings.tag, "Cannot parse .gpx file " + file.getAbsolutePath() + " as GPX " + version + ": file not found!"); + } + try { + Xml.parse(fis, Xml.Encoding.UTF_8, root.getContentHandler()); + parsed = true; + } catch (IOException e) { + Log.e(cgSettings.tag, "Cannot parse .gpx file " + file.getAbsolutePath() + " as GPX " + version + ": could not read file!"); + } catch (SAXException e) { + Log.e(cgSettings.tag, "Cannot parse .gpx file " + file.getAbsolutePath() + " as GPX " + version + ": could not parse XML - " + e.toString()); + } + try { + fis.close(); + } catch (IOException e) { + Log.e(cgSettings.tag, "Error after parsing .gpx file " + file.getAbsolutePath() + " as GPX " + version + ": could not close file!"); + } + return parsed ? search.getCurrentId() : 0l; + } + + protected abstract Element getCacheParent(Element waypoint); + + protected String validate(String input) { + if ("nil".equalsIgnoreCase(input)) { + return ""; + } + return input; + } + + private void setType(String parsedString) { + final String knownType = cgBase.cacheTypes.get(parsedString); + if (knownType != null) { + cache.type = knownType; + } + else { + if (cache.type == null || cache.type.length() == 0) { + cache.type = "mystery"; // default for not recognized types + } + } + } + + public static Long parseGPX(cgeoapplication app, File file, int listId, Handler handler) { + cgSearch search = new cgSearch(); + long searchId = 0l; + + try { + GPXParser parser = new GPX10Parser(app, listId, search); + searchId = parser.parse(file, handler); + if (searchId == 0l) { + parser = new GPX11Parser(app, listId, search); + searchId = parser.parse(file, handler); + } + } 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(); + } +} diff --git a/src/cgeo/geocaching/LocParser.java b/src/cgeo/geocaching/LocParser.java new file mode 100644 index 0000000..21e9e02 --- /dev/null +++ b/src/cgeo/geocaching/LocParser.java @@ -0,0 +1,170 @@ +package cgeo.geocaching; + +import java.io.File; +import java.util.HashMap; +import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import android.os.Handler; +import android.util.Log; + +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("([^<]+)"); + private static final Pattern patternTerrain = Pattern + .compile("([^<]+)"); + private static final Pattern patternContainer = Pattern + .compile("([^<]+)"); + private static final Pattern patternName = Pattern.compile("CDATA\\[([^\\]]+)\\]"); + + public static void parseLoc(final cgCacheWrap caches, + final String fileContent) { + final HashMap 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.latitude = coord.latitude; + cache.longitude = coord.longitude; + cache.difficulty = coord.difficulty; + cache.terrain = coord.terrain; + cache.size = coord.size; + cache.geocode = coord.geocode.toUpperCase(); + if (cache.name == null || cache.name.length() == 0) { + cache.name = coord.name; + } + } + + private static HashMap parseCoordinates( + final String fileContent) { + final HashMap coords = new HashMap(); + if (fileContent == null || fileContent.length() <= 0) { + return coords; + } + // >> premium only + + final String[] points = fileContent.split(""); + + // parse coordinates + for (String pointString : points) { + final cgCoord pointCoord = new cgCoord(); + HashMap tmp = null; + + 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); + if (matcherLat.find()) { + tmp = cgBase.parseCoordinate(matcherLat.group(1).trim(), "lat"); + pointCoord.latitude = (Double) tmp.get("coordinate"); + } + final Matcher matcherLon = patternLon.matcher(pointString); + if (matcherLon.find()) { + tmp = cgBase.parseCoordinate(matcherLon.group(1).trim(), "lon"); + pointCoord.longitude = (Double) tmp.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 (pointCoord.name != null && pointCoord.name.length() > 0) { + coords.put(pointCoord.name, pointCoord); + } + } + + Log.i(cgSettings.tag, + "Coordinates found in .loc file: " + coords.size()); + return coords; + } + + public static long parseLoc(cgeoapplication app, File file, int listId, + Handler handler) { + cgSearch search = new cgSearch(); + long searchId = 0l; + + try { + HashMap coords = parseCoordinates(readFile(file).toString()); + final cgCacheWrap caches = new cgCacheWrap(); + for (Entry entry : coords.entrySet()) { + cgCoord coord = entry.getValue(); + if (coord.geocode == null || coord.geocode.length() == 0 || coord.name == null || coord.name.length() == 0) { + 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(); + } +} diff --git a/src/cgeo/geocaching/cgBase.java b/src/cgeo/geocaching/cgBase.java index 38e2d9e..aeb253d 100644 --- a/src/cgeo/geocaching/cgBase.java +++ b/src/cgeo/geocaching/cgBase.java @@ -935,87 +935,7 @@ public class cgBase { } } - if (coordinates != null && coordinates.length() > 0) { - final HashMap cidCoords = new HashMap(); - final Pattern patternCidCode = Pattern.compile("name id=\"([^\"]+)\""); - final Pattern patternCidLat = Pattern.compile("lat=\"([^\"]+)\""); - final Pattern patternCidLon = Pattern.compile("lon=\"([^\"]+)\""); - // premium only >> - final Pattern patternCidDif = Pattern.compile("([^<]+)"); - final Pattern patternCidTer = Pattern.compile("([^<]+)"); - final Pattern patternCidCon = Pattern.compile("([^<]+)"); - // >> premium only - - final String[] points = coordinates.split(""); - - // parse coordinates - for (String point : points) { - final cgCoord pointCoord = new cgCoord(); - final Matcher matcherCidCode = patternCidCode.matcher(point); - final Matcher matcherLatCode = patternCidLat.matcher(point); - final Matcher matcherLonCode = patternCidLon.matcher(point); - final Matcher matcherDifCode = patternCidDif.matcher(point); - final Matcher matcherTerCode = patternCidTer.matcher(point); - final Matcher matcherConCode = patternCidCon.matcher(point); - HashMap tmp = null; - - if (matcherCidCode.find()) { - pointCoord.name = matcherCidCode.group(1).trim().toUpperCase(); - } - if (matcherLatCode.find()) { - tmp = parseCoordinate(matcherLatCode.group(1), "lat"); - pointCoord.latitude = (Double) tmp.get("coordinate"); - } - if (matcherLonCode.find()) { - tmp = parseCoordinate(matcherLonCode.group(1), "lon"); - pointCoord.longitude = (Double) tmp.get("coordinate"); - } - if (matcherDifCode.find()) { - pointCoord.difficulty = new Float(matcherDifCode.group(1)); - } - if (matcherTerCode.find()) { - pointCoord.terrain = new Float(matcherTerCode.group(1)); - } - if (matcherConCode.find()) { - final int size = Integer.parseInt(matcherConCode.group(1)); - - 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"; - } - } - - cidCoords.put(pointCoord.name, pointCoord); - } - - Log.i(cgSettings.tag, "Coordinates found in .loc file: " + cidCoords.size()); - - // save found cache coordinates - for (cgCache oneCache : caches.cacheList) { - if (cidCoords.containsKey(oneCache.geocode)) { - cgCoord thisCoords = cidCoords.get(oneCache.geocode); - - oneCache.latitude = thisCoords.latitude; - oneCache.longitude = thisCoords.longitude; - oneCache.difficulty = thisCoords.difficulty; - oneCache.terrain = thisCoords.terrain; - oneCache.size = thisCoords.size; - } - } - } + LocParser.parseLoc(caches, coordinates); } catch (Exception e) { Log.e(cgSettings.tag, "cgBase.parseSearch.CIDs: " + e.toString()); } @@ -2117,26 +2037,6 @@ public class cgBase { return ratings; } - public static Long parseGPX(cgeoapplication app, File file, int listId, Handler handler) { - cgSearch search = new cgSearch(); - long searchId = 0l; - - try { - cgGPXParser GPXparser = new cgGPXParser(app, listId, search); - - searchId = GPXparser.parse(file, 10, handler); - if (searchId == 0l) { - searchId = GPXparser.parse(file, 11, handler); - } - } 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(); - } - public cgTrackable parseTrackable(String page) { if (page == null || page.length() == 0) { Log.e(cgSettings.tag, "cgeoBase.parseTrackable: No page given"); diff --git a/src/cgeo/geocaching/cgData.java b/src/cgeo/geocaching/cgData.java index 78a3442..deb8ea1 100644 --- a/src/cgeo/geocaching/cgData.java +++ b/src/cgeo/geocaching/cgData.java @@ -1354,7 +1354,7 @@ public class cgData { if (!waypoints.isEmpty()) { ContentValues values = new ContentValues(); for (cgWaypoint oneWaypoint : waypoints) { - if (oneWaypoint.type.equalsIgnoreCase("own")) { + if (oneWaypoint.isUserDefined()) { continue; } diff --git a/src/cgeo/geocaching/cgFileList.java b/src/cgeo/geocaching/cgFileList.java deleted file mode 100644 index 3536ba6..0000000 --- a/src/cgeo/geocaching/cgFileList.java +++ /dev/null @@ -1,232 +0,0 @@ -package cgeo.geocaching; - -import java.io.File; -import java.util.ArrayList; - -import android.app.ProgressDialog; -import android.content.DialogInterface; -import android.os.Bundle; -import android.os.Environment; -import android.os.Handler; -import android.os.Message; -import android.util.Log; -import android.widget.ArrayAdapter; -import cgeo.geocaching.activity.AbstractListActivity; - -public abstract class cgFileList> extends AbstractListActivity { - - private ArrayList files = new ArrayList(); - private T adapter = null; - private ProgressDialog waitDialog = null; - private loadFiles searchingThread = null; - private boolean endSearching = false; - private int listId = 1; - final private Handler changeWaitDialogHandler = new Handler() { - - @Override - public void handleMessage(Message msg) { - if (msg.obj != null && waitDialog != null) { - waitDialog.setMessage(res.getString(R.string.file_searching_in) + " " + (String) msg.obj); - } - } - }; - final private Handler loadFilesHandler = new Handler() { - - @Override - public void handleMessage(Message msg) { - try { - if (files == null || files.isEmpty()) { - if (waitDialog != null) { - waitDialog.dismiss(); - } - - showToast(res.getString(R.string.file_list_no_files)); - - finish(); - return; - } else { - if (adapter != null) { - adapter.notifyDataSetChanged(); - } - } - - if (waitDialog != null) { - waitDialog.dismiss(); - } - } catch (Exception e) { - if (waitDialog != null) { - waitDialog.dismiss(); - } - Log.e(cgSettings.tag, "cgFileList.loadFilesHandler: " + e.toString()); - } - } - }; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setTheme(); - setContentView(R.layout.gpx); - setTitle(); - - Bundle extras = getIntent().getExtras(); - if (extras != null) { - listId = extras.getInt("list"); - } - if (listId <= 0) { - listId = 1; - } - - setAdapter(); - - waitDialog = ProgressDialog.show( - this, - res.getString(R.string.file_title_searching), - res.getString(R.string.file_searching), - true, - true, - new DialogInterface.OnCancelListener() { - public void onCancel(DialogInterface arg0) { - if (searchingThread != null && searchingThread.isAlive()) { - searchingThread.notifyEnd(); - } - if (files.isEmpty()) { - finish(); - } - } - } - ); - - endSearching = false; - searchingThread = new loadFiles(); - searchingThread.start(); - } - - @Override - public void onResume() { - super.onResume(); - - getSettings().load(); - } - - protected abstract T getAdapter(ArrayList files); - - private void setAdapter() { - if (adapter == null) { - adapter = getAdapter(files); - setListAdapter(adapter); - } - } - - /** - * Gets the base folder for file searches - * @return The folder to start the recursive search in - */ - protected abstract String[] getBaseFolders(); - - /** - * Triggers the deriving class to set the title - */ - protected abstract void setTitle(); - - private class loadFiles extends Thread { - public void notifyEnd() { - endSearching = true; - } - - @Override - public void run() { - ArrayList list = new ArrayList(); - - try { - if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { - boolean loaded = false; - for(String baseFolder : getBaseFolders()) - { - final File dir = new File(baseFolder); - - if (dir.exists() && dir.isDirectory()) { - listDir(list, dir); - if (list.size() > 0) { - loaded = true; - break; - } - } - } - if (!loaded) { - listDir(list, Environment.getExternalStorageDirectory()); - } - } else { - Log.w(cgSettings.tag, "No external media mounted."); - } - } catch (Exception e) { - Log.e(cgSettings.tag, "cgFileList.loadFiles.run: " + e.toString()); - } - - final Message msg = new Message(); - msg.obj = "loaded directories"; - changeWaitDialogHandler.sendMessage(msg); - - files.addAll(list); - list.clear(); - - loadFilesHandler.sendMessage(new Message()); - } - } - - /** - * Get the file extension to search for - * @return The file extension - */ - protected abstract String getFileExtension(); - - private void listDir(ArrayList list, File directory) { - if (directory == null || directory.isDirectory() == false || directory.canRead() == false) { - return; - } - - final File[] listPre = directory.listFiles(); - String fileExt = getFileExtension(); - - if (listPre != null && listPre.length > 0) { - final int listCnt = listPre.length; - - for (int i = 0; i < listCnt; i++) { - if (endSearching) { - return; - } - - if (listPre[i].canRead() && listPre[i].isFile()) { - final String[] nameParts = listPre[i].getName().split("\\."); - if (nameParts.length > 1) { - final String extension = nameParts[(nameParts.length - 1)].toLowerCase(); - - if (extension.equals(fileExt) == false) { - continue; - } - } else { - continue; // file has no extension - } - - list.add(listPre[i]); // add file to list - } else if (listPre[i].canRead() && listPre[i].isDirectory()) { - final Message msg = new Message(); - String name = listPre[i].getName(); - if (name.substring(0, 1).equals(".")) { - continue; // skip hidden directories - } - if (name.length() > 16) { - name = name.substring(0, 14) + "..."; - } - msg.obj = name; - changeWaitDialogHandler.sendMessage(msg); - - listDir(list, listPre[i]); // go deeper - } - } - } - - return; - } -} diff --git a/src/cgeo/geocaching/cgGPXParser.java b/src/cgeo/geocaching/cgGPXParser.java deleted file mode 100644 index 2e9b671..0000000 --- a/src/cgeo/geocaching/cgGPXParser.java +++ /dev/null @@ -1,573 +0,0 @@ -package cgeo.geocaching; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Date; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.xml.sax.Attributes; -import org.xml.sax.SAXException; - -import android.os.Handler; -import android.os.Message; -import android.sax.Element; -import android.sax.EndElementListener; -import android.sax.EndTextElementListener; -import android.sax.RootElement; -import android.sax.StartElementListener; -import android.text.Html; -import android.util.Log; -import android.util.Xml; - -public class cgGPXParser { - - private cgeoapplication app = null; - private int listId = 1; - private cgSearch search = null; - private Handler handler = null; - private cgCache cache = new cgCache(); - private cgTrackable trackable = new cgTrackable(); - private cgLog log = new cgLog(); - private boolean htmlShort = true; - private boolean htmlLong = true; - private String type = null; - private String sym = null; - private String ns = null; - private ArrayList nsGCList = new ArrayList(); - private final Pattern patternGeocode = Pattern.compile("(GC[0-9A-Z]+)", Pattern.CASE_INSENSITIVE); - private String name = null; - private String cmt = null; - private String desc = null; - - public cgGPXParser(cgeoapplication appIn, int listIdIn, cgSearch searchIn) { - app = appIn; - listId = listIdIn; - search = searchIn; - - nsGCList.add("http://www.groundspeak.com/cache/1/1"); // PQ 1.1 - nsGCList.add("http://www.groundspeak.com/cache/1/0/1"); // PQ 1.0.1 - nsGCList.add("http://www.groundspeak.com/cache/1/0"); // PQ 1.0 - } - - public long parse(File file, int version, Handler handlerIn) { - handler = handlerIn; - if (file == null) { - return 0l; - } - - if (version == 11) { - ns = "http://www.topografix.com/GPX/1/1"; // GPX 1.1 - } else { - ns = "http://www.topografix.com/GPX/1/0"; // GPX 1.0 - } - final RootElement root = new RootElement(ns, "gpx"); - final Element waypoint = root.getChild(ns, "wpt"); - - // waypoint - attributes - waypoint.setStartElementListener(new StartElementListener() { - - public void start(Attributes attrs) { - try { - if (attrs.getIndex("lat") > -1) { - cache.latitude = new Double(attrs.getValue("lat")); - } - if (attrs.getIndex("lon") > -1) { - cache.longitude = new Double(attrs.getValue("lon")); - } - } catch (Exception e) { - Log.w(cgSettings.tag, "Failed to parse waypoint's latitude and/or longitude."); - } - } - }); - - // waypoint - waypoint.setEndElementListener(new EndElementListener() { - - public void end() { - if (cache.geocode == null || cache.geocode.length() == 0) { - // try to find geocode somewhere else - String geocode = null; - Matcher matcherGeocode = null; - - if (name != null && geocode == null) { - matcherGeocode = patternGeocode.matcher(name); - while (matcherGeocode.find()) { - if (matcherGeocode.groupCount() > 0) { - geocode = matcherGeocode.group(1); - } - } - } - - if (desc != null && geocode == null) { - matcherGeocode = patternGeocode.matcher(desc); - while (matcherGeocode.find()) { - if (matcherGeocode.groupCount() > 0) { - geocode = matcherGeocode.group(1); - } - } - } - - if (cmt != null && geocode == null) { - matcherGeocode = patternGeocode.matcher(cmt); - while (matcherGeocode.find()) { - if (matcherGeocode.groupCount() > 0) { - geocode = matcherGeocode.group(1); - } - } - } - - if (geocode != null && geocode.length() > 0) { - cache.geocode = geocode; - } - - geocode = null; - matcherGeocode = null; - } - - if (cache.geocode != null && cache.geocode.length() > 0 - && cache.latitude != null && cache.longitude != null - && ((type == null && sym == null) - || (type != null && type.indexOf("geocache") > -1) - || (sym != null && sym.indexOf("geocache") > -1))) { - cache.latitudeString = cgBase.formatCoordinate(cache.latitude, "lat", true); - cache.longitudeString = cgBase.formatCoordinate(cache.longitude, "lon", true); - if (cache.inventory != null) { - cache.inventoryItems = cache.inventory.size(); - } else { - cache.inventoryItems = 0; - } - cache.reason = listId; - cache.updated = new Date().getTime(); - cache.detailedUpdate = new Date().getTime(); - cache.detailed = true; - - app.addCacheToSearch(search, cache); - } - - if (handler != null) { - final Message msg = new Message(); - msg.obj = search.getCount(); - handler.sendMessage(msg); - } - - htmlShort = true; - htmlLong = true; - type = null; - sym = null; - name = null; - desc = null; - cmt = null; - - cache = null; - cache = new cgCache(); - } - }); - - // waypoint.time - waypoint.getChild(ns, "time").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - try { - cache.hidden = cgBase.dateGPXIn.parse(body.trim()); - } catch (Exception e) { - Log.w(cgSettings.tag, "Failed to parse cache date: " + e.toString()); - } - } - }); - - // waypoint.name - waypoint.getChild(ns, "name").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - name = body; - - final String content = Html.fromHtml(body).toString().trim(); - cache.name = content; - if (cache.name.length() > 2 && cache.name.substring(0, 2).equalsIgnoreCase("GC")) { - cache.geocode = cache.name.toUpperCase(); - } - } - }); - - // waypoint.desc - waypoint.getChild(ns, "desc").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - desc = body; - - final String content = Html.fromHtml(body).toString().trim(); - cache.shortdesc = content; - } - }); - - // waypoint.cmt - waypoint.getChild(ns, "cmt").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - cmt = body; - - final String content = Html.fromHtml(body).toString().trim(); - cache.description = content; - } - }); - - // waypoint.type - waypoint.getChild(ns, "type").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - final String[] content = body.split("\\|"); - if (content.length > 0) { - type = content[0].toLowerCase().trim(); - } - } - }); - - // waypoint.sym - waypoint.getChild(ns, "sym").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - body = body.toLowerCase(); - sym = body; - if (body.indexOf("geocache") != -1 && body.indexOf("found") != -1) { - cache.found = true; - } - } - }); - - // for GPX 1.0, cache info comes from waypoint node (so called private children, - // for GPX 1.1 from extensions node - final Element cacheParent = version == 11 ? waypoint.getChild(ns, "extensions") : waypoint; - - for (String nsGC : nsGCList) { - // waypoints.cache - final Element gcCache = cacheParent.getChild(nsGC, "cache"); - - gcCache.setStartElementListener(new StartElementListener() { - - public void start(Attributes attrs) { - try { - if (attrs.getIndex("id") > -1) { - cache.cacheid = attrs.getValue("id"); - } - if (attrs.getIndex("archived") > -1) { - cache.archived = attrs.getValue("archived").equalsIgnoreCase("true"); - } - if (attrs.getIndex("available") > -1) { - cache.disabled = !attrs.getValue("available").equalsIgnoreCase("true"); - } - } catch (Exception e) { - Log.w(cgSettings.tag, "Failed to parse cache attributes."); - } - } - }); - - // waypoint.cache.name - gcCache.getChild(nsGC, "name").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - final String content = Html.fromHtml(body).toString().trim(); - cache.name = validate(content); - } - }); - - // waypoint.cache.owner - gcCache.getChild(nsGC, "owner").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - final String content = Html.fromHtml(body).toString().trim(); - cache.owner = validate(content); - } - }); - - // waypoint.cache.type - gcCache.getChild(nsGC, "type").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - String parsedString = validate(body.toLowerCase()); - setType(parsedString); - } - }); - - // waypoint.cache.container - gcCache.getChild(nsGC, "container").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - final String content = body.toLowerCase(); - cache.size = validate(content); - } - }); - - // waypoint.cache.difficulty - gcCache.getChild(nsGC, "difficulty").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - try { - cache.difficulty = new Float(body); - } catch (Exception e) { - Log.w(cgSettings.tag, "Failed to parse difficulty: " + e.toString()); - } - } - }); - - // waypoint.cache.terrain - gcCache.getChild(nsGC, "terrain").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - try { - cache.terrain = new Float(body); - } catch (Exception e) { - Log.w(cgSettings.tag, "Failed to parse terrain: " + e.toString()); - } - } - }); - - // waypoint.cache.country - gcCache.getChild(nsGC, "country").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - if (cache.location == null || cache.location.length() == 0) { - cache.location = validate(body.trim()); - } else { - cache.location = cache.location + ", " + body.trim(); - } - } - }); - - // waypoint.cache.state - gcCache.getChild(nsGC, "state").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - if (cache.location == null || cache.location.length() == 0) { - cache.location = validate(body.trim()); - } else { - cache.location = body.trim() + ", " + cache.location; - } - } - }); - - // waypoint.cache.encoded_hints - gcCache.getChild(nsGC, "encoded_hints").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - cache.hint = validate(body.trim()); - } - }); - - // waypoint.cache.short_description - gcCache.getChild(nsGC, "short_description").setStartElementListener(new StartElementListener() { - - public void start(Attributes attrs) { - try { - if (attrs.getIndex("html") > -1) { - final String at = attrs.getValue("html"); - if (at.equalsIgnoreCase("false")) { - htmlShort = false; - } - } - } catch (Exception e) { - // nothing - } - } - }); - - gcCache.getChild(nsGC, "short_description").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - if (!htmlShort) { - cache.shortdesc = Html.fromHtml(body).toString(); - } else { - cache.shortdesc = body; - } - } - }); - - // waypoint.cache.long_description - gcCache.getChild(nsGC, "long_description").setStartElementListener(new StartElementListener() { - - public void start(Attributes attrs) { - try { - if (attrs.getIndex("html") > -1) { - if (attrs.getValue("html").equalsIgnoreCase("false")) { - htmlLong = false; - } - } - } catch (Exception e) { - // nothing - } - } - }); - - gcCache.getChild(nsGC, "long_description").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - if (htmlLong == false) { - cache.description = Html.fromHtml(body).toString().trim(); - } else { - cache.description = body; - } - } - }); - - // waypoint.cache.travelbugs - final Element gcTBs = gcCache.getChild(nsGC, "travelbugs"); - - // waypoint.cache.travelbugs.travelbug - gcTBs.getChild(nsGC, "travelbug").setStartElementListener(new StartElementListener() { - - public void start(Attributes attrs) { - trackable = new cgTrackable(); - - try { - if (attrs.getIndex("ref") > -1) { - trackable.geocode = attrs.getValue("ref").toUpperCase(); - } - } catch (Exception e) { - // nothing - } - } - }); - - // waypoint.cache.travelbug - final Element gcTB = gcTBs.getChild(nsGC, "travelbug"); - - gcTB.setEndElementListener(new EndElementListener() { - - public void end() { - if (trackable.geocode != null && trackable.geocode.length() > 0 && trackable.name != null && trackable.name.length() > 0) { - if (cache.inventory == null) - cache.inventory = new ArrayList(); - cache.inventory.add(trackable); - } - } - }); - - // waypoint.cache.travelbugs.travelbug.name - gcTB.getChild(nsGC, "name").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - String content = Html.fromHtml(body).toString(); - trackable.name = content; - } - }); - - // waypoint.cache.logs - final Element gcLogs = gcCache.getChild(nsGC, "logs"); - - // waypoint.cache.log - final Element gcLog = gcLogs.getChild(nsGC, "log"); - - gcLog.setStartElementListener(new StartElementListener() { - - public void start(Attributes attrs) { - log = new cgLog(); - - try { - if (attrs.getIndex("id") > -1) { - log.id = Integer.parseInt(attrs.getValue("id")); - } - } catch (Exception e) { - // nothing - } - } - }); - - gcLog.setEndElementListener(new EndElementListener() { - - public void end() { - if (log.log != null && log.log.length() > 0) { - if (cache.logs == null) - cache.logs = new ArrayList(); - cache.logs.add(log); - } - } - }); - - // waypoint.cache.logs.log.date - gcLog.getChild(nsGC, "date").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - try { - log.date = cgBase.dateGPXIn.parse(body.trim()).getTime(); - } catch (Exception e) { - Log.w(cgSettings.tag, "Failed to parse log date: " + e.toString()); - } - } - }); - - // waypoint.cache.logs.log.type - gcLog.getChild(nsGC, "type").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - final String content = body.trim().toLowerCase(); - if (cgBase.logTypes0.containsKey(content)) { - log.type = cgBase.logTypes0.get(content); - } else { - log.type = 4; - } - } - }); - - // waypoint.cache.logs.log.finder - gcLog.getChild(nsGC, "finder").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - String content = Html.fromHtml(body).toString(); - log.author = content; - } - }); - - // waypoint.cache.logs.log.finder - gcLog.getChild(nsGC, "text").setEndTextElementListener(new EndTextElementListener() { - - public void end(String body) { - String content = Html.fromHtml(body).toString(); - log.log = content; - } - }); - } - FileInputStream fis = null; - boolean parsed = false; - try { - fis = new FileInputStream(file); - } catch (FileNotFoundException e) { - Log.e(cgSettings.tag, "Cannot parse .gpx file " + file.getAbsolutePath() + " as GPX " + version + ": file not found!"); - } - try { - Xml.parse(fis, Xml.Encoding.UTF_8, root.getContentHandler()); - parsed = true; - } catch (IOException e) { - Log.e(cgSettings.tag, "Cannot parse .gpx file " + file.getAbsolutePath() + " as GPX " + version + ": could not read file!"); - } catch (SAXException e) { - Log.e(cgSettings.tag, "Cannot parse .gpx file " + file.getAbsolutePath() + " as GPX " + version + ": could not parse XML - " + e.toString()); - } - try { - fis.close(); - } catch (IOException e) { - Log.e(cgSettings.tag, "Error after parsing .gpx file " + file.getAbsolutePath() + " as GPX " + version + ": could not close file!"); - } - return parsed ? search.getCurrentId() : 0l; - } - - protected String validate(String input) { - if ("nil".equalsIgnoreCase(input)) { - return ""; - } - return input; - } - - private void setType(String parsedString) { - final String knownType = cgBase.cacheTypes.get(parsedString); - if (knownType != null) { - cache.type = knownType; - } - else { - if (cache.type == null || cache.type.length() == 0) { - cache.type = "mystery"; // default for not recognized types - } - } - } -} diff --git a/src/cgeo/geocaching/cgSelectMapfile.java b/src/cgeo/geocaching/cgSelectMapfile.java index 0d9e57c..d119f26 100644 --- a/src/cgeo/geocaching/cgSelectMapfile.java +++ b/src/cgeo/geocaching/cgSelectMapfile.java @@ -7,7 +7,11 @@ import android.content.Intent; import android.os.Bundle; import android.os.Environment; -public class cgSelectMapfile extends cgFileList { +public class cgSelectMapfile extends FileList { + + public cgSelectMapfile() { + super("map"); + } String mapFile; @@ -40,11 +44,6 @@ public class cgSelectMapfile extends cgFileList { } @Override - protected String getFileExtension() { - return "map"; - } - - @Override protected void setTitle() { setTitle(res.getString(R.string.map_file_select_title)); } diff --git a/src/cgeo/geocaching/cgWaypoint.java b/src/cgeo/geocaching/cgWaypoint.java index 5703dfa..7746c2c 100644 --- a/src/cgeo/geocaching/cgWaypoint.java +++ b/src/cgeo/geocaching/cgWaypoint.java @@ -90,4 +90,8 @@ public class cgWaypoint { } } } + + public boolean isUserDefined() { + return type != null && type.equalsIgnoreCase("own"); + } } \ No newline at end of file diff --git a/src/cgeo/geocaching/cgeodetail.java b/src/cgeo/geocaching/cgeodetail.java index b6f4cd4..ff6d702 100644 --- a/src/cgeo/geocaching/cgeodetail.java +++ b/src/cgeo/geocaching/cgeodetail.java @@ -343,7 +343,7 @@ public class cgeodetail extends AbstractActivity { // nothing, we lost the window } - threadCache = new loadCache(loadCacheHandler, geocode, guid); + threadCache = new loadCache(loadCacheHandler); threadCache.start(); } @@ -1253,13 +1253,9 @@ public class cgeodetail extends AbstractActivity { private class loadCache extends Thread { private Handler handler = null; - private String geocode = null; - private String guid = null; - public loadCache(Handler handlerIn, String geocodeIn, String guidIn) { + public loadCache(Handler handlerIn) { handler = handlerIn; - geocode = geocodeIn; - guid = guidIn; if (geocode == null && guid == null) { showToast(res.getString(R.string.err_detail_cache_forgot)); diff --git a/src/cgeo/geocaching/cgeogpxes.java b/src/cgeo/geocaching/cgeogpxes.java index 772f268..380f0f7 100644 --- a/src/cgeo/geocaching/cgeogpxes.java +++ b/src/cgeo/geocaching/cgeogpxes.java @@ -9,7 +9,13 @@ import android.os.Environment; import android.os.Handler; import android.os.Message; -public class cgeogpxes extends cgFileList { +public class cgeogpxes extends FileList { + + public cgeogpxes() { + super(new String[] {"gpx" + // TODO , "loc" + }); + } private ProgressDialog parseDialog = null; private int listId = 1; @@ -55,11 +61,6 @@ public class cgeogpxes extends cgFileList { } @Override - protected String getFileExtension() { - return "gpx"; - } - - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -100,8 +101,14 @@ public class cgeogpxes extends cgFileList { @Override public void run() { - final long searchId = cgBase.parseGPX(app, file, listId, changeParseDialogHandler); - + final long searchId; + String name = file.getName().toLowerCase(); + if (name.endsWith("gpx")) { + searchId = GPXParser.parseGPX(app, file, listId, changeParseDialogHandler); + } + else { + searchId = LocParser.parseLoc(app, file, listId, changeParseDialogHandler); + } imported = app.getCount(searchId); loadCachesHandler.sendMessage(new Message()); diff --git a/src/cgeo/geocaching/cgeovisit.java b/src/cgeo/geocaching/cgeovisit.java index 43a97aa..767363d 100644 --- a/src/cgeo/geocaching/cgeovisit.java +++ b/src/cgeo/geocaching/cgeovisit.java @@ -83,7 +83,7 @@ public class cgeovisit extends cgLogForm { showToast(res.getString(R.string.err_log_load_data_again)); loadData thread; - thread = new loadData(cacheid); + thread = new loadData(); thread.start(); return; @@ -551,7 +551,7 @@ public class cgeovisit extends cgLogForm { post.setOnClickListener(null); loadData thread; - thread = new loadData(cacheid); + thread = new loadData(); thread.start(); } else { post.setEnabled(true); @@ -687,11 +687,7 @@ public class cgeovisit extends cgLogForm { private class loadData extends Thread { - private String cacheid = null; - - public loadData(String cacheidIn) { - cacheid = cacheidIn; - + public loadData() { if (cacheid == null) { showToast(res.getString(R.string.err_detail_cache_forgot_visit)); diff --git a/src/cgeo/geocaching/cgeowaypoint.java b/src/cgeo/geocaching/cgeowaypoint.java index 672e7f5..e4ec379 100644 --- a/src/cgeo/geocaching/cgeowaypoint.java +++ b/src/cgeo/geocaching/cgeowaypoint.java @@ -88,11 +88,11 @@ public class cgeowaypoint extends AbstractActivity { } Button buttonEdit = (Button) findViewById(R.id.edit); - buttonEdit.setOnClickListener(new editWaypointListener(waypoint.id)); + buttonEdit.setOnClickListener(new editWaypointListener()); Button buttonDelete = (Button) findViewById(R.id.delete); - if (waypoint.type != null && waypoint.type.equalsIgnoreCase("own")) { - buttonDelete.setOnClickListener(new deleteWaypointListener(waypoint.id)); + if (waypoint.isUserDefined()) { + buttonDelete.setOnClickListener(new deleteWaypointListener()); buttonDelete.setVisibility(View.VISIBLE); } @@ -294,12 +294,6 @@ public class cgeowaypoint extends AbstractActivity { private class editWaypointListener implements View.OnClickListener { - private int id = -1; - - public editWaypointListener(int idIn) { - id = idIn; - } - public void onClick(View arg0) { Intent editIntent = new Intent(cgeowaypoint.this, cgeowaypointadd.class); editIntent.putExtra("waypoint", id); @@ -309,12 +303,6 @@ public class cgeowaypoint extends AbstractActivity { private class deleteWaypointListener implements View.OnClickListener { - private Integer id = null; - - public deleteWaypointListener(int idIn) { - id = idIn; - } - public void onClick(View arg0) { if (app.deleteWaypoint(id) == false) { showToast(res.getString(R.string.err_waypoint_delete_failed)); -- cgit v1.1