1 package tim.prune.load.xml;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
9 import javax.xml.parsers.SAXParser;
10 import javax.xml.parsers.SAXParserFactory;
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;
20 import tim.prune.I18nManager;
21 import tim.prune.data.SourceInfo;
22 import tim.prune.load.MediaLinkInfo;
25 * Class for handling loading of Xml files, and passing the
26 * loaded data back to the App object
28 public class XmlFileLoader extends DefaultHandler implements Runnable
30 private File _file = null;
31 private App _app = null;
32 private XmlHandler _handler = null;
33 private String _unknownType = null;
38 * @param inApp Application object to inform of track load
40 public XmlFileLoader(App inApp)
46 * Reset the handler to ensure data cleared
55 * Open the selected file
56 * @param inFile File to open
58 public void openFile(File inFile)
62 // start new thread in case xml parsing is time-consuming
63 new Thread(this).start();
68 * Run method, to parse the file
69 * @see java.lang.Runnable#run()
73 FileInputStream inStream = null;
74 boolean success = false;
77 inStream = new FileInputStream(_file);
78 success = parseXmlStream(inStream);
80 catch (FileNotFoundException fnfe) {}
82 // Clean up the stream, don't need it any more
83 try {inStream.close();} catch (IOException e2) {}
87 // Check whether handler was properly instantiated
90 // Wasn't either kml or gpx
91 _app.showErrorMessageNoLookup("error.load.dialogtitle",
92 I18nManager.getText("error.load.unknownxml") + " " + _unknownType);
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()));
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
112 public boolean parseXmlStream(InputStream inStream)
114 boolean success = false;
115 // Firstly, try to use xerces to parse the xml (will throw an exception if not available)
118 XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
119 xmlReader.setContentHandler(this);
120 xmlReader.parse(new InputSource(inStream));
121 success = true; // worked
123 catch (Exception e) {} // don't care too much if it didn't work, there's a backup
125 // If that didn't work, try the built-in classes (which work for xml1.0 but handling for 1.1 contains bugs)
130 // Construct a SAXParser and use this as a default handler
131 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
132 saxParser.parse(inStream, this);
138 _app.showErrorMessageNoLookup("error.load.dialogtitle",
139 I18nManager.getText("error.load.othererror") + " " + e.getMessage());
147 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
149 public void startElement(String uri, String localName, String qName,
150 Attributes attributes) throws SAXException
152 // Check for "kml" or "gpx" tags
153 if (_handler == null)
155 if (qName.equals("kml")) {_handler = new KmlHandler();}
156 else if (qName.equals("gpx")) {_handler = new GpxHandler();}
157 else if (_unknownType == null && !qName.equals(""))
159 _unknownType = qName;
164 // Handler instantiated so pass tags on to it
165 _handler.startElement(uri, localName, qName, attributes);
167 super.startElement(uri, localName, qName, attributes);
172 * Receive characters, either between or inside tags
173 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
175 public void characters(char[] ch, int start, int length)
178 if (_handler != null)
180 // Handler instantiated so pass tags on to it
181 _handler.characters(ch, start, length);
183 super.characters(ch, start, length);
188 * Receive end of element
189 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
191 public void endElement(String uri, String localName, String qName)
194 if (_handler != null)
196 // Handler instantiated so pass tags on to it
197 _handler.endElement(uri, localName, qName);
199 super.endElement(uri, localName, qName);
203 * @return The Xml handler used for the parsing
205 public XmlHandler getHandler()