[1637] | 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;
|
---|
[1696] | 8 | import java.io.FileNotFoundException;
|
---|
[1637] | 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 | // Workaround for SAX BOM bug
|
---|
| 36 | // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6206835
|
---|
| 37 | if (!((is.read() == 0xef) && (is.read() == 0xbb) && (is.read() == 0xbf))) {
|
---|
| 38 | is.close();
|
---|
| 39 | if (file.getName().endsWith(".gpx.gz"))
|
---|
| 40 | is = new GZIPInputStream(new FileInputStream(file));
|
---|
| 41 | else
|
---|
| 42 | is = new FileInputStream(file);
|
---|
| 43 | }
|
---|
| 44 | r = new GpxReader(is, file.getAbsoluteFile().getParentFile());
|
---|
| 45 | r.data.storageFile = file;
|
---|
| 46 | GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
|
---|
| 47 | Main.main.addLayer(gpxLayer);
|
---|
| 48 | if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
|
---|
| 49 | MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
|
---|
| 50 | if (ml.data.size() > 0)
|
---|
| 51 | Main.main.addLayer(ml);
|
---|
| 52 | }
|
---|
[1696] | 53 | } catch (FileNotFoundException e) {
|
---|
| 54 | e.printStackTrace();
|
---|
| 55 | throw new IOException(tr("File \"{0}\" does not exist", file.getName()));
|
---|
[1637] | 56 | } catch (SAXException e) {
|
---|
| 57 | e.printStackTrace();
|
---|
[1696] | 58 | throw new IOException(tr("Parsing file \"{0}\" failed", file.getName()));
|
---|
[1637] | 59 | }
|
---|
| 60 | }
|
---|
| 61 | }
|
---|