aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/files/GPXImporter.java
diff options
context:
space:
mode:
authorStephan Merker <merker.stephan@googlemail.com>2011-11-21 22:35:17 +0100
committerStephan Merker <merker.stephan@googlemail.com>2011-11-21 22:37:30 +0100
commitde56503fb781d832e6f8eec656675c289863d894 (patch)
treedefa529667ae45d95232dd5c2c591ae38750b559 /main/src/cgeo/geocaching/files/GPXImporter.java
parent6ac087e09a128e6383e0a13f4ce7b09da8455717 (diff)
downloadcgeo-de56503fb781d832e6f8eec656675c289863d894.zip
cgeo-de56503fb781d832e6f8eec656675c289863d894.tar.gz
cgeo-de56503fb781d832e6f8eec656675c289863d894.tar.bz2
refactored import thread classes
- common GPX10 and GPX11 handling - one zip import implementation is enough
Diffstat (limited to 'main/src/cgeo/geocaching/files/GPXImporter.java')
-rw-r--r--main/src/cgeo/geocaching/files/GPXImporter.java208
1 files changed, 82 insertions, 126 deletions
diff --git a/main/src/cgeo/geocaching/files/GPXImporter.java b/main/src/cgeo/geocaching/files/GPXImporter.java
index 68bfd40..15331b1 100644
--- a/main/src/cgeo/geocaching/files/GPXImporter.java
+++ b/main/src/cgeo/geocaching/files/GPXImporter.java
@@ -24,16 +24,17 @@ import android.os.Message;
import android.util.Log;
import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CancellationException;
import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class GPXImporter {
+ static final int IMPORT_STEP_START = 0;
static final int IMPORT_STEP_READ_FILE = 1;
static final int IMPORT_STEP_READ_WPT_FILE = 2;
static final int IMPORT_STEP_STORE_CACHES = 3;
@@ -120,6 +121,7 @@ public class GPXImporter {
public void run() {
final Collection<cgCache> caches;
try {
+ importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_START));
caches = doImport();
importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_STORE_CACHES, R.string.gpx_import_storing, caches.size()));
@@ -162,108 +164,69 @@ public class GPXImporter {
}
}
- static class ImportGpxFileThread extends ImportThread {
- private final File cacheFile;
+ static class ImportLocFileThread extends ImportThread {
+ private final File file;
- public ImportGpxFileThread(final File file, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
+ public ImportLocFileThread(final File file, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
super(listId, importStepHandler, progressHandler);
- this.cacheFile = file;
+ this.file = file;
}
@Override
protected Collection<cgCache> doImport() throws IOException, ParserException {
- Log.i(Settings.tag, "Import GPX file: " + cacheFile.getAbsolutePath());
- importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, (int) cacheFile.length()));
- Collection<cgCache> caches;
- GPXParser parser;
- try {
- // try to parse cache file as GPX 10
- parser = new GPX10Parser(listId);
- caches = parser.parse(cacheFile, progressHandler);
- } catch (ParserException pe) {
- // didn't work -> lets try GPX11
- parser = new GPX11Parser(listId);
- caches = parser.parse(cacheFile, progressHandler);
- }
-
- final File wptsFile = new File(cacheFile.getParentFile(), getWaypointsFileNameForGpxFileName(cacheFile.getName()));
- if (wptsFile.canRead()) {
- Log.i(Settings.tag, "Import GPX waypoint file: " + wptsFile.getAbsolutePath());
- importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_WPT_FILE, R.string.gpx_import_loading_waypoints, (int) wptsFile.length()));
- caches = parser.parse(wptsFile, progressHandler);
- }
-
- return caches;
+ Log.i(Settings.tag, "Import LOC file: " + file.getAbsolutePath());
+ importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, (int) file.length()));
+ LocParser parser = new LocParser(listId);
+ return parser.parse(file, progressHandler);
}
}
- static class ImportGpxZipFileThread extends ImportThread {
- private final File cacheFile;
+ static abstract class ImportGpxThread extends ImportThread {
- public ImportGpxZipFileThread(final File file, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
+ public ImportGpxThread(int listId, Handler importStepHandler, CancellableHandler progressHandler) {
super(listId, importStepHandler, progressHandler);
- this.cacheFile = file;
}
@Override
protected Collection<cgCache> doImport() throws IOException, ParserException {
- Log.i(Settings.tag, "Import GPX zip file: " + cacheFile.getAbsolutePath());
- ZipFile zipFile = new ZipFile(cacheFile);
try {
- final String gpxName = getGpxFileNameForZipFileName(cacheFile.getName());
- if (gpxName == null) {
- throw new ParserException(cacheFile.getName() + " is not a GPX zip file");
- }
- final ZipEntry gpxEntry = zipFile.getEntry(gpxName);
- if (gpxEntry == null) {
- throw new ParserException(cacheFile.getName() + " is not a GPX zip file");
- }
- importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, (int) gpxEntry.getSize()));
-
- Collection<cgCache> caches;
- GPXParser parser;
- try {
- // try to parse cache file as GPX 10
- parser = new GPX10Parser(listId);
- caches = parser.parse(zipFile.getInputStream(gpxEntry), progressHandler);
- } catch (ParserException pe) {
- // didn't work -> lets try GPX11
- parser = new GPX11Parser(listId);
- caches = parser.parse(zipFile.getInputStream(gpxEntry), progressHandler);
- }
-
- final ZipEntry gpxWptsEntry = zipFile.getEntry(getWaypointsFileNameForGpxFileName(gpxName));
- if (gpxWptsEntry != null) {
- Log.i(Settings.tag, "Import GPX waypoint file: " + gpxWptsEntry.getName());
- importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_WPT_FILE, R.string.gpx_import_loading_waypoints, (int) gpxWptsEntry.getSize()));
- caches = parser.parse(zipFile.getInputStream(gpxWptsEntry), progressHandler);
- }
-
- return caches;
- } finally {
- zipFile.close();
+ // try to parse cache file as GPX 10
+ return doImport(new GPX10Parser(listId));
+ } catch (ParserException pe) {
+ // didn't work -> lets try GPX11
+ return doImport(new GPX11Parser(listId));
}
}
+
+ protected abstract Collection<cgCache> doImport(GPXParser parser) throws IOException, ParserException;
}
- static class ImportLocFileThread extends ImportThread {
- private final File file;
+ static class ImportGpxFileThread extends ImportGpxThread {
+ private final File cacheFile;
- public ImportLocFileThread(final File file, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
+ public ImportGpxFileThread(final File file, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
super(listId, importStepHandler, progressHandler);
- this.file = file;
+ this.cacheFile = file;
}
@Override
- protected Collection<cgCache> doImport() throws IOException, ParserException {
- Log.i(Settings.tag, "Import LOC file: " + file.getAbsolutePath());
- importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, (int) file.length()));
- LocParser parser = new LocParser(listId);
- return parser.parse(file, progressHandler);
+ protected Collection<cgCache> doImport(GPXParser parser) throws IOException, ParserException {
+ Log.i(Settings.tag, "Import GPX file: " + cacheFile.getAbsolutePath());
+ importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, (int) cacheFile.length()));
+ Collection<cgCache> caches = parser.parse(cacheFile, progressHandler);
+
+ final File wptsFile = new File(cacheFile.getParentFile(), getWaypointsFileNameForGpxFileName(cacheFile.getName()));
+ if (wptsFile.canRead()) {
+ Log.i(Settings.tag, "Import GPX waypoint file: " + wptsFile.getAbsolutePath());
+ importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_WPT_FILE, R.string.gpx_import_loading_waypoints, (int) wptsFile.length()));
+ caches = parser.parse(wptsFile, progressHandler);
+ }
+
+ return caches;
}
}
- static class ImportGpxAttachmentThread extends ImportThread {
+ static class ImportGpxAttachmentThread extends ImportGpxThread {
private final Uri uri;
private ContentResolver contentResolver;
@@ -274,25 +237,9 @@ public class GPXImporter {
}
@Override
- protected Collection<cgCache> doImport() throws IOException, ParserException {
+ protected Collection<cgCache> doImport(GPXParser parser) throws IOException, ParserException {
Log.i(Settings.tag, "Import GPX from uri: " + uri);
importStepHandler.sendMessage(importStepHandler.obtainMessage(IMPORT_STEP_READ_FILE, R.string.gpx_import_loading_caches, -1));
-
- Collection<cgCache> caches;
- GPXParser parser;
- try {
- // try to parse cache file as GPX 10
- parser = new GPX10Parser(listId);
- caches = parseAttachment(parser);
- } catch (ParserException pe) {
- // didn't work -> lets try GPX11
- parser = new GPX11Parser(listId);
- caches = parseAttachment(parser);
- }
- return caches;
- }
-
- private Collection<cgCache> parseAttachment(GPXParser parser) throws IOException, ParserException {
InputStream is = contentResolver.openInputStream(uri);
try {
return parser.parse(is, progressHandler);
@@ -302,34 +249,18 @@ public class GPXImporter {
}
}
- static class ImportGpxZipAttachmentThread extends ImportThread {
- private final Uri uri;
- private ContentResolver contentResolver;
+ static abstract class ImportGpxZipThread extends ImportGpxThread {
- public ImportGpxZipAttachmentThread(Uri uri, ContentResolver contentResolver, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
+ public ImportGpxZipThread(int listId, Handler importStepHandler, CancellableHandler progressHandler) {
super(listId, importStepHandler, progressHandler);
- this.uri = uri;
- this.contentResolver = contentResolver;
}
@Override
- protected Collection<cgCache> doImport() throws IOException, ParserException {
- Log.i(Settings.tag, "Import zipped GPX from uri: " + uri);
-
- try {
- // try to parse cache file as GPX 10
- return parseAttachment(new GPX10Parser(listId));
- } catch (ParserException pe) {
- // didn't work -> lets try GPX11
- return parseAttachment(new GPX11Parser(listId));
- }
- }
-
- private Collection<cgCache> parseAttachment(GPXParser parser) throws IOException, ParserException {
+ protected Collection<cgCache> doImport(GPXParser parser) throws IOException, ParserException {
Collection<cgCache> caches = Collections.emptySet();
// can't assume that GPX file comes before waypoint file in zip -> so we need two passes
// 1. parse GPX files
- ZipInputStream zis = new ZipInputStream(contentResolver.openInputStream(uri));
+ ZipInputStream zis = new ZipInputStream(getInputStream());
try {
for (ZipEntry zipEntry = zis.getNextEntry(); zipEntry != null; zipEntry = zis.getNextEntry()) {
if (StringUtils.endsWithIgnoreCase(zipEntry.getName(), GPX_FILE_EXTENSION)) {
@@ -338,7 +269,7 @@ public class GPXImporter {
caches = parser.parse(new NoCloseInputStream(zis), progressHandler);
}
} else {
- throw new ParserException(uri.toString() + " is not a GPX zip file");
+ throw new ParserException("Imported zip is not a GPX zip file.");
}
zis.closeEntry();
}
@@ -347,7 +278,7 @@ public class GPXImporter {
}
// 2. parse waypoint files
- zis = new ZipInputStream(contentResolver.openInputStream(uri));
+ zis = new ZipInputStream(getInputStream());
try {
for (ZipEntry zipEntry = zis.getNextEntry(); zipEntry != null; zipEntry = zis.getNextEntry()) {
if (StringUtils.endsWithIgnoreCase(zipEntry.getName(), WAYPOINTS_FILE_SUFFIX_AND_EXTENSION)) {
@@ -362,6 +293,40 @@ public class GPXImporter {
return caches;
}
+
+ protected abstract InputStream getInputStream() throws IOException;
+ }
+
+ static class ImportGpxZipFileThread extends ImportGpxZipThread {
+ private final File cacheFile;
+
+ public ImportGpxZipFileThread(final File file, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
+ super(listId, importStepHandler, progressHandler);
+ this.cacheFile = file;
+ Log.i(Settings.tag, "Import zipped GPX: " + file);
+ }
+
+ @Override
+ protected InputStream getInputStream() throws IOException {
+ return new FileInputStream(cacheFile);
+ }
+ }
+
+ static class ImportGpxZipAttachmentThread extends ImportGpxZipThread {
+ private final Uri uri;
+ private ContentResolver contentResolver;
+
+ public ImportGpxZipAttachmentThread(Uri uri, ContentResolver contentResolver, int listId, Handler importStepHandler, CancellableHandler progressHandler) {
+ super(listId, importStepHandler, progressHandler);
+ this.uri = uri;
+ this.contentResolver = contentResolver;
+ Log.i(Settings.tag, "Import zipped GPX from uri: " + uri);
+ }
+
+ @Override
+ protected InputStream getInputStream() throws IOException {
+ return contentResolver.openInputStream(uri);
+ }
}
final private CancellableHandler progressHandler = new CancellableHandler() {
@@ -375,12 +340,12 @@ public class GPXImporter {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
- case IMPORT_STEP_READ_FILE:
+ case IMPORT_STEP_START:
Message cancelMessage = importStepHandler.obtainMessage(IMPORT_STEP_CANCEL);
- progress.show((Context) fromActivity, res.getString(R.string.gpx_import_title_reading_file), res.getString(msg.arg1), ProgressDialog.STYLE_HORIZONTAL, cancelMessage);
- progress.setMaxProgressAndReset(msg.arg2);
+ progress.show((Context) fromActivity, res.getString(R.string.gpx_import_title_reading_file), res.getString(R.string.gpx_import_loading_caches), ProgressDialog.STYLE_HORIZONTAL, cancelMessage);
break;
+ case IMPORT_STEP_READ_FILE:
case IMPORT_STEP_READ_WPT_FILE:
case IMPORT_STEP_STORE_CACHES:
progress.setMessage(res.getString(msg.arg1));
@@ -412,15 +377,6 @@ public class GPXImporter {
}
};
- // 1234567.zip -> 1234567.gpx
- static String getGpxFileNameForZipFileName(String name) {
- if (StringUtils.endsWithIgnoreCase(name, ZIP_FILE_EXTENSION) && (StringUtils.length(name) > ZIP_FILE_EXTENSION.length())) {
- return StringUtils.substringBeforeLast(name, ".") + GPX_FILE_EXTENSION;
- } else {
- return null;
- }
- }
-
// 1234567.gpx -> 1234567-wpts.gpx
static String getWaypointsFileNameForGpxFileName(String name) {
if (StringUtils.endsWithIgnoreCase(name, GPX_FILE_EXTENSION) && (StringUtils.length(name) > GPX_FILE_EXTENSION.length())) {