X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Fload%2Fxml%2FXmlFileLoader.java;h=c907d373ded1a80a3f8b30783bc5298673302dcf;hb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465;hp=d528420029a325f8cfc33238de6c80b5c55ef8cc;hpb=6814c830d470f73c7ec57c71235de333f5ea4279;p=GpsPrune.git diff --git a/tim/prune/load/xml/XmlFileLoader.java b/tim/prune/load/xml/XmlFileLoader.java index d528420..c907d37 100644 --- a/tim/prune/load/xml/XmlFileLoader.java +++ b/tim/prune/load/xml/XmlFileLoader.java @@ -2,17 +2,24 @@ package tim.prune.load.xml; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; +import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; +import org.xml.sax.helpers.XMLReaderFactory; + import tim.prune.App; import tim.prune.I18nManager; -import tim.prune.data.Altitude; import tim.prune.data.SourceInfo; +import tim.prune.load.MediaLinkInfo; /** * Class for handling loading of Xml files, and passing the @@ -64,13 +71,19 @@ public class XmlFileLoader extends DefaultHandler implements Runnable public void run() { FileInputStream inStream = null; + boolean success = false; try { - // Construct a SAXParser and use this as a default handler - SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); inStream = new FileInputStream(_file); - saxParser.parse(inStream, this); + success = parseXmlStream(inStream); + } + catch (FileNotFoundException fnfe) {} + + // Clean up the stream, don't need it any more + try {inStream.close();} catch (IOException e2) {} + if (success) + { // Check whether handler was properly instantiated if (_handler == null) { @@ -80,27 +93,57 @@ public class XmlFileLoader extends DefaultHandler implements Runnable } else { + SourceInfo.FILE_TYPE sourceType = (_handler instanceof GpxHandler ? SourceInfo.FILE_TYPE.GPX : SourceInfo.FILE_TYPE.KML); + SourceInfo sourceInfo = new SourceInfo(_file, sourceType); + sourceInfo.setFileTitle(_handler.getFileTitle()); + // 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, sourceInfo, _handler.getTrackNameList()); + null, sourceInfo, _handler.getTrackNameList(), + new MediaLinkInfo(_handler.getLinkArray())); } } - catch (Exception e) + } + + + /** + * Try both Xerces and the built-in java classes to parse the given xml stream + * @param inStream input stream from file / zip / gzip + * @return true on success, false if both xerces and built-in parser failed + */ + public boolean parseXmlStream(InputStream inStream) + { + boolean success = false; + // Firstly, try to use xerces to parse the xml (will throw an exception if not available) + try + { + XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); + xmlReader.setContentHandler(this); + xmlReader.parse(new InputSource(inStream)); + success = true; // worked + } + catch (Exception e) {} // don't care too much if it didn't work, there's a backup + + // If that didn't work, try the built-in classes (which work for xml1.0 but handling for 1.1 contains bugs) + if (!success) { - // clean up file stream - try { - inStream.close(); + try + { + // Construct a SAXParser and use this as a default handler + SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); + saxParser.parse(inStream, this); + success = true; + } + catch (Exception e) + { + // Show error dialog + _app.showErrorMessageNoLookup("error.load.dialogtitle", + I18nManager.getText("error.load.othererror") + " " + e.getMessage()); } - catch (Exception e2) {} - // Show error dialog - _app.showErrorMessageNoLookup("error.load.dialogtitle", - I18nManager.getText("error.load.othererror") + " " + e.getMessage()); } + return success; } - /** * Receive a tag * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)