Changeset 2795 in josm for trunk/src/org


Ignore:
Timestamp:
2010-01-09T21:16:03+01:00 (14 years ago)
Author:
jttt
Message:

Allow to load incomplete gpx files

Location:
trunk/src/org/openstreetmap/josm
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/gpx/GpxData.java

    r2247 r2795  
    7979    }
    8080
     81    public boolean isEmpty() {
     82        return !hasRoutePoints() && !hasTrackPoints() && waypoints.isEmpty();
     83    }
     84
    8185    // FIXME might perhaps use visitor pattern?
    8286    public Bounds recalculateBounds() {
  • trunk/src/org/openstreetmap/josm/gui/layer/geoimage/CorrelateGpxWithImages.java

    r2786 r2795  
    179179                        iStream = new FileInputStream(sel);
    180180                    }
    181                     data = new GpxReader(iStream, sel).data;
     181                    GpxReader reader = new GpxReader(iStream);
     182                    reader.parse(false);
     183                    data = reader.data;
    182184                    data.storageFile = sel;
    183185
     
    874876
    875877                    tfTimezone.setText(formatTimezone(timezone));
    876                     tfOffset.setText(Long.toString(delta + dayOffset*24*60*60l));    // add the day offset to the offset field
     878                    tfOffset.setText(Long.toString(delta + 24*60*60L*dayOffset));    // add the day offset to the offset field
    877879
    878880                    tfTimezone.getDocument().addDocumentListener(statusBarListener);
  • trunk/src/org/openstreetmap/josm/io/BoundingBoxDownloader.java

    r2676 r2795  
    5151                }
    5252                progressMonitor.setTicks(0);
    53                 GpxData currentGpx = new GpxReader(in, null).data;
     53                GpxReader reader = new GpxReader(in);
     54                reader.parse(false);
     55                GpxData currentGpx = reader.data;
    5456                if (result == null) {
    5557                    result = currentGpx;
  • trunk/src/org/openstreetmap/josm/io/GpxImporter.java

    r2235 r2795  
    1111import java.util.zip.GZIPInputStream;
    1212
     13import javax.swing.JOptionPane;
    1314import javax.swing.SwingUtilities;
    1415
     
    4546                }
    4647            }
    47             final GpxReader r = new GpxReader(is, file.getAbsoluteFile().getParentFile());
     48            final GpxReader r = new GpxReader(is);
     49            final boolean parsedProperly = r.parse(true);
    4850            r.data.storageFile = file;
    4951            final GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
     
    6163                            Main.main.addLayer(ml);
    6264                        }
     65                    }
     66                    if (!parsedProperly) {
     67                        JOptionPane.showMessageDialog(null, tr("Error occured while parsing gpx file {0}. Only part of the file will be available", file.getName()));
    6368                    }
    6469                }
  • trunk/src/org/openstreetmap/josm/io/GpxReader.java

    r2789 r2795  
    66import static org.openstreetmap.josm.tools.I18n.tr;
    77
    8 import java.io.File;
    98import java.io.IOException;
    109import java.io.InputStream;
     
    1312import java.util.Collection;
    1413import java.util.LinkedList;
     14import java.util.List;
    1515import java.util.Map;
    1616import java.util.Stack;
     
    4141     */
    4242    public GpxData data;
    43     public enum State { init, metadata, wpt, rte, trk, ext, author, link, trkseg, copyright}
     43    private enum State { init, metadata, wpt, rte, trk, ext, author, link, trkseg, copyright}
     44    private InputSource inputSource;
    4445
    4546    private class Parser extends DefaultHandler {
     
    5556        private GpxLink currentLink;
    5657        private Stack<State> states;
     58        private final Stack<String> elements = new Stack<String>();
    5759
    5860        private StringBuffer accumulator = new StringBuffer();
     
    8183
    8284        @Override public void startElement(String namespaceURI, String qName, String rqName, Attributes atts) throws SAXException {
     85            elements.push(qName);
    8386            switch(currentState) {
    8487            case init:
     
    211214        @SuppressWarnings("unchecked")
    212215        @Override public void endElement(String namespaceURI, String qName, String rqName) {
     216            elements.pop();
    213217            switch (currentState) {
    214218            case metadata:
     
    333337            data = currentData;
    334338        }
     339
     340        public void tryToFinish() throws SAXException {
     341            List<String> remainingElements = new ArrayList<String>(elements);
     342            for (int i=remainingElements.size() - 1; i >= 0; i--) {
     343                endElement(null, remainingElements.get(i), null);
     344            }
     345            endDocument();
     346        }
    335347    }
    336348
     
    338350     * Parse the input stream and store the result in trackData and markerData
    339351     *
    340      * @param relativeMarkerPath The directory to use as relative path for all &lt;wpt&gt;
    341      *    marker tags. Maybe <code>null</code>, in which case no relative urls are constructed for the markers.
    342352     */
    343     public GpxReader(InputStream source, File relativeMarkerPath) throws SAXException, IOException {
    344 
     353    public GpxReader(InputStream source) throws IOException {
     354        this.inputSource = new InputSource(new InputStreamReader(source, "UTF-8"));
     355    }
     356
     357    /**
     358     *
     359     * @return True if file was properly parsed, false if there was error during parsing but some data were parsed anyway
     360     * @throws SAXException
     361     * @throws IOException
     362     */
     363    public boolean parse(boolean tryToFinish) throws SAXException, IOException {
    345364        Parser parser = new Parser();
    346         InputSource inputSource = new InputSource(new InputStreamReader(source, "UTF-8"));
    347365        try {
    348366            SAXParserFactory factory = SAXParserFactory.newInstance();
    349367            factory.setNamespaceAware(true);
    350368            factory.newSAXParser().parse(inputSource, parser);
     369            return true;
     370        } catch (SAXException e) {
     371            if (tryToFinish) {
     372                parser.tryToFinish();
     373                if (parser.currentData.isEmpty())
     374                    throw e;
     375                return false;
     376            } else
     377                throw e;
    351378        } catch (ParserConfigurationException e) {
    352379            e.printStackTrace(); // broken SAXException chaining
Note: See TracChangeset for help on using the changeset viewer.