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