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