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

Last change on this file since 3965 was 3372, checked in by bastiK, 14 years ago

fixed #5226 - XML encodings and byte-order marker (BOM) support

  • Property svn:eol-style set to native
File size: 2.9 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 final GpxReader r = new GpxReader(is);
40 final boolean parsedProperly = r.parse(true);
41 r.data.storageFile = file;
42 final GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
43
44 // FIXME: remove UI stuff from the IO subsystem
45 //
46 Runnable task = new Runnable() {
47 public void run() {
48 if (r.data.hasRoutePoints() || r.data.hasTrackPoints()) {
49 Main.main.addLayer(gpxLayer);
50 }
51 if (Main.pref.getBoolean("marker.makeautomarkers", true) && !r.data.waypoints.isEmpty()) {
52 MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
53 if (ml.data.size() > 0) {
54 Main.main.addLayer(ml);
55 }
56 }
57 if (!parsedProperly) {
58 JOptionPane.showMessageDialog(null, tr("Error occured while parsing gpx file {0}. Only part of the file will be available", file.getName()));
59 }
60 }
61 };
62 if (SwingUtilities.isEventDispatchThread()) {
63 task.run();
64 } else {
65 SwingUtilities.invokeLater(task);
66 }
67 } catch (FileNotFoundException e) {
68 e.printStackTrace();
69 throw new IOException(tr("File \"{0}\" does not exist", file.getName()));
70 } catch (SAXException e) {
71 e.printStackTrace();
72 throw new IOException(tr("Parsing file \"{0}\" failed", file.getName()));
73 }
74 }
75}
Note: See TracBrowser for help on using the repository browser.