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

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

Better gpx layer tooltip, don't add gpx layer if gpx file is empty

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.openstreetmap.josm.gui.progress.ProgressMonitor;
21import org.xml.sax.SAXException;
22
23public 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}
Note: See TracBrowser for help on using the repository browser.