]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/xml/ZipFileLoader.java
74e48f9497c6ab387dd779b2a97747511e320a85
[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.SourceInfo;
15 import tim.prune.load.MediaLinkInfo;
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                                                 {
67                                                         // Send back to app
68                                                         SourceInfo sourceInfo = new SourceInfo(inFile,
69                                                                 (handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML));
70                                                         _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
71                                                                 null, sourceInfo, handler.getTrackNameList(),
72                                                                 new MediaLinkInfo(inFile, handler.getLinkArray()));
73                                                         xmlFound = true;
74                                                 }
75                                         }
76                                 }
77                         }
78                         file.close();
79                         // Check whether there was an xml file inside
80                         if (!xmlFound) {
81                                 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
82                         }
83                 }
84                 catch (Exception e) {
85                         _app.showErrorMessageNoLookup("error.load.dialogtitle", e.getClass().getName() + "\n - " + e.getMessage());
86                 }
87         }
88
89         /**
90          * Use the given stream to access a remote zip file
91          * @param inStream stream to use to access file
92          */
93         public void openStream(InputStream inStream)
94         {
95                 try
96                 {
97                         ZipInputStream zis = new ZipInputStream(inStream);
98                         boolean xmlFound = false;
99                         while (!xmlFound && zis.available() > 0)
100                         {
101                                 ZipEntry entry = zis.getNextEntry();
102                                 String entryName = entry.toString();
103                                 if (entryName != null && entryName.length() > 4)
104                                 {
105                                         String suffix = entryName.substring(entryName.length()-4).toLowerCase();
106                                         if (suffix.equals(".kml") || suffix.equals(".gpx") || suffix.equals(".xml"))
107                                         {
108                                                 _xmlLoader.reset();
109                                                 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
110                                                 saxParser.parse(zis, _xmlLoader);
111                                                 XmlHandler handler = _xmlLoader.getHandler();
112                                                 if (handler == null) {
113                                                         _app.showErrorMessage("error.load.dialogtitle", "error.load.othererror");
114                                                 }
115                                                 else
116                                                 {
117                                                         // Send back to app
118                                                         _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
119                                                                 new SourceInfo("gpsies", SourceInfo.FILE_TYPE.GPSIES),
120                                                                 handler.getTrackNameList());
121                                                         xmlFound = true;
122                                                 }
123                                         }
124                                 }
125                         }
126                         // Check whether there was an xml file inside
127                         if (!xmlFound) {
128                                 _app.showErrorMessage("error.load.dialogtitle", "error.load.noxmlinzip");
129                         }
130                 }
131                 catch (Exception e) {
132                         System.err.println("ZipStream Error: " + e.getClass().getName() + " -message= " + e.getMessage());
133                 }
134         }
135 }