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.SourceInfo;
15 import tim.prune.load.MediaLinkInfo;
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 private 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 // Parse the stream using either Xerces or java classes
60 _xmlLoader.parseXmlStream(file.getInputStream(entry));
61 XmlHandler handler = _xmlLoader.getHandler();
62 if (handler == null) {
63 _app.showErrorMessage("error.load.dialogtitle", "error.load.othererror");
68 SourceInfo sourceInfo = new SourceInfo(inFile,
69 (handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML));
70 _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
71 null, sourceInfo, handler.getTrackNameList(),
72 new MediaLinkInfo(inFile, handler.getLinkArray()));
79 // Check whether there was an xml file inside
81 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
85 _app.showErrorMessageNoLookup("error.load.dialogtitle", e.getClass().getName() + "\n - " + e.getMessage());
90 * Use the given stream to access a remote zip file
91 * @param inStream stream to use to access file
93 public void openStream(InputStream inStream)
97 ZipInputStream zis = new ZipInputStream(inStream);
98 boolean xmlFound = false;
99 while (!xmlFound && zis.available() > 0)
101 ZipEntry entry = zis.getNextEntry();
102 String entryName = entry.toString();
103 if (entryName != null && entryName.length() > 4)
105 String suffix = entryName.substring(entryName.length()-4).toLowerCase();
106 if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
109 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
110 saxParser.parse(zis, _xmlLoader);
111 XmlHandler handler = _xmlLoader.getHandler();
112 if (handler == null) {
113 _app.showErrorMessage("error.load.dialogtitle", "error.load.othererror");
118 _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
119 new SourceInfo("gpsies", SourceInfo.FILE_TYPE.GPSIES),
120 handler.getTrackNameList());
126 // Check whether there was an xml file inside
128 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
131 catch (Exception e) {
132 System.err.println("ZipStream Error: " + e.getClass().getName() + " -message= " + e.getMessage());