]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/xml/GzipFileLoader.java
3ebd4caae7658eca9e345ada1c25d202ef4fde53
[GpsPrune.git] / tim / prune / load / xml / GzipFileLoader.java
1 package tim.prune.load.xml;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.util.zip.GZIPInputStream;
6 import javax.xml.parsers.SAXParser;
7 import javax.xml.parsers.SAXParserFactory;
8 import tim.prune.App;
9 import tim.prune.I18nManager;
10 import tim.prune.data.SourceInfo;
11 import tim.prune.load.MediaLinkInfo;
12
13 /**
14  * Class to handle the loading of gzipped xml files
15  */
16 public class GzipFileLoader
17 {
18         /** App for callback of file loading */
19         private App _app = null;
20         /** Object to do the handling of the xml */
21         XmlFileLoader _xmlLoader = null;
22
23         /**
24          * Constructor
25          * @param inApp App object
26          * @param inXmlLoader object to do the xml handling
27          */
28         public GzipFileLoader(App inApp, XmlFileLoader inXmlLoader)
29         {
30                 _app = inApp;
31                 _xmlLoader = inXmlLoader;
32         }
33
34         /**
35          * Open the selected file and select appropriate xml loader
36          * @param inFile File to open
37          */
38         public void openFile(File inFile)
39         {
40                 GZIPInputStream istream = null;
41                 try
42                 {
43                         istream = new GZIPInputStream(new FileInputStream(inFile));
44                         _xmlLoader.reset();
45                         SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
46                         saxParser.parse(istream, _xmlLoader);
47                         XmlHandler handler = _xmlLoader.getHandler();
48                         if (handler == null) {
49                                 _app.showErrorMessage("error.load.dialogtitle", "error.load.noread");
50                         }
51                         else
52                         {
53                                 // Send back to app
54                                 SourceInfo sourceInfo = new SourceInfo(inFile,
55                                         (handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML));
56                                 _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
57                                         null, sourceInfo, handler.getTrackNameList(),
58                                         new MediaLinkInfo(inFile, handler.getLinkArray()));
59                         }
60                 }
61                 catch (Exception e)
62                 {
63                         // Error occurred, could be a non-xml file borking the parser
64                         _app.showErrorMessageNoLookup("error.load.dialogtitle",
65                                 I18nManager.getText("error.load.othererror") + " " + e.getClass().getName());
66                         // It would be nice to verify the filename of the file inside the gz,
67                         // but the java classes don't give access to this information
68                 }
69                 finally {
70                         try {
71                                 istream.close();
72                         }
73                         catch (Exception e2) {}
74                 }
75         }
76 }