]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/xml/ZipFileLoader.java
Version 7, February 2009
[GpsPrune.git] / tim / prune / load / xml / ZipFileLoader.java
1 package tim.prune.load.xml;
2
3 import java.io.File;
4 import java.util.Enumeration;
5 import java.util.zip.ZipEntry;
6 import java.util.zip.ZipFile;
7
8 import javax.xml.parsers.SAXParser;
9 import javax.xml.parsers.SAXParserFactory;
10
11 import tim.prune.App;
12 import tim.prune.data.Altitude;
13
14 /**
15  * Class to handle the loading of zipped xml files
16  */
17 public class ZipFileLoader
18 {
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;
23
24         /**
25          * Constructor
26          * @param inApp App object
27          * @param inXmlLoader object to do the xml handling
28          */
29         public ZipFileLoader(App inApp, XmlFileLoader inXmlLoader)
30         {
31                 _app = inApp;
32                 _xmlLoader = inXmlLoader;
33         }
34
35         /**
36          * Open the selected file and show the GUI dialog to select load options
37          * @param inFile File to open
38          */
39         public void openFile(File inFile)
40         {
41                 try
42                 {
43                         ZipFile file = new ZipFile(inFile);
44                         Enumeration<?> entries = file.entries();
45                         boolean xmlFound = false;
46                         while (entries.hasMoreElements() && !xmlFound)
47                         {
48                                 ZipEntry entry = (ZipEntry) entries.nextElement();
49                                 String entryName = entry.toString();
50                                 if (entryName != null && entryName.length() > 4)
51                                 {
52                                         String suffix = entryName.substring(entryName.length()-4).toLowerCase();
53                                         if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
54                                         {
55                                                 _xmlLoader.reset();
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");
61                                                 }
62                                                 else {
63                                                         // Send back to app
64                                                         _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
65                                                                 Altitude.Format.METRES, inFile.getName());
66                                                         xmlFound = true;
67                                                 }
68                                         }
69                                 }
70                         }
71                         file.close();
72                         // Check whether there was an xml file inside
73                         if (!xmlFound) {
74                                 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
75                         }
76                 }
77                 catch (Exception e) {
78                         System.err.println("Error: " + e.getMessage());
79                 }
80         }
81
82 }