X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fload%2Fxml%2FZipFileLoader.java;h=fff0d92aa67f1587aa6e311411bc1f43caecd293;hb=f1b92378a792131ac8fb33a869405851d5b2d1f7;hp=cf3801c3053fd34f8b7322b35f764a9add4e73c3;hpb=54b9d8bc8f0025ccf97a67d9dd217ef1f9cf082f;p=GpsPrune.git diff --git a/tim/prune/load/xml/ZipFileLoader.java b/tim/prune/load/xml/ZipFileLoader.java index cf3801c..fff0d92 100644 --- a/tim/prune/load/xml/ZipFileLoader.java +++ b/tim/prune/load/xml/ZipFileLoader.java @@ -1,15 +1,19 @@ package tim.prune.load.xml; import java.io.File; +import java.io.InputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; +import java.util.zip.ZipInputStream; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import tim.prune.App; import tim.prune.data.Altitude; +import tim.prune.data.SourceInfo; +import tim.prune.load.MediaLinkInfo; /** * Class to handle the loading of zipped xml files @@ -19,7 +23,7 @@ public class ZipFileLoader /** App for callback of file loading */ private App _app = null; /** Object to do the handling of the xml */ - XmlFileLoader _xmlLoader = null; + private XmlFileLoader _xmlLoader = null; /** * Constructor @@ -33,7 +37,7 @@ public class ZipFileLoader } /** - * Open the selected file and show the GUI dialog to select load options + * Open the selected file and select appropriate xml loader * @param inFile File to open */ public void openFile(File inFile) @@ -59,10 +63,14 @@ public class ZipFileLoader if (handler == null) { _app.showErrorMessage("error.load.dialogtitle", "error.load.othererror"); } - else { + else + { // Send back to app + SourceInfo sourceInfo = new SourceInfo(inFile, + (handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML)); _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(), - Altitude.Format.METRES, inFile.getName()); + Altitude.Format.METRES, sourceInfo, handler.getTrackNameList(), + new MediaLinkInfo(inFile, handler.getLinkArray())); xmlFound = true; } } @@ -75,8 +83,54 @@ public class ZipFileLoader } } catch (Exception e) { - System.err.println("Error: " + e.getMessage()); + _app.showErrorMessageNoLookup("error.load.dialogtitle", e.getClass().getName() + "\n - " + e.getMessage()); } } + /** + * Use the given stream to access a remote zip file + * @param inStream stream to use to access file + */ + public void openStream(InputStream inStream) + { + try + { + ZipInputStream zis = new ZipInputStream(inStream); + boolean xmlFound = false; + while (!xmlFound && zis.available() > 0) + { + ZipEntry entry = zis.getNextEntry(); + String entryName = entry.toString(); + if (entryName != null && entryName.length() > 4) + { + String suffix = entryName.substring(entryName.length()-4).toLowerCase(); + if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml")) + { + _xmlLoader.reset(); + SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); + saxParser.parse(zis, _xmlLoader); + XmlHandler handler = _xmlLoader.getHandler(); + if (handler == null) { + _app.showErrorMessage("error.load.dialogtitle", "error.load.othererror"); + } + else + { + // Send back to app + _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(), + Altitude.Format.METRES, new SourceInfo("gpsies", SourceInfo.FILE_TYPE.GPSIES), + handler.getTrackNameList()); + xmlFound = true; + } + } + } + } + // Check whether there was an xml file inside + if (!xmlFound) { + _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip"); + } + } + catch (Exception e) { + System.err.println("ZipStream Error: " + e.getClass().getName() + " -message= " + e.getMessage()); + } + } }