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

Last change on this file since 2001 was 1814, checked in by Gubaer, 15 years ago

removed dependencies to Main.ds, removed Main.ds
removed AddVisitor, NameVisitor, DeleteVisitor - unnecessary double dispatching for these simple cases

File size: 2.4 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 org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.actions.ExtensionFileFilter;
15import org.openstreetmap.josm.gui.layer.GpxLayer;
16import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
17import org.xml.sax.SAXException;
18
19public 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}
Note: See TracBrowser for help on using the repository browser.