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 org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
15 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
16 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
17 | import org.xml.sax.SAXException;
|
---|
18 |
|
---|
19 | public class GpxImporter extends FileImporter {
|
---|
20 |
|
---|
21 | public GpxImporter() {
|
---|
22 | super(new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)"));
|
---|
23 | }
|
---|
24 |
|
---|
25 | @Override public void importData(File file) throws IOException {
|
---|
26 | String fn = file.getName();
|
---|
27 |
|
---|
28 | try {
|
---|
29 | GpxReader r = null;
|
---|
30 | InputStream is;
|
---|
31 | if (file.getName().endsWith(".gpx.gz")) {
|
---|
32 | is = new GZIPInputStream(new FileInputStream(file));
|
---|
33 | } else {
|
---|
34 | is = new FileInputStream(file);
|
---|
35 | }
|
---|
36 | // Workaround for SAX BOM bug
|
---|
37 | // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6206835
|
---|
38 | if (!((is.read() == 0xef) && (is.read() == 0xbb) && (is.read() == 0xbf))) {
|
---|
39 | is.close();
|
---|
40 | if (file.getName().endsWith(".gpx.gz")) {
|
---|
41 | is = new GZIPInputStream(new FileInputStream(file));
|
---|
42 | } else {
|
---|
43 | is = new FileInputStream(file);
|
---|
44 | }
|
---|
45 | }
|
---|
46 | r = new GpxReader(is, file.getAbsoluteFile().getParentFile());
|
---|
47 | r.data.storageFile = file;
|
---|
48 | GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
|
---|
49 | Main.main.addLayer(gpxLayer);
|
---|
50 | if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
|
---|
51 | MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
|
---|
52 | if (ml.data.size() > 0) {
|
---|
53 | Main.main.addLayer(ml);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | } catch (FileNotFoundException e) {
|
---|
57 | e.printStackTrace();
|
---|
58 | throw new IOException(tr("File \"{0}\" does not exist", file.getName()));
|
---|
59 | } catch (SAXException e) {
|
---|
60 | e.printStackTrace();
|
---|
61 | throw new IOException(tr("Parsing file \"{0}\" failed", file.getName()));
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|