1 package tim.prune.load.xml;
4 import java.io.FileInputStream;
6 import javax.xml.parsers.SAXParser;
7 import javax.xml.parsers.SAXParserFactory;
9 import org.xml.sax.Attributes;
10 import org.xml.sax.SAXException;
11 import org.xml.sax.helpers.DefaultHandler;
13 import tim.prune.I18nManager;
14 import tim.prune.data.Altitude;
15 import tim.prune.data.SourceInfo;
18 * Class for handling loading of Xml files, and passing the
19 * loaded data back to the App object
21 public class XmlFileLoader extends DefaultHandler implements Runnable
23 private File _file = null;
24 private App _app = null;
25 private XmlHandler _handler = null;
26 private String _unknownType = null;
31 * @param inApp Application object to inform of track load
33 public XmlFileLoader(App inApp)
39 * Reset the handler to ensure data cleared
48 * Open the selected file
49 * @param inFile File to open
51 public void openFile(File inFile)
55 // start new thread in case xml parsing is time-consuming
56 new Thread(this).start();
61 * Run method, to parse the file
62 * @see java.lang.Runnable#run()
66 FileInputStream inStream = null;
69 // Construct a SAXParser and use this as a default handler
70 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
71 inStream = new FileInputStream(_file);
72 saxParser.parse(inStream, this);
74 // Check whether handler was properly instantiated
77 // Wasn't either kml or gpx
78 _app.showErrorMessageNoLookup("error.load.dialogtitle",
79 I18nManager.getText("error.load.unknownxml") + " " + _unknownType);
83 // Pass information back to app
84 SourceInfo sourceInfo = new SourceInfo(_file,
85 (_handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML));
86 _app.informDataLoaded(_handler.getFieldArray(), _handler.getDataArray(),
87 Altitude.Format.METRES, sourceInfo, _handler.getTrackNameList());
92 // clean up file stream
96 catch (Exception e2) {}
98 _app.showErrorMessageNoLookup("error.load.dialogtitle",
99 I18nManager.getText("error.load.othererror") + " " + e.getMessage());
106 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
108 public void startElement(String uri, String localName, String qName,
109 Attributes attributes) throws SAXException
111 // Check for "kml" or "gpx" tags
112 if (_handler == null)
114 if (qName.equals("kml")) {_handler = new KmlHandler();}
115 else if (qName.equals("gpx")) {_handler = new GpxHandler();}
116 else if (_unknownType == null && !qName.equals(""))
118 _unknownType = qName;
123 // Handler instantiated so pass tags on to it
124 _handler.startElement(uri, localName, qName, attributes);
126 super.startElement(uri, localName, qName, attributes);
131 * Receive characters, either between or inside tags
132 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
134 public void characters(char[] ch, int start, int length)
137 if (_handler != null)
139 // Handler instantiated so pass tags on to it
140 _handler.characters(ch, start, length);
142 super.characters(ch, start, length);
147 * Receive end of element
148 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
150 public void endElement(String uri, String localName, String qName)
153 if (_handler != null)
155 // Handler instantiated so pass tags on to it
156 _handler.endElement(uri, localName, qName);
158 super.endElement(uri, localName, qName);
162 * @return The Xml handler used for the parsing
164 public XmlHandler getHandler()