]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/xml/XmlFileLoader.java
Version 19.2, December 2018
[GpsPrune.git] / tim / prune / load / xml / XmlFileLoader.java
1 package tim.prune.load.xml;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8
9 import javax.xml.parsers.SAXParser;
10 import javax.xml.parsers.SAXParserFactory;
11
12 import org.xml.sax.Attributes;
13 import org.xml.sax.InputSource;
14 import org.xml.sax.SAXException;
15 import org.xml.sax.XMLReader;
16 import org.xml.sax.helpers.DefaultHandler;
17 import org.xml.sax.helpers.XMLReaderFactory;
18
19 import tim.prune.App;
20 import tim.prune.I18nManager;
21 import tim.prune.data.SourceInfo;
22 import tim.prune.load.MediaLinkInfo;
23
24 /**
25  * Class for handling loading of Xml files, and passing the
26  * loaded data back to the App object
27  */
28 public class XmlFileLoader extends DefaultHandler implements Runnable
29 {
30         private File _file = null;
31         private App _app = null;
32         private XmlHandler _handler = null;
33         private String _unknownType = null;
34
35
36         /**
37          * Constructor
38          * @param inApp Application object to inform of track load
39          */
40         public XmlFileLoader(App inApp)
41         {
42                 _app = inApp;
43         }
44
45         /**
46          * Reset the handler to ensure data cleared
47          */
48         public void reset()
49         {
50                 _handler = null;
51                 _unknownType = null;
52         }
53
54         /**
55          * Open the selected file
56          * @param inFile File to open
57          */
58         public void openFile(File inFile)
59         {
60                 _file = inFile;
61                 reset();
62                 // start new thread in case xml parsing is time-consuming
63                 new Thread(this).start();
64         }
65
66
67         /**
68          * Run method, to parse the file
69          * @see java.lang.Runnable#run()
70          */
71         public void run()
72         {
73                 FileInputStream inStream = null;
74                 boolean success = false;
75                 try
76                 {
77                         inStream = new FileInputStream(_file);
78                         success = parseXmlStream(inStream);
79                 }
80                 catch (FileNotFoundException fnfe) {}
81
82                 // Clean up the stream, don't need it any more
83                 try {inStream.close();} catch (IOException e2) {}
84
85                 if (success)
86                 {
87                         // Check whether handler was properly instantiated
88                         if (_handler == null)
89                         {
90                                 // Wasn't either kml or gpx
91                                 _app.showErrorMessageNoLookup("error.load.dialogtitle",
92                                         I18nManager.getText("error.load.unknownxml") + " " + _unknownType);
93                         }
94                         else
95                         {
96                                 SourceInfo.FILE_TYPE sourceType = (_handler instanceof GpxHandler ? SourceInfo.FILE_TYPE.GPX : SourceInfo.FILE_TYPE.KML);
97                                 SourceInfo sourceInfo = new SourceInfo(_file, sourceType);
98                                 sourceInfo.setFileTitle(_handler.getFileTitle());
99
100                                 // Pass information back to app
101                                 _app.informDataLoaded(_handler.getFieldArray(), _handler.getDataArray(),
102                                         null, sourceInfo, _handler.getTrackNameList(),
103                                         new MediaLinkInfo(_handler.getLinkArray()));
104                         }
105                 }
106         }
107
108
109         /**
110          * Try both Xerces and the built-in java classes to parse the given xml stream
111          * @param inStream input stream from file / zip / gzip
112          * @return true on success, false if both xerces and built-in parser failed
113          */
114         public boolean parseXmlStream(InputStream inStream)
115         {
116                 boolean success = false;
117                 // Firstly, try to use xerces to parse the xml (will throw an exception if not available)
118                 try
119                 {
120                         XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
121                         xmlReader.setContentHandler(this);
122                         xmlReader.parse(new InputSource(inStream));
123                         success = true; // worked
124                 }
125                 catch (Exception e) {} // don't care too much if it didn't work, there's a backup
126
127                 // If that didn't work, try the built-in classes (which work for xml1.0 but handling for 1.1 contains bugs)
128                 if (!success)
129                 {
130                         try
131                         {
132                                 // Construct a SAXParser and use this as a default handler
133                                 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
134                                 saxParser.parse(inStream, this);
135                                 success = true;
136                         }
137                         catch (Exception e)
138                         {
139                                 // Show error dialog
140                                 _app.showErrorMessageNoLookup("error.load.dialogtitle",
141                                         I18nManager.getText("error.load.othererror") + " " + e.getMessage());
142                         }
143                 }
144                 return success;
145         }
146
147         /**
148          * Receive a tag
149          * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
150          */
151         public void startElement(String uri, String localName, String qName,
152                 Attributes attributes) throws SAXException
153         {
154                 // Check for "kml" or "gpx" tags
155                 if (_handler == null)
156                 {
157                         if (qName.equals("kml")) {_handler = new KmlHandler();}
158                         else if (qName.equals("gpx")) {_handler = new GpxHandler();}
159                         else if (_unknownType == null && !qName.equals(""))
160                         {
161                                 _unknownType = qName;
162                         }
163                 }
164                 else
165                 {
166                         // Handler instantiated so pass tags on to it
167                         _handler.startElement(uri, localName, qName, attributes);
168                 }
169                 super.startElement(uri, localName, qName, attributes);
170         }
171
172
173         /**
174          * Receive characters, either between or inside tags
175          * @see org.xml.sax.ContentHandler#characters(char[], int, int)
176          */
177         public void characters(char[] ch, int start, int length)
178                 throws SAXException
179         {
180                 if (_handler != null)
181                 {
182                         // Handler instantiated so pass tags on to it
183                         _handler.characters(ch, start, length);
184                 }
185                 super.characters(ch, start, length);
186         }
187
188
189         /**
190          * Receive end of element
191          * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
192          */
193         public void endElement(String uri, String localName, String qName)
194                 throws SAXException
195         {
196                 if (_handler != null)
197                 {
198                         // Handler instantiated so pass tags on to it
199                         _handler.endElement(uri, localName, qName);
200                 }
201                 super.endElement(uri, localName, qName);
202         }
203
204         /**
205          * @return The Xml handler used for the parsing
206          */
207         public XmlHandler getHandler()
208         {
209                 return _handler;
210         }
211 }