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