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;
17 * Class to handle the loading of zipped xml files
19 public class ZipFileLoader
21 /** App for callback of file loading */
22 private App _app = null;
23 /** Object to do the handling of the xml */
24 XmlFileLoader _xmlLoader = null;
28 * @param inApp App object
29 * @param inXmlLoader object to do the xml handling
31 public ZipFileLoader(App inApp, XmlFileLoader inXmlLoader)
34 _xmlLoader = inXmlLoader;
38 * Open the selected file and select appropriate xml loader
39 * @param inFile File to open
41 public void openFile(File inFile)
45 ZipFile file = new ZipFile(inFile);
46 Enumeration<?> entries = file.entries();
47 boolean xmlFound = false;
48 while (entries.hasMoreElements() && !xmlFound)
50 ZipEntry entry = (ZipEntry) entries.nextElement();
51 String entryName = entry.toString();
52 if (entryName != null && entryName.length() > 4)
54 String suffix = entryName.substring(entryName.length()-4).toLowerCase();
55 if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
58 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
59 saxParser.parse(file.getInputStream(entry), _xmlLoader);
60 XmlHandler handler = _xmlLoader.getHandler();
61 if (handler == null) {
62 _app.showErrorMessage("error.load.dialogtitle", "error.load.othererror");
66 _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
67 Altitude.Format.METRES, inFile.getName());
74 // Check whether there was an xml file inside
76 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
80 System.err.println("Error: " + e.getClass().getName() + " -message= " + e.getMessage());
85 * Use the given stream to access a remote zip file
86 * @param inStream stream to use to access file
88 public void openStream(InputStream inStream)
92 ZipInputStream zis = new ZipInputStream(inStream);
93 boolean xmlFound = false;
94 while (!xmlFound && zis.available() > 0)
96 ZipEntry entry = zis.getNextEntry();
97 String entryName = entry.toString();
98 if (entryName != null && entryName.length() > 4)
100 String suffix = entryName.substring(entryName.length()-4).toLowerCase();
101 if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
104 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
105 saxParser.parse(zis, _xmlLoader);
106 XmlHandler handler = _xmlLoader.getHandler();
107 if (handler == null) {
108 _app.showErrorMessage("error.load.dialogtitle", "error.load.othererror");
112 _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
113 Altitude.Format.METRES, "gpsies");
119 // Check whether there was an xml file inside
121 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
124 catch (Exception e) {
125 System.err.println("Error: " + e.getClass().getName() + " -message= " + e.getMessage());