1 package tim.prune.load.xml;
4 import javax.xml.parsers.SAXParser;
5 import javax.xml.parsers.SAXParserFactory;
7 import org.xml.sax.Attributes;
8 import org.xml.sax.SAXException;
9 import org.xml.sax.helpers.DefaultHandler;
11 import tim.prune.I18nManager;
12 import tim.prune.data.Altitude;
15 * Class for handling loading of Xml files, and passing the
16 * loaded data back to the App object
18 public class XmlFileLoader extends DefaultHandler implements Runnable
20 private File _file = null;
21 private App _app = null;
22 private XmlHandler _handler = null;
23 private String _unknownType = null;
28 * @param inApp Application object to inform of track load
30 public XmlFileLoader(App inApp)
36 * Reset the handler to ensure data cleared
45 * Open the selected file
46 * @param inFile File to open
48 public void openFile(File inFile)
52 // start new thread in case xml parsing is time-consuming
53 new Thread(this).start();
58 * Run method, to parse the file
59 * @see java.lang.Runnable#run()
65 // Construct a SAXParser and use this as a default handler
66 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
67 saxParser.parse(_file, this);
69 // Check whether handler was properly instantiated
72 // Wasn't either kml or gpx
73 _app.showErrorMessageNoLookup("error.load.dialogtitle",
74 I18nManager.getText("error.load.unknownxml") + " " + _unknownType);
78 // Pass information back to app
79 _app.informDataLoaded(_handler.getFieldArray(), _handler.getDataArray(),
80 Altitude.Format.METRES, _file.getName());
86 _app.showErrorMessageNoLookup("error.load.dialogtitle",
87 I18nManager.getText("error.load.othererror") + " " + e.getMessage());
94 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
96 public void startElement(String uri, String localName, String qName,
97 Attributes attributes) throws SAXException
99 // Check for "kml" or "gpx" tags
100 if (_handler == null)
102 if (qName.equals("kml")) {_handler = new KmlHandler();}
103 else if (qName.equals("gpx")) {_handler = new GpxHandler();}
104 else if (_unknownType == null && !qName.equals(""))
106 _unknownType = qName;
111 // Handler instantiated so pass tags on to it
112 _handler.startElement(uri, localName, qName, attributes);
114 super.startElement(uri, localName, qName, attributes);
119 * Receive characters, either between or inside tags
120 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
122 public void characters(char[] ch, int start, int length)
125 if (_handler != null)
127 // Handler instantiated so pass tags on to it
128 _handler.characters(ch, start, length);
130 super.characters(ch, start, length);
135 * Receive end of element
136 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
138 public void endElement(String uri, String localName, String qName)
141 if (_handler != null)
143 // Handler instantiated so pass tags on to it
144 _handler.endElement(uri, localName, qName);
146 super.endElement(uri, localName, qName);
150 * @return The Xml handler used for the parsing
152 public XmlHandler getHandler()