]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/load/xml/XmlFileLoader.java
Version 19.2, December 2018
[GpsPrune.git] / tim / prune / load / xml / XmlFileLoader.java
index 8801d00e08f6db1083a7617b9e9f74106f0e0f69..c907d373ded1a80a3f8b30783bc5298673302dcf 100644 (file)
@@ -1,17 +1,25 @@
 package tim.prune.load.xml;
 
 import java.io.File;
-import javax.swing.JFrame;
-import javax.swing.JOptionPane;
+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
@@ -21,7 +29,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 +36,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,36 +70,79 @@ 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();
-                       saxParser.parse(_file, this);
+                       inStream = new FileInputStream(_file);
+                       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)
                        {
                                // 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
                        {
+                               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
                                _app.informDataLoaded(_handler.getFieldArray(), _handler.getDataArray(),
-                                       Altitude.FORMAT_METRES, _file.getName());
+                                       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
                {
-                       // Show error dialog
-                       JOptionPane.showMessageDialog(_parentFrame,
-                               I18nManager.getText("error.load.othererror") + " " + e.getMessage(),
-                               I18nManager.getText("error.load.dialogtitle"), JOptionPane.ERROR_MESSAGE);
+                       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)
+               {
+                       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
@@ -101,7 +156,7 @@ public class XmlFileLoader extends DefaultHandler implements Runnable
                {
                        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;
                        }