1 package tim.prune.load.xml;
4 import java.io.FileInputStream;
5 import java.io.IOException;
7 import javax.xml.parsers.SAXParser;
8 import javax.xml.parsers.SAXParserFactory;
10 import org.xml.sax.Attributes;
11 import org.xml.sax.SAXException;
12 import org.xml.sax.helpers.DefaultHandler;
14 import tim.prune.I18nManager;
15 import tim.prune.data.Altitude;
16 import tim.prune.data.SourceInfo;
19 * Class for handling loading of Xml files, and passing the
20 * loaded data back to the App object
22 public class XmlFileLoader extends DefaultHandler implements Runnable
24 private File _file = null;
25 private App _app = null;
26 private XmlHandler _handler = null;
27 private String _unknownType = null;
32 * @param inApp Application object to inform of track load
34 public XmlFileLoader(App inApp)
40 * Reset the handler to ensure data cleared
49 * Open the selected file
50 * @param inFile File to open
52 public void openFile(File inFile)
56 // start new thread in case xml parsing is time-consuming
57 new Thread(this).start();
62 * Run method, to parse the file
63 * @see java.lang.Runnable#run()
67 FileInputStream inStream = null;
70 // Construct a SAXParser and use this as a default handler
71 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
72 inStream = new FileInputStream(_file);
73 saxParser.parse(inStream, this);
75 // Check whether handler was properly instantiated
78 // Wasn't either kml or gpx
79 _app.showErrorMessageNoLookup("error.load.dialogtitle",
80 I18nManager.getText("error.load.unknownxml") + " " + _unknownType);
84 // Pass information back to app
85 SourceInfo sourceInfo = new SourceInfo(_file,
86 (_handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML));
87 _app.informDataLoaded(_handler.getFieldArray(), _handler.getDataArray(),
88 Altitude.Format.METRES, sourceInfo, _handler.getTrackNameList(), _handler.getLinkArray());
94 _app.showErrorMessageNoLookup("error.load.dialogtitle",
95 I18nManager.getText("error.load.othererror") + " " + e.getMessage());
98 try {inStream.close();} catch (IOException e2) {}
105 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
107 public void startElement(String uri, String localName, String qName,
108 Attributes attributes) throws SAXException
110 // Check for "kml" or "gpx" tags
111 if (_handler == null)
113 if (qName.equals("kml")) {_handler = new KmlHandler();}
114 else if (qName.equals("gpx")) {_handler = new GpxHandler();}
115 else if (_unknownType == null && !qName.equals(""))
117 _unknownType = qName;
122 // Handler instantiated so pass tags on to it
123 _handler.startElement(uri, localName, qName, attributes);
125 super.startElement(uri, localName, qName, attributes);
130 * Receive characters, either between or inside tags
131 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
133 public void characters(char[] ch, int start, int length)
136 if (_handler != null)
138 // Handler instantiated so pass tags on to it
139 _handler.characters(ch, start, length);
141 super.characters(ch, start, length);
146 * Receive end of element
147 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
149 public void endElement(String uri, String localName, String qName)
152 if (_handler != null)
154 // Handler instantiated so pass tags on to it
155 _handler.endElement(uri, localName, qName);
157 super.endElement(uri, localName, qName);
161 * @return The Xml handler used for the parsing
163 public XmlHandler getHandler()