source: josm/trunk/src/org/openstreetmap/josm/io/GpxImporter.java@ 2795

Last change on this file since 2795 was 2795, checked in by jttt, 14 years ago

Allow to load incomplete gpx files

File size: 3.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import java.io.FileInputStream;
8import java.io.FileNotFoundException;
9import java.io.IOException;
10import java.io.InputStream;
11import java.util.zip.GZIPInputStream;
12
13import javax.swing.JOptionPane;
14import javax.swing.SwingUtilities;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.actions.ExtensionFileFilter;
18import org.openstreetmap.josm.gui.layer.GpxLayer;
19import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
20import org.xml.sax.SAXException;
21
22public class GpxImporter extends FileImporter {
23
24 public GpxImporter() {
25 super(new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)"));
26 }
27
28 @Override public void importData(final File file) throws IOException {
29 final String fn = file.getName();
30
31 try {
32 InputStream is;
33 if (file.getName().endsWith(".gpx.gz")) {
34 is = new GZIPInputStream(new FileInputStream(file));
35 } else {
36 is = new FileInputStream(file);
37 }
38 // Workaround for SAX BOM bug
39 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6206835
40 if (!((is.read() == 0xef) && (is.read() == 0xbb) && (is.read() == 0xbf))) {
41 is.close();
42 if (file.getName().endsWith(".gpx.gz")) {
43 is = new GZIPInputStream(new FileInputStream(file));
44 } else {
45 is = new FileInputStream(file);
46 }
47 }
48 final GpxReader r = new GpxReader(is);
49 final boolean parsedProperly = r.parse(true);
50 r.data.storageFile = file;
51 final GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
52
53 // FIXME: remove UI stuff from the IO subsystem
54 //
55 Runnable task = new Runnable() {
56 public void run() {
57 if (!r.data.tracks.isEmpty() || ! r.data.routes.isEmpty()) {
58 Main.main.addLayer(gpxLayer);
59 }
60 if (Main.pref.getBoolean("marker.makeautomarkers", true) && !r.data.waypoints.isEmpty()) {
61 MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
62 if (ml.data.size() > 0) {
63 Main.main.addLayer(ml);
64 }
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()));
68 }
69 }
70 };
71 if (SwingUtilities.isEventDispatchThread()) {
72 task.run();
73 } else {
74 SwingUtilities.invokeLater(task);
75 }
76 } catch (FileNotFoundException e) {
77 e.printStackTrace();
78 throw new IOException(tr("File \"{0}\" does not exist", file.getName()));
79 } catch (SAXException e) {
80 e.printStackTrace();
81 throw new IOException(tr("Parsing file \"{0}\" failed", file.getName()));
82 }
83 }
84}
Note: See TracBrowser for help on using the repository browser.