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;
16 import tim.prune.load.MediaLinkInfo;
19 * Class to handle the loading of zipped xml files
21 public class ZipFileLoader
23 /** App for callback of file loading */
24 private App _app = null;
25 /** Object to do the handling of the xml */
26 private XmlFileLoader _xmlLoader = null;
30 * @param inApp App object
31 * @param inXmlLoader object to do the xml handling
33 public ZipFileLoader(App inApp, XmlFileLoader inXmlLoader)
36 _xmlLoader = inXmlLoader;
40 * Open the selected file and select appropriate xml loader
41 * @param inFile File to open
43 public void openFile(File inFile)
47 ZipFile file = new ZipFile(inFile);
48 Enumeration<?> entries = file.entries();
49 boolean xmlFound = false;
50 while (entries.hasMoreElements() && !xmlFound)
52 ZipEntry entry = (ZipEntry) entries.nextElement();
53 String entryName = entry.toString();
54 if (entryName != null && entryName.length() > 4)
56 String suffix = entryName.substring(entryName.length()-4).toLowerCase();
57 if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
60 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
61 saxParser.parse(file.getInputStream(entry), _xmlLoader);
62 XmlHandler handler = _xmlLoader.getHandler();
63 if (handler == null) {
64 _app.showErrorMessage("error.load.dialogtitle", "error.load.othererror");
69 SourceInfo sourceInfo = new SourceInfo(inFile,
70 (handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML));
71 _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
72 Altitude.Format.METRES, sourceInfo, handler.getTrackNameList(),
73 new MediaLinkInfo(inFile, handler.getLinkArray()));
80 // Check whether there was an xml file inside
82 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
86 System.err.println("ZipFile Error: " + e.getClass().getName() + " -message= " + e.getMessage());
91 * Use the given stream to access a remote zip file
92 * @param inStream stream to use to access file
94 public void openStream(InputStream inStream)
98 ZipInputStream zis = new ZipInputStream(inStream);
99 boolean xmlFound = false;
100 while (!xmlFound && zis.available() > 0)
102 ZipEntry entry = zis.getNextEntry();
103 String entryName = entry.toString();
104 if (entryName != null && entryName.length() > 4)
106 String suffix = entryName.substring(entryName.length()-4).toLowerCase();
107 if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
110 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
111 saxParser.parse(zis, _xmlLoader);
112 XmlHandler handler = _xmlLoader.getHandler();
113 if (handler == null) {
114 _app.showErrorMessage("error.load.dialogtitle", "error.load.othererror");
119 _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
120 Altitude.Format.METRES, new SourceInfo("gpsies", SourceInfo.FILE_TYPE.GPSIES),
121 handler.getTrackNameList());
127 // Check whether there was an xml file inside
129 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
132 catch (Exception e) {
133 System.err.println("ZipStream Error: " + e.getClass().getName() + " -message= " + e.getMessage());