]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/xml/ZipFileLoader.java
Version 8, September 2009
[GpsPrune.git] / tim / prune / load / xml / ZipFileLoader.java
1 package tim.prune.load.xml;
2
3 import java.io.File;
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;
9
10 import javax.xml.parsers.SAXParser;
11 import javax.xml.parsers.SAXParserFactory;
12
13 import tim.prune.App;
14 import tim.prune.data.Altitude;
15
16 /**
17  * Class to handle the loading of zipped xml files
18  */
19 public class ZipFileLoader
20 {
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;
25
26         /**
27          * Constructor
28          * @param inApp App object
29          * @param inXmlLoader object to do the xml handling
30          */
31         public ZipFileLoader(App inApp, XmlFileLoader inXmlLoader)
32         {
33                 _app = inApp;
34                 _xmlLoader = inXmlLoader;
35         }
36
37         /**
38          * Open the selected file and select appropriate xml loader
39          * @param inFile File to open
40          */
41         public void openFile(File inFile)
42         {
43                 try
44                 {
45                         ZipFile file = new ZipFile(inFile);
46                         Enumeration<?> entries = file.entries();
47                         boolean xmlFound = false;
48                         while (entries.hasMoreElements() && !xmlFound)
49                         {
50                                 ZipEntry entry = (ZipEntry) entries.nextElement();
51                                 String entryName = entry.toString();
52                                 if (entryName != null && entryName.length() > 4)
53                                 {
54                                         String suffix = entryName.substring(entryName.length()-4).toLowerCase();
55                                         if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
56                                         {
57                                                 _xmlLoader.reset();
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");
63                                                 }
64                                                 else {
65                                                         // Send back to app
66                                                         _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
67                                                                 Altitude.Format.METRES, inFile.getName());
68                                                         xmlFound = true;
69                                                 }
70                                         }
71                                 }
72                         }
73                         file.close();
74                         // Check whether there was an xml file inside
75                         if (!xmlFound) {
76                                 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
77                         }
78                 }
79                 catch (Exception e) {
80                         System.err.println("Error: " + e.getClass().getName() + " -message= " + e.getMessage());
81                 }
82         }
83
84         /**
85          * Use the given stream to access a remote zip file
86          * @param inStream stream to use to access file
87          */
88         public void openStream(InputStream inStream)
89         {
90                 try
91                 {
92                         ZipInputStream zis = new ZipInputStream(inStream);
93                         boolean xmlFound = false;
94                         while (!xmlFound && zis.available() > 0)
95                         {
96                                 ZipEntry entry = zis.getNextEntry();
97                                 String entryName = entry.toString();
98                                 if (entryName != null && entryName.length() > 4)
99                                 {
100                                         String suffix = entryName.substring(entryName.length()-4).toLowerCase();
101                                         if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
102                                         {
103                                                 _xmlLoader.reset();
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");
109                                                 }
110                                                 else {
111                                                         // Send back to app
112                                                         _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
113                                                                 Altitude.Format.METRES, "gpsies");
114                                                         xmlFound = true;
115                                                 }
116                                         }
117                                 }
118                         }
119                         // Check whether there was an xml file inside
120                         if (!xmlFound) {
121                                 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
122                         }
123                 }
124                 catch (Exception e) {
125                         System.err.println("Error: " + e.getClass().getName() + " -message= " + e.getMessage());
126                 }
127         }
128 }