]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/xml/GzipFileLoader.java
Version 11, August 2010
[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
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                                 // Send back to app
53                                 SourceInfo sourceInfo = new SourceInfo(inFile,
54                                         (handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML));
55                                 _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(),
56                                         Altitude.Format.METRES, sourceInfo, handler.getTrackNameList());
57                         }
58                 }
59                 catch (Exception e) {
60                         // Error occurred, could be a non-xml file borking the parser
61                         _app.showErrorMessageNoLookup("error.load.dialogtitle",
62                                 I18nManager.getText("error.load.othererror") + " " + e.getClass().getName());
63                         // It would be nice to verify the filename of the file inside the gz,
64                         // but the java classes don't give access to this information
65                 }
66                 finally {
67                         try {
68                                 istream.close();
69                         }
70                         catch (Exception e2) {}
71                 }
72         }
73 }