]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/xml/XmlFileLoader.java
Version 16, February 2014
[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                                 // Pass information back to app
97                                 SourceInfo sourceInfo = new SourceInfo(_file,
98                                         (_handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML));
99                                 _app.informDataLoaded(_handler.getFieldArray(), _handler.getDataArray(),
100                                         null, sourceInfo, _handler.getTrackNameList(),
101                                         new MediaLinkInfo(_handler.getLinkArray()));
102                         }
103                 }
104         }
105
106
107         /**
108          * Try both Xerces and the built-in java classes to parse the given xml stream
109          * @param inStream input stream from file / zip / gzip
110          * @return true on success, false if both xerces and built-in parser failed
111          */
112         public boolean parseXmlStream(InputStream inStream)
113         {
114                 boolean success = false;
115                 // Firstly, try to use xerces to parse the xml (will throw an exception if not available)
116                 try
117                 {
118                         XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
119                         xmlReader.setContentHandler(this);
120                         xmlReader.parse(new InputSource(inStream));
121                         success = true; // worked
122                 }
123                 catch (Exception e) {} // don't care too much if it didn't work, there's a backup
124
125                 // If that didn't work, try the built-in classes (which work for xml1.0 but handling for 1.1 contains bugs)
126                 if (!success)
127                 {
128                         try
129                         {
130                                 // Construct a SAXParser and use this as a default handler
131                                 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
132                                 saxParser.parse(inStream, this);
133                                 success = true;
134                         }
135                         catch (Exception e)
136                         {
137                                 // Show error dialog
138                                 _app.showErrorMessageNoLookup("error.load.dialogtitle",
139                                         I18nManager.getText("error.load.othererror") + " " + e.getMessage());
140                         }
141                 }
142                 return success;
143         }
144
145         /**
146          * Receive a tag
147          * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
148          */
149         public void startElement(String uri, String localName, String qName,
150                 Attributes attributes) throws SAXException
151         {
152                 // Check for "kml" or "gpx" tags
153                 if (_handler == null)
154                 {
155                         if (qName.equals("kml")) {_handler = new KmlHandler();}
156                         else if (qName.equals("gpx")) {_handler = new GpxHandler();}
157                         else if (_unknownType == null && !qName.equals(""))
158                         {
159                                 _unknownType = qName;
160                         }
161                 }
162                 else
163                 {
164                         // Handler instantiated so pass tags on to it
165                         _handler.startElement(uri, localName, qName, attributes);
166                 }
167                 super.startElement(uri, localName, qName, attributes);
168         }
169
170
171         /**
172          * Receive characters, either between or inside tags
173          * @see org.xml.sax.ContentHandler#characters(char[], int, int)
174          */
175         public void characters(char[] ch, int start, int length)
176                 throws SAXException
177         {
178                 if (_handler != null)
179                 {
180                         // Handler instantiated so pass tags on to it
181                         _handler.characters(ch, start, length);
182                 }
183                 super.characters(ch, start, length);
184         }
185
186
187         /**
188          * Receive end of element
189          * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
190          */
191         public void endElement(String uri, String localName, String qName)
192                 throws SAXException
193         {
194                 if (_handler != null)
195                 {
196                         // Handler instantiated so pass tags on to it
197                         _handler.endElement(uri, localName, qName);
198                 }
199                 super.endElement(uri, localName, qName);
200         }
201
202         /**
203          * @return The Xml handler used for the parsing
204          */
205         public XmlHandler getHandler()
206         {
207                 return _handler;
208         }
209 }