X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fload%2Fxml%2FXmlFileLoader.java;h=454149f2c35cd432761edde2c8d271c50d2b3b15;hb=f35b6d628f68e3b5ef19965ad8988d0dd1eb8efa;hp=a607b22a2803b1b0cc09708ab6b6ef0390443681;hpb=5625a1abadb5f2ca5f017fe7dbda1d5141cb637b;p=GpsPrune.git diff --git a/tim/prune/load/xml/XmlFileLoader.java b/tim/prune/load/xml/XmlFileLoader.java index a607b22..454149f 100644 --- a/tim/prune/load/xml/XmlFileLoader.java +++ b/tim/prune/load/xml/XmlFileLoader.java @@ -1,8 +1,9 @@ package tim.prune.load.xml; import java.io.File; -import javax.swing.JFrame; -import javax.swing.JOptionPane; +import java.io.FileInputStream; +import java.io.IOException; + import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; @@ -12,6 +13,7 @@ import org.xml.sax.helpers.DefaultHandler; import tim.prune.App; import tim.prune.I18nManager; import tim.prune.data.Altitude; +import tim.prune.data.SourceInfo; /** * Class for handling loading of Xml files, and passing the @@ -21,7 +23,6 @@ public class XmlFileLoader extends DefaultHandler implements Runnable { private File _file = null; private App _app = null; - private JFrame _parentFrame = null; private XmlHandler _handler = null; private String _unknownType = null; @@ -29,24 +30,29 @@ public class XmlFileLoader extends DefaultHandler implements Runnable /** * Constructor * @param inApp Application object to inform of track load - * @param inParentFrame parent frame to reference for dialogs */ - public XmlFileLoader(App inApp, JFrame inParentFrame) + public XmlFileLoader(App inApp) { _app = inApp; - _parentFrame = inParentFrame; } + /** + * Reset the handler to ensure data cleared + */ + public void reset() + { + _handler = null; + _unknownType = null; + } /** - * Open the selected file and show the GUI dialog - * to select load options + * Open the selected file + * @param inFile File to open */ public void openFile(File inFile) { _file = inFile; - _handler = null; - _unknownType = null; + reset(); // start new thread in case xml parsing is time-consuming new Thread(this).start(); } @@ -58,33 +64,38 @@ public class XmlFileLoader extends DefaultHandler implements Runnable */ public void run() { + FileInputStream inStream = null; try { // Construct a SAXParser and use this as a default handler SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); - saxParser.parse(_file, this); + inStream = new FileInputStream(_file); + saxParser.parse(inStream, this); // Check whether handler was properly instantiated if (_handler == null) { // Wasn't either kml or gpx - JOptionPane.showMessageDialog(_parentFrame, - I18nManager.getText("error.load.unknownxml") + " " + _unknownType, - I18nManager.getText("error.load.dialogtitle"), JOptionPane.ERROR_MESSAGE); + _app.showErrorMessageNoLookup("error.load.dialogtitle", + I18nManager.getText("error.load.unknownxml") + " " + _unknownType); } else { // Pass information back to app + SourceInfo sourceInfo = new SourceInfo(_file, + (_handler instanceof GpxHandler?SourceInfo.FILE_TYPE.GPX:SourceInfo.FILE_TYPE.KML)); _app.informDataLoaded(_handler.getFieldArray(), _handler.getDataArray(), - Altitude.FORMAT_METRES, _file.getName()); + Altitude.Format.METRES, sourceInfo, _handler.getTrackNameList(), _handler.getLinkArray()); } } catch (Exception e) { // Show error dialog - JOptionPane.showMessageDialog(_parentFrame, - I18nManager.getText("error.load.othererror") + " " + e.getMessage(), - I18nManager.getText("error.load.dialogtitle"), JOptionPane.ERROR_MESSAGE); + _app.showErrorMessageNoLookup("error.load.dialogtitle", + I18nManager.getText("error.load.othererror") + " " + e.getMessage()); + } + finally { + try {inStream.close();} catch (IOException e2) {} } } @@ -94,14 +105,14 @@ public class XmlFileLoader extends DefaultHandler implements Runnable * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes) */ public void startElement(String uri, String localName, String qName, - Attributes attributes) throws SAXException + Attributes attributes) throws SAXException { // Check for "kml" or "gpx" tags if (_handler == null) { if (qName.equals("kml")) {_handler = new KmlHandler();} else if (qName.equals("gpx")) {_handler = new GpxHandler();} - else if (_unknownType == null && qName != null && !qName.equals("")) + else if (_unknownType == null && !qName.equals("")) { _unknownType = qName; } @@ -120,7 +131,7 @@ public class XmlFileLoader extends DefaultHandler implements Runnable * @see org.xml.sax.ContentHandler#characters(char[], int, int) */ public void characters(char[] ch, int start, int length) - throws SAXException + throws SAXException { if (_handler != null) { @@ -136,7 +147,7 @@ public class XmlFileLoader extends DefaultHandler implements Runnable * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String) */ public void endElement(String uri, String localName, String qName) - throws SAXException + throws SAXException { if (_handler != null) { @@ -145,4 +156,12 @@ public class XmlFileLoader extends DefaultHandler implements Runnable } super.endElement(uri, localName, qName); } + + /** + * @return The Xml handler used for the parsing + */ + public XmlHandler getHandler() + { + return _handler; + } }