1 package tim.prune.load.xml;
4 import java.io.InputStream;
5 import java.util.Enumeration;
6 import java.util.zip.ZipEntry;
7 import java.util.zip.ZipFile;
8 import java.util.zip.ZipInputStream;
10 import javax.xml.parsers.SAXParser;
11 import javax.xml.parsers.SAXParserFactory;
14 import tim.prune.data.Altitude;
15 import tim.prune.data.SourceInfo;
18 * Class to handle the loading of zipped xml files
20 public class ZipFileLoader
22 /** App for callback of file loading */
23 private App _app = null;
24 /** Object to do the handling of the xml */
25 XmlFileLoader _xmlLoader = null;
29 * @param inApp App object
30 * @param inXmlLoader object to do the xml handling
32 public ZipFileLoader(App inApp, XmlFileLoader inXmlLoader)
35 _xmlLoader = inXmlLoader;
39 * Open the selected file and select appropriate xml loader
40 * @param inFile File to open
42 public void openFile(File inFile)
46 ZipFile file = new ZipFile(inFile);
47 Enumeration<?> entries = file.entries();
48 boolean xmlFound = false;
49 while (entries.hasMoreElements() && !xmlFound)
51 ZipEntry entry = (ZipEntry) entries.nextElement();
52 String entryName = entry.toString();
53 if (entryName != null && entryName.length() > 4)
55 String suffix = entryName.substring(entryName.length()-4).toLowerCase();
56 if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
59 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
60 saxParser.parse(file.getInputStream(entry), _xmlLoader);
61 XmlHandler handler = _xmlLoader.getHandler();
62 if (handler == null) {
63 _app.showErrorMessage("error.load.dialogtitle", "error.load.othererror");
67 SourceInfo sourceInfo = new SourceInfo(inFile,
68 (handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML));
69 _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
70 Altitude.Format.METRES, sourceInfo);
77 // Check whether there was an xml file inside
79 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
83 System.err.println("ZipFile Error: " + e.getClass().getName() + " -message= " + e.getMessage());
88 * Use the given stream to access a remote zip file
89 * @param inStream stream to use to access file
91 public void openStream(InputStream inStream)
95 ZipInputStream zis = new ZipInputStream(inStream);
96 boolean xmlFound = false;
97 while (!xmlFound && zis.available() > 0)
99 ZipEntry entry = zis.getNextEntry();
100 String entryName = entry.toString();
101 if (entryName != null && entryName.length() > 4)
103 String suffix = entryName.substring(entryName.length()-4).toLowerCase();
104 if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
107 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
108 saxParser.parse(zis, _xmlLoader);
109 XmlHandler handler = _xmlLoader.getHandler();
110 if (handler == null) {
111 _app.showErrorMessage("error.load.dialogtitle", "error.load.othererror");
115 _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
116 Altitude.Format.METRES, new SourceInfo("gpsies", SourceInfo.FILE_TYPE.GPSIES));
122 // Check whether there was an xml file inside
124 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
127 catch (Exception e) {
128 System.err.println("ZipStream Error: " + e.getClass().getName() + " -message= " + e.getMessage());