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