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;
13 import tim.prune.data.SourceInfo;
16 * Class for handling loading of Xml files, and passing the
17 * loaded data back to the App object
19 public class XmlFileLoader extends DefaultHandler implements Runnable
21 private File _file = null;
22 private App _app = null;
23 private XmlHandler _handler = null;
24 private String _unknownType = null;
29 * @param inApp Application object to inform of track load
31 public XmlFileLoader(App inApp)
37 * Reset the handler to ensure data cleared
46 * Open the selected file
47 * @param inFile File to open
49 public void openFile(File inFile)
53 // start new thread in case xml parsing is time-consuming
54 new Thread(this).start();
59 * Run method, to parse the file
60 * @see java.lang.Runnable#run()
66 // Construct a SAXParser and use this as a default handler
67 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
68 saxParser.parse(_file, this);
70 // Check whether handler was properly instantiated
73 // Wasn't either kml or gpx
74 _app.showErrorMessageNoLookup("error.load.dialogtitle",
75 I18nManager.getText("error.load.unknownxml") + " " + _unknownType);
79 // Pass information back to app
80 SourceInfo sourceInfo = new SourceInfo(_file,
81 (_handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML));
82 _app.informDataLoaded(_handler.getFieldArray(), _handler.getDataArray(),
83 Altitude.Format.METRES, sourceInfo);
89 _app.showErrorMessageNoLookup("error.load.dialogtitle",
90 I18nManager.getText("error.load.othererror") + " " + e.getMessage());
97 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
99 public void startElement(String uri, String localName, String qName,
100 Attributes attributes) throws SAXException
102 // Check for "kml" or "gpx" tags
103 if (_handler == null)
105 if (qName.equals("kml")) {_handler = new KmlHandler();}
106 else if (qName.equals("gpx")) {_handler = new GpxHandler();}
107 else if (_unknownType == null && !qName.equals(""))
109 _unknownType = qName;
114 // Handler instantiated so pass tags on to it
115 _handler.startElement(uri, localName, qName, attributes);
117 super.startElement(uri, localName, qName, attributes);
122 * Receive characters, either between or inside tags
123 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
125 public void characters(char[] ch, int start, int length)
128 if (_handler != null)
130 // Handler instantiated so pass tags on to it
131 _handler.characters(ch, start, length);
133 super.characters(ch, start, length);
138 * Receive end of element
139 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
141 public void endElement(String uri, String localName, String qName)
144 if (_handler != null)
146 // Handler instantiated so pass tags on to it
147 _handler.endElement(uri, localName, qName);
149 super.endElement(uri, localName, qName);
153 * @return The Xml handler used for the parsing
155 public XmlHandler getHandler()