1 package tim.prune.load.xml;
4 import java.util.Enumeration;
5 import java.util.zip.ZipEntry;
6 import java.util.zip.ZipFile;
8 import javax.xml.parsers.SAXParser;
9 import javax.xml.parsers.SAXParserFactory;
12 import tim.prune.data.Altitude;
15 * Class to handle the loading of zipped xml files
17 public class ZipFileLoader
19 /** App for callback of file loading */
20 private App _app = null;
21 /** Object to do the handling of the xml */
22 XmlFileLoader _xmlLoader = null;
26 * @param inApp App object
27 * @param inXmlLoader object to do the xml handling
29 public ZipFileLoader(App inApp, XmlFileLoader inXmlLoader)
32 _xmlLoader = inXmlLoader;
36 * Open the selected file and show the GUI dialog to select load options
37 * @param inFile File to open
39 public void openFile(File inFile)
43 ZipFile file = new ZipFile(inFile);
44 Enumeration<?> entries = file.entries();
45 boolean xmlFound = false;
46 while (entries.hasMoreElements() && !xmlFound)
48 ZipEntry entry = (ZipEntry) entries.nextElement();
49 String entryName = entry.toString();
50 if (entryName != null && entryName.length() > 4)
52 String suffix = entryName.substring(entryName.length()-4).toLowerCase();
53 if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
56 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
57 saxParser.parse(file.getInputStream(entry), _xmlLoader);
58 XmlHandler handler = _xmlLoader.getHandler();
59 if (handler == null) {
60 _app.showErrorMessage("error.load.dialogtitle", "error.load.othererror");
64 _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
65 Altitude.Format.METRES, inFile.getName());
72 // Check whether there was an xml file inside
74 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
78 System.err.println("Error: " + e.getMessage());