X-Git-Url: https://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Fload%2Fxml%2FXmlFileLoader.java;h=5af471a2119e2e839589ec1af042d479d75f6016;hp=783ac798ed3d0bf9a6a580cb1ee33b4cc1ebfb18;hb=326f489e36aa7f235bc19409a57bf4955cd50f24;hpb=8c8868ae29b3252f02e094c02307384cf61ba667 diff --git a/tim/prune/load/xml/XmlFileLoader.java b/tim/prune/load/xml/XmlFileLoader.java index 783ac79..5af471a 100644 --- a/tim/prune/load/xml/XmlFileLoader.java +++ b/tim/prune/load/xml/XmlFileLoader.java @@ -2,14 +2,19 @@ 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; @@ -66,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) { @@ -90,18 +101,47 @@ public class XmlFileLoader extends DefaultHandler implements Runnable 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 { - // Show error dialog - _app.showErrorMessageNoLookup("error.load.dialogtitle", - I18nManager.getText("error.load.othererror") + " " + e.getMessage()); + XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser"); + xmlReader.setContentHandler(this); + xmlReader.parse(new InputSource(inStream)); + success = true; // worked } - finally { - try {inStream.close();} catch (IOException e2) {} + 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) + { + 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()); + } } + return success; } - /** * Receive a tag * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)