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 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());
100 // Pass information back to app
101 _app.informDataLoaded(_handler.getFieldArray(), _handler.getDataArray(),
102 null, sourceInfo, _handler.getTrackNameList(),
103 new MediaLinkInfo(_handler.getLinkArray()));
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
114 public boolean parseXmlStream(InputStream inStream)
116 boolean success = false;
117 // Firstly, try to use xerces to parse the xml (will throw an exception if not available)
120 XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
121 xmlReader.setContentHandler(this);
122 xmlReader.parse(new InputSource(inStream));
123 success = true; // worked
125 catch (Exception e) {} // don't care too much if it didn't work, there's a backup
127 // If that didn't work, try the built-in classes (which work for xml1.0 but handling for 1.1 contains bugs)
132 // Construct a SAXParser and use this as a default handler
133 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
134 saxParser.parse(inStream, this);
140 _app.showErrorMessageNoLookup("error.load.dialogtitle",
141 I18nManager.getText("error.load.othererror") + " " + e.getMessage());
149 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
151 public void startElement(String uri, String localName, String qName,
152 Attributes attributes) throws SAXException
154 // Check for "kml" or "gpx" tags
155 if (_handler == null)
157 if (qName.equals("kml")) {_handler = new KmlHandler();}
158 else if (qName.equals("gpx")) {_handler = new GpxHandler();}
159 else if (_unknownType == null && !qName.equals(""))
161 _unknownType = qName;
166 // Handler instantiated so pass tags on to it
167 _handler.startElement(uri, localName, qName, attributes);
169 super.startElement(uri, localName, qName, attributes);
174 * Receive characters, either between or inside tags
175 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
177 public void characters(char[] ch, int start, int length)
180 if (_handler != null)
182 // Handler instantiated so pass tags on to it
183 _handler.characters(ch, start, length);
185 super.characters(ch, start, length);
190 * Receive end of element
191 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
193 public void endElement(String uri, String localName, String qName)
196 if (_handler != null)
198 // Handler instantiated so pass tags on to it
199 _handler.endElement(uri, localName, qName);
201 super.endElement(uri, localName, qName);
205 * @return The Xml handler used for the parsing
207 public XmlHandler getHandler()