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