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;
17 import tim.prune.load.MediaLinkInfo;
20 * Class for handling loading of Xml files, and passing the
21 * loaded data back to the App object
23 public class XmlFileLoader extends DefaultHandler implements Runnable
25 private File _file = null;
26 private App _app = null;
27 private XmlHandler _handler = null;
28 private String _unknownType = null;
33 * @param inApp Application object to inform of track load
35 public XmlFileLoader(App inApp)
41 * Reset the handler to ensure data cleared
50 * Open the selected file
51 * @param inFile File to open
53 public void openFile(File inFile)
57 // start new thread in case xml parsing is time-consuming
58 new Thread(this).start();
63 * Run method, to parse the file
64 * @see java.lang.Runnable#run()
68 FileInputStream inStream = null;
71 // Construct a SAXParser and use this as a default handler
72 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
73 inStream = new FileInputStream(_file);
74 saxParser.parse(inStream, this);
76 // Check whether handler was properly instantiated
79 // Wasn't either kml or gpx
80 _app.showErrorMessageNoLookup("error.load.dialogtitle",
81 I18nManager.getText("error.load.unknownxml") + " " + _unknownType);
85 // Pass information back to app
86 SourceInfo sourceInfo = new SourceInfo(_file,
87 (_handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML));
88 _app.informDataLoaded(_handler.getFieldArray(), _handler.getDataArray(),
89 Altitude.Format.METRES, sourceInfo, _handler.getTrackNameList(),
90 new MediaLinkInfo(_handler.getLinkArray()));
96 _app.showErrorMessageNoLookup("error.load.dialogtitle",
97 I18nManager.getText("error.load.othererror") + " " + e.getMessage());
100 try {inStream.close();} catch (IOException e2) {}
107 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
109 public void startElement(String uri, String localName, String qName,
110 Attributes attributes) throws SAXException
112 // Check for "kml" or "gpx" tags
113 if (_handler == null)
115 if (qName.equals("kml")) {_handler = new KmlHandler();}
116 else if (qName.equals("gpx")) {_handler = new GpxHandler();}
117 else if (_unknownType == null && !qName.equals(""))
119 _unknownType = qName;
124 // Handler instantiated so pass tags on to it
125 _handler.startElement(uri, localName, qName, attributes);
127 super.startElement(uri, localName, qName, attributes);
132 * Receive characters, either between or inside tags
133 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
135 public void characters(char[] ch, int start, int length)
138 if (_handler != null)
140 // Handler instantiated so pass tags on to it
141 _handler.characters(ch, start, length);
143 super.characters(ch, start, length);
148 * Receive end of element
149 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
151 public void endElement(String uri, String localName, String qName)
154 if (_handler != null)
156 // Handler instantiated so pass tags on to it
157 _handler.endElement(uri, localName, qName);
159 super.endElement(uri, localName, qName);
163 * @return The Xml handler used for the parsing
165 public XmlHandler getHandler()