| 1 | Index: /Users/mueck/dev/josm/src/org/openstreetmap/josm/actions/DiskAccessAction.java
|
|---|
| 2 | ===================================================================
|
|---|
| 3 | --- /Users/mueck/dev/josm/src/org/openstreetmap/josm/actions/DiskAccessAction.java (revision 1632)
|
|---|
| 4 | +++ /Users/mueck/dev/josm/src/org/openstreetmap/josm/actions/DiskAccessAction.java (working copy)
|
|---|
| 5 | @@ -4,7 +4,6 @@
|
|---|
| 6 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 7 |
|
|---|
| 8 | import java.io.File;
|
|---|
| 9 | -
|
|---|
| 10 | import javax.swing.JFileChooser;
|
|---|
| 11 | import org.openstreetmap.josm.Main;
|
|---|
| 12 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
|---|
| 13 | @@ -9,6 +8,7 @@
|
|---|
| 14 | import org.openstreetmap.josm.Main;
|
|---|
| 15 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
|---|
| 16 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 17 | +import org.openstreetmap.josm.io.FileImporter;
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * Helper class for all actions that access the disk
|
|---|
| 21 | @@ -24,12 +24,14 @@
|
|---|
| 22 | if (curDir.equals(""))
|
|---|
| 23 | curDir = ".";
|
|---|
| 24 | JFileChooser fc = new JFileChooser(new File(curDir));
|
|---|
| 25 | - if(title != null)
|
|---|
| 26 | + if (title != null)
|
|---|
| 27 | fc.setDialogTitle(title);
|
|---|
| 28 |
|
|---|
| 29 | fc.setMultiSelectionEnabled(multiple);
|
|---|
| 30 | - for (int i = 0; i < ExtensionFileFilter.filters.length; ++i)
|
|---|
| 31 | - fc.addChoosableFileFilter(ExtensionFileFilter.filters[i]);
|
|---|
| 32 | + for (FileImporter imExporter: ExtensionFileFilter.importers) {
|
|---|
| 33 | + fc.addChoosableFileFilter(imExporter.filter);
|
|---|
| 34 | + }
|
|---|
| 35 | +
|
|---|
| 36 | fc.setAcceptAllFileFilterUsed(true);
|
|---|
| 37 |
|
|---|
| 38 | int answer = open ? fc.showOpenDialog(Main.parent) : fc.showSaveDialog(Main.parent);
|
|---|
| 39 | Index: /Users/mueck/dev/josm/src/org/openstreetmap/josm/actions/ExtensionFileFilter.java
|
|---|
| 40 | ===================================================================
|
|---|
| 41 | --- /Users/mueck/dev/josm/src/org/openstreetmap/josm/actions/ExtensionFileFilter.java (revision 1632)
|
|---|
| 42 | +++ /Users/mueck/dev/josm/src/org/openstreetmap/josm/actions/ExtensionFileFilter.java (working copy)
|
|---|
| 43 | @@ -1,12 +1,17 @@
|
|---|
| 44 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
|---|
| 45 | package org.openstreetmap.josm.actions;
|
|---|
| 46 |
|
|---|
| 47 | -import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 48 | -
|
|---|
| 49 | import java.io.File;
|
|---|
| 50 | +import java.util.ArrayList;
|
|---|
| 51 | +import java.util.Arrays;
|
|---|
| 52 |
|
|---|
| 53 | import javax.swing.filechooser.FileFilter;
|
|---|
| 54 |
|
|---|
| 55 | +import org.openstreetmap.josm.io.FileImporter;
|
|---|
| 56 | +import org.openstreetmap.josm.io.GpxImporter;
|
|---|
| 57 | +import org.openstreetmap.josm.io.NMEAImporter;
|
|---|
| 58 | +import org.openstreetmap.josm.io.OsmImporter;
|
|---|
| 59 | +
|
|---|
| 60 | /**
|
|---|
| 61 | * A file filter that filters after the extension. Also includes a list of file
|
|---|
| 62 | * filters used in JOSM.
|
|---|
| 63 | @@ -18,15 +23,8 @@
|
|---|
| 64 | private final String description;
|
|---|
| 65 | public final String defaultExtension;
|
|---|
| 66 |
|
|---|
| 67 | - public static final int OSM = 0;
|
|---|
| 68 | - public static final int GPX = 1;
|
|---|
| 69 | - public static final int NMEA = 2;
|
|---|
| 70 | -
|
|---|
| 71 | - public static ExtensionFileFilter[] filters = {
|
|---|
| 72 | - new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files")+ " (*.osm *.xml)"),
|
|---|
| 73 | - new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)"),
|
|---|
| 74 | - new ExtensionFileFilter("nmea,nme,nma,txt", "nmea", tr("NMEA-0183 Files") + " (*.nmea *.nme *.nma *.txt)"),
|
|---|
| 75 | - };
|
|---|
| 76 | + public static ArrayList<FileImporter> importers = new ArrayList<FileImporter>(Arrays.asList(new OsmImporter(),
|
|---|
| 77 | + new GpxImporter(), new NMEAImporter()));
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Construct an extension file filter by giving the extension to check after.
|
|---|
| 81 | Index: /Users/mueck/dev/josm/src/org/openstreetmap/josm/actions/OpenFileAction.java
|
|---|
| 82 | ===================================================================
|
|---|
| 83 | --- /Users/mueck/dev/josm/src/org/openstreetmap/josm/actions/OpenFileAction.java (revision 1632)
|
|---|
| 84 | +++ /Users/mueck/dev/josm/src/org/openstreetmap/josm/actions/OpenFileAction.java (working copy)
|
|---|
| 85 | @@ -5,12 +5,8 @@
|
|---|
| 86 |
|
|---|
| 87 | import java.awt.event.ActionEvent;
|
|---|
| 88 | import java.awt.event.KeyEvent;
|
|---|
| 89 | -import java.io.InputStream;
|
|---|
| 90 | import java.io.File;
|
|---|
| 91 | -import java.io.FileInputStream;
|
|---|
| 92 | -import java.io.FileNotFoundException;
|
|---|
| 93 | import java.io.IOException;
|
|---|
| 94 | -import java.util.zip.GZIPInputStream;
|
|---|
| 95 |
|
|---|
| 96 | import javax.swing.JFileChooser;
|
|---|
| 97 | import javax.swing.JOptionPane;
|
|---|
| 98 | @@ -16,19 +12,12 @@
|
|---|
| 99 | import javax.swing.JOptionPane;
|
|---|
| 100 |
|
|---|
| 101 | import org.openstreetmap.josm.Main;
|
|---|
| 102 | -import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 103 | -import org.openstreetmap.josm.gui.layer.GpxLayer;
|
|---|
| 104 | -import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 105 | -import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
|---|
| 106 | -import org.openstreetmap.josm.io.GpxReader;
|
|---|
| 107 | -import org.openstreetmap.josm.io.NmeaReader;
|
|---|
| 108 | -import org.openstreetmap.josm.io.OsmReader;
|
|---|
| 109 | -import org.xml.sax.SAXException;
|
|---|
| 110 | +import org.openstreetmap.josm.io.FileImporter;
|
|---|
| 111 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 112 |
|
|---|
| 113 | /**
|
|---|
| 114 | - * Open a file chooser dialog and select an file to import. Then call the gpx-import
|
|---|
| 115 | - * driver. Finally open an internal frame into the main window with the gpx data shown.
|
|---|
| 116 | + * Open a file chooser dialog and select an file to import. Then call the gpx-import driver. Finally
|
|---|
| 117 | + * open an internal frame into the main window with the gpx data shown.
|
|---|
| 118 | *
|
|---|
| 119 | * @author imi
|
|---|
| 120 | */
|
|---|
| 121 | @@ -57,126 +47,14 @@
|
|---|
| 122 | public void openFile(File file) {
|
|---|
| 123 | try {
|
|---|
| 124 | System.out.println("Open file: " + file.getAbsolutePath() + " (" + file.length() + " bytes)");
|
|---|
| 125 | - if (asGpxData(file.getName()))
|
|---|
| 126 | - openFileAsGpx(file);
|
|---|
| 127 | - else if (asNmeaData(file.getName()))
|
|---|
| 128 | - openFileAsNmea(file);
|
|---|
| 129 | - else
|
|---|
| 130 | - openAsData(file);
|
|---|
| 131 | - } catch (SAXException x) {
|
|---|
| 132 | - x.printStackTrace();
|
|---|
| 133 | - JOptionPane.showMessageDialog(Main.parent, tr("Error while parsing {0}",file.getName())+": "+x.getMessage());
|
|---|
| 134 | + for (FileImporter importer : ExtensionFileFilter.importers)
|
|---|
| 135 | + if (importer.acceptFile(file))
|
|---|
| 136 | + importer.importData(file);
|
|---|
| 137 | } catch (IOException x) {
|
|---|
| 138 | x.printStackTrace();
|
|---|
| 139 | - JOptionPane.showMessageDialog(Main.parent, tr("Could not read \"{0}\"",file.getName())+"\n"+x.getMessage());
|
|---|
| 140 | - }
|
|---|
| 141 | - }
|
|---|
| 142 | -
|
|---|
| 143 | - private void openAsData(File file) throws SAXException, IOException, FileNotFoundException {
|
|---|
| 144 | - String fn = file.getName();
|
|---|
| 145 | - if (ExtensionFileFilter.filters[ExtensionFileFilter.OSM].acceptName(fn)) {
|
|---|
| 146 | - OsmReader osm = OsmReader.parseDataSetOsm(new FileInputStream(file), null, Main.pleaseWaitDlg);
|
|---|
| 147 | - DataSet dataSet = osm.getDs();
|
|---|
| 148 | - OsmDataLayer layer = new OsmDataLayer(dataSet, file.getName(), file);
|
|---|
| 149 | - Main.main.addLayer(layer);
|
|---|
| 150 | - layer.fireDataChange();
|
|---|
| 151 | - if (osm.getParseNotes().length() != 0) {
|
|---|
| 152 | - /* display at most five lines */
|
|---|
| 153 | - String notes = osm.getParseNotes();
|
|---|
| 154 | - int j = 0;
|
|---|
| 155 | - for (int i = 0; i < 5; i++) {
|
|---|
| 156 | - j = notes.indexOf('\n', j + 1);
|
|---|
| 157 | - }
|
|---|
| 158 | - j = j >= 0 ? j : notes.length();
|
|---|
| 159 | - JOptionPane.showMessageDialog(Main.parent, notes.substring(0, j));
|
|---|
| 160 | - }
|
|---|
| 161 | - }
|
|---|
| 162 | - else
|
|---|
| 163 | - JOptionPane.showMessageDialog(Main.parent, fn+": "+tr("Unknown file extension: {0}", fn.substring(file.getName().lastIndexOf('.')+1)));
|
|---|
| 164 | - }
|
|---|
| 165 | -
|
|---|
| 166 | - private void openFileAsGpx(File file) throws SAXException, IOException, FileNotFoundException {
|
|---|
| 167 | - String fn = file.getName();
|
|---|
| 168 | - if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn)) {
|
|---|
| 169 | - GpxReader r = null;
|
|---|
| 170 | - InputStream is;
|
|---|
| 171 | - if (file.getName().endsWith(".gpx.gz")) {
|
|---|
| 172 | - is = new GZIPInputStream(new FileInputStream(file));
|
|---|
| 173 | - } else {
|
|---|
| 174 | - is = new FileInputStream(file);
|
|---|
| 175 | - }
|
|---|
| 176 | - // Workaround for SAX BOM bug
|
|---|
| 177 | - // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6206835
|
|---|
| 178 | - if(!((is.read()==0xef)&&(is.read()==0xbb)&&(is.read()==0xbf))) {
|
|---|
| 179 | - is.close();
|
|---|
| 180 | - if (file.getName().endsWith(".gpx.gz")) {
|
|---|
| 181 | - is = new GZIPInputStream(new FileInputStream(file));
|
|---|
| 182 | - } else {
|
|---|
| 183 | - is = new FileInputStream(file);
|
|---|
| 184 | - }
|
|---|
| 185 | - }
|
|---|
| 186 | - r = new GpxReader(is,file.getAbsoluteFile().getParentFile());
|
|---|
| 187 | - r.data.storageFile = file;
|
|---|
| 188 | - GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
|
|---|
| 189 | - Main.main.addLayer(gpxLayer);
|
|---|
| 190 | - if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
|
|---|
| 191 | - MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
|
|---|
| 192 | - if (ml.data.size() > 0) {
|
|---|
| 193 | - Main.main.addLayer(ml);
|
|---|
| 194 | - }
|
|---|
| 195 | - }
|
|---|
| 196 | -
|
|---|
| 197 | - } else {
|
|---|
| 198 | - throw new IllegalStateException();
|
|---|
| 199 | - }
|
|---|
| 200 | - }
|
|---|
| 201 | -
|
|---|
| 202 | - private void showNmeaInfobox(boolean success, NmeaReader r) {
|
|---|
| 203 | - String msg = tr("Coordinates imported: ") + r.getNumberOfCoordinates() + "\n" +
|
|---|
| 204 | - tr("Malformed sentences: ") + r.getParserMalformed() + "\n" +
|
|---|
| 205 | - tr("Checksum errors: ") + r.getParserChecksumErrors() + "\n";
|
|---|
| 206 | - if(!success) // don't scare the user unneccessarily
|
|---|
| 207 | - msg += tr("Unknown sentences: ") + r.getParserUnknown() + "\n";
|
|---|
| 208 | - msg += tr("Zero coordinates: ") + r.getParserZeroCoordinates();
|
|---|
| 209 | - if(success) {
|
|---|
| 210 | - JOptionPane.showMessageDialog(
|
|---|
| 211 | - Main.parent, msg,
|
|---|
| 212 | - tr("NMEA import success"),JOptionPane.INFORMATION_MESSAGE);
|
|---|
| 213 | - } else {
|
|---|
| 214 | - JOptionPane.showMessageDialog(
|
|---|
| 215 | - Main.parent, msg,
|
|---|
| 216 | - tr("NMEA import faliure!"),JOptionPane.ERROR_MESSAGE);
|
|---|
| 217 | - }
|
|---|
| 218 | - }
|
|---|
| 219 | -
|
|---|
| 220 | - private void openFileAsNmea(File file) throws IOException, FileNotFoundException {
|
|---|
| 221 | - String fn = file.getName();
|
|---|
| 222 | - if (ExtensionFileFilter.filters[ExtensionFileFilter.NMEA].acceptName(fn)) {
|
|---|
| 223 | - NmeaReader r = new NmeaReader(new FileInputStream(file), file.getAbsoluteFile().getParentFile());
|
|---|
| 224 | - if(r.getNumberOfCoordinates()>0) {
|
|---|
| 225 | - r.data.storageFile = file;
|
|---|
| 226 | - GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
|
|---|
| 227 | - Main.main.addLayer(gpxLayer);
|
|---|
| 228 | - if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
|
|---|
| 229 | - MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
|
|---|
| 230 | - if (ml.data.size() > 0) {
|
|---|
| 231 | - Main.main.addLayer(ml);
|
|---|
| 232 | - }
|
|---|
| 233 | - }
|
|---|
| 234 | - }
|
|---|
| 235 | - showNmeaInfobox(r.getNumberOfCoordinates()>0, r);
|
|---|
| 236 | - } else {
|
|---|
| 237 | - throw new IllegalStateException();
|
|---|
| 238 | + JOptionPane.showMessageDialog(Main.parent, tr("Could not read \"{0}\"", file.getName()) + "\n"
|
|---|
| 239 | + + x.getMessage());
|
|---|
| 240 | }
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | - private boolean asGpxData(String fn) {
|
|---|
| 244 | - return ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(fn);
|
|---|
| 245 | - }
|
|---|
| 246 | -
|
|---|
| 247 | - private boolean asNmeaData(String fn) {
|
|---|
| 248 | - return ExtensionFileFilter.filters[ExtensionFileFilter.NMEA].acceptName(fn);
|
|---|
| 249 | - }
|
|---|
| 250 | -
|
|---|
| 251 | -
|
|---|
| 252 | }
|
|---|
| 253 | Index: /Users/mueck/dev/josm/src/org/openstreetmap/josm/actions/SaveActionBase.java
|
|---|
| 254 | ===================================================================
|
|---|
| 255 | --- /Users/mueck/dev/josm/src/org/openstreetmap/josm/actions/SaveActionBase.java (revision 1632)
|
|---|
| 256 | +++ /Users/mueck/dev/josm/src/org/openstreetmap/josm/actions/SaveActionBase.java (working copy)
|
|---|
| 257 | @@ -21,7 +21,9 @@
|
|---|
| 258 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
|---|
| 259 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 260 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 261 | +import org.openstreetmap.josm.io.GpxImporter;
|
|---|
| 262 | import org.openstreetmap.josm.io.GpxWriter;
|
|---|
| 263 | +import org.openstreetmap.josm.io.OsmImporter;
|
|---|
| 264 | import org.openstreetmap.josm.io.OsmWriter;
|
|---|
| 265 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 266 |
|
|---|
| 267 | @@ -149,9 +147,11 @@
|
|---|
| 268 | public static void save(File file, OsmDataLayer layer) {
|
|---|
| 269 | File tmpFile = null;
|
|---|
| 270 | try {
|
|---|
| 271 | - if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(file.getPath())) {
|
|---|
| 272 | + GpxImporter gpxImExporter = new GpxImporter();
|
|---|
| 273 | + OsmImporter osmImExporter = new OsmImporter();
|
|---|
| 274 | + if (gpxImExporter.acceptFile(file))
|
|---|
| 275 | GpxExportAction.exportGpx(file, layer);
|
|---|
| 276 | - } else if (ExtensionFileFilter.filters[ExtensionFileFilter.OSM].acceptName(file.getPath())) {
|
|---|
| 277 | + else if (osmImExporter.acceptFile(file)) {
|
|---|
| 278 | // use a tmp file because if something errors out in the
|
|---|
| 279 | // process of writing the file, we might just end up with
|
|---|
| 280 | // a truncated file. That can destroy lots of work.
|
|---|
| 281 | @@ -193,7 +192,8 @@
|
|---|
| 282 | public static void save(File file, GpxLayer layer) {
|
|---|
| 283 | File tmpFile = null;
|
|---|
| 284 | try {
|
|---|
| 285 | - if (ExtensionFileFilter.filters[ExtensionFileFilter.GPX].acceptName(file.getPath())) {
|
|---|
| 286 | + GpxImporter gpxImExporter = new GpxImporter();
|
|---|
| 287 | + if (gpxImExporter.acceptFile(file)) {
|
|---|
| 288 |
|
|---|
| 289 | // use a tmp file because if something errors out in the
|
|---|
| 290 | // process of writing the file, we might just end up with
|
|---|
| 291 | Index: /Users/mueck/dev/josm/src/org/openstreetmap/josm/io/FileImporter.java
|
|---|
| 292 | ===================================================================
|
|---|
| 293 | --- /Users/mueck/dev/josm/src/org/openstreetmap/josm/io/FileImporter.java (revision 0)
|
|---|
| 294 | +++ /Users/mueck/dev/josm/src/org/openstreetmap/josm/io/FileImporter.java (revision 0)
|
|---|
| 295 | @@ -0,0 +1,27 @@
|
|---|
| 296 | +// License: GPL. For details, see LICENSE file.
|
|---|
| 297 | +package org.openstreetmap.josm.io;
|
|---|
| 298 | +
|
|---|
| 299 | +import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 300 | +
|
|---|
| 301 | +import java.io.File;
|
|---|
| 302 | +import java.io.IOException;
|
|---|
| 303 | +
|
|---|
| 304 | +import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
|---|
| 305 | +
|
|---|
| 306 | +public abstract class FileImporter {
|
|---|
| 307 | +
|
|---|
| 308 | + public ExtensionFileFilter filter;
|
|---|
| 309 | +
|
|---|
| 310 | + public FileImporter(ExtensionFileFilter filter) {
|
|---|
| 311 | + this.filter = filter;
|
|---|
| 312 | + }
|
|---|
| 313 | +
|
|---|
| 314 | + public boolean acceptFile(File pathname) {
|
|---|
| 315 | + return filter.acceptName(pathname.getName());
|
|---|
| 316 | + }
|
|---|
| 317 | +
|
|---|
| 318 | + public void importData(File file) throws IOException {
|
|---|
| 319 | + throw new IOException(tr("Could not read \"{0}\"", file.getName()));
|
|---|
| 320 | + }
|
|---|
| 321 | +
|
|---|
| 322 | +}
|
|---|
| 323 | Index: /Users/mueck/dev/josm/src/org/openstreetmap/josm/io/GpxImporter.java
|
|---|
| 324 | ===================================================================
|
|---|
| 325 | --- /Users/mueck/dev/josm/src/org/openstreetmap/josm/io/GpxImporter.java (revision 0)
|
|---|
| 326 | +++ /Users/mueck/dev/josm/src/org/openstreetmap/josm/io/GpxImporter.java (revision 0)
|
|---|
| 327 | @@ -0,0 +1,59 @@
|
|---|
| 328 | +// License: GPL. For details, see LICENSE file.
|
|---|
| 329 | +package org.openstreetmap.josm.io;
|
|---|
| 330 | +
|
|---|
| 331 | +import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 332 | +
|
|---|
| 333 | +import java.io.File;
|
|---|
| 334 | +import java.io.FileInputStream;
|
|---|
| 335 | +import java.io.IOException;
|
|---|
| 336 | +import java.io.InputStream;
|
|---|
| 337 | +import java.util.zip.GZIPInputStream;
|
|---|
| 338 | +
|
|---|
| 339 | +import org.openstreetmap.josm.Main;
|
|---|
| 340 | +import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
|---|
| 341 | +import org.openstreetmap.josm.gui.layer.GpxLayer;
|
|---|
| 342 | +import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
|---|
| 343 | +import org.xml.sax.SAXException;
|
|---|
| 344 | +
|
|---|
| 345 | +public class GpxImporter extends FileImporter {
|
|---|
| 346 | +
|
|---|
| 347 | + public GpxImporter() {
|
|---|
| 348 | + super(new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)"));
|
|---|
| 349 | + }
|
|---|
| 350 | +
|
|---|
| 351 | + @Override public void importData(File file) throws IOException {
|
|---|
| 352 | + String fn = file.getName();
|
|---|
| 353 | +
|
|---|
| 354 | + try {
|
|---|
| 355 | + GpxReader r = null;
|
|---|
| 356 | + InputStream is;
|
|---|
| 357 | + if (file.getName().endsWith(".gpx.gz"))
|
|---|
| 358 | + is = new GZIPInputStream(new FileInputStream(file));
|
|---|
| 359 | + else
|
|---|
| 360 | + is = new FileInputStream(file);
|
|---|
| 361 | + // Workaround for SAX BOM bug
|
|---|
| 362 | + // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6206835
|
|---|
| 363 | + if (!((is.read() == 0xef) && (is.read() == 0xbb) && (is.read() == 0xbf))) {
|
|---|
| 364 | + is.close();
|
|---|
| 365 | + if (file.getName().endsWith(".gpx.gz"))
|
|---|
| 366 | + is = new GZIPInputStream(new FileInputStream(file));
|
|---|
| 367 | + else
|
|---|
| 368 | + is = new FileInputStream(file);
|
|---|
| 369 | + }
|
|---|
| 370 | + r = new GpxReader(is, file.getAbsoluteFile().getParentFile());
|
|---|
| 371 | + r.data.storageFile = file;
|
|---|
| 372 | + GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
|
|---|
| 373 | + Main.main.addLayer(gpxLayer);
|
|---|
| 374 | + if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
|
|---|
| 375 | + MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
|
|---|
| 376 | + if (ml.data.size() > 0)
|
|---|
| 377 | + Main.main.addLayer(ml);
|
|---|
| 378 | + }
|
|---|
| 379 | + } catch (SAXException e) {
|
|---|
| 380 | + e.printStackTrace();
|
|---|
| 381 | + throw new IOException(tr("Could not read \"{0}\"", file.getName()));
|
|---|
| 382 | + }
|
|---|
| 383 | +
|
|---|
| 384 | + }
|
|---|
| 385 | +
|
|---|
| 386 | +}
|
|---|
| 387 | Index: /Users/mueck/dev/josm/src/org/openstreetmap/josm/io/NMEAImporter.java
|
|---|
| 388 | ===================================================================
|
|---|
| 389 | --- /Users/mueck/dev/josm/src/org/openstreetmap/josm/io/NMEAImporter.java (revision 0)
|
|---|
| 390 | +++ /Users/mueck/dev/josm/src/org/openstreetmap/josm/io/NMEAImporter.java (revision 0)
|
|---|
| 391 | @@ -0,0 +1,55 @@
|
|---|
| 392 | +// License: GPL. For details, see LICENSE file.
|
|---|
| 393 | +package org.openstreetmap.josm.io;
|
|---|
| 394 | +
|
|---|
| 395 | +import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 396 | +
|
|---|
| 397 | +import java.io.File;
|
|---|
| 398 | +import java.io.FileInputStream;
|
|---|
| 399 | +import java.io.IOException;
|
|---|
| 400 | +
|
|---|
| 401 | +import javax.swing.JOptionPane;
|
|---|
| 402 | +
|
|---|
| 403 | +import org.openstreetmap.josm.Main;
|
|---|
| 404 | +import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
|---|
| 405 | +import org.openstreetmap.josm.gui.layer.GpxLayer;
|
|---|
| 406 | +import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
|---|
| 407 | +
|
|---|
| 408 | +public class NMEAImporter extends FileImporter {
|
|---|
| 409 | +
|
|---|
| 410 | + public NMEAImporter() {
|
|---|
| 411 | + super(
|
|---|
| 412 | + new ExtensionFileFilter("nmea,nme,nma,txt", "nmea", tr("NMEA-0183 Files")
|
|---|
| 413 | + + " (*.nmea *.nme *.nma *.txt)"));
|
|---|
| 414 | + }
|
|---|
| 415 | +
|
|---|
| 416 | + @Override public void importData(File file) throws IOException {
|
|---|
| 417 | + String fn = file.getName();
|
|---|
| 418 | + NmeaReader r = new NmeaReader(new FileInputStream(file), file.getAbsoluteFile().getParentFile());
|
|---|
| 419 | + if (r.getNumberOfCoordinates() > 0) {
|
|---|
| 420 | + r.data.storageFile = file;
|
|---|
| 421 | + GpxLayer gpxLayer = new GpxLayer(r.data, fn, true);
|
|---|
| 422 | + Main.main.addLayer(gpxLayer);
|
|---|
| 423 | + if (Main.pref.getBoolean("marker.makeautomarkers", true)) {
|
|---|
| 424 | + MarkerLayer ml = new MarkerLayer(r.data, tr("Markers from {0}", fn), file, gpxLayer);
|
|---|
| 425 | + if (ml.data.size() > 0) {
|
|---|
| 426 | + Main.main.addLayer(ml);
|
|---|
| 427 | + }
|
|---|
| 428 | + }
|
|---|
| 429 | + }
|
|---|
| 430 | + showNmeaInfobox(r.getNumberOfCoordinates() > 0, r);
|
|---|
| 431 | + }
|
|---|
| 432 | +
|
|---|
| 433 | + private void showNmeaInfobox(boolean success, NmeaReader r) {
|
|---|
| 434 | + String msg = tr("Coordinates imported: ") + r.getNumberOfCoordinates() + "\n" + tr("Malformed sentences: ")
|
|---|
| 435 | + + r.getParserMalformed() + "\n" + tr("Checksum errors: ") + r.getParserChecksumErrors() + "\n";
|
|---|
| 436 | + if (!success) // don't scare the user unneccessarily
|
|---|
| 437 | + msg += tr("Unknown sentences: ") + r.getParserUnknown() + "\n";
|
|---|
| 438 | + msg += tr("Zero coordinates: ") + r.getParserZeroCoordinates();
|
|---|
| 439 | + if (success) {
|
|---|
| 440 | + JOptionPane.showMessageDialog(Main.parent, msg, tr("NMEA import success"), JOptionPane.INFORMATION_MESSAGE);
|
|---|
| 441 | + } else {
|
|---|
| 442 | + JOptionPane.showMessageDialog(Main.parent, msg, tr("NMEA import faliure!"), JOptionPane.ERROR_MESSAGE);
|
|---|
| 443 | + }
|
|---|
| 444 | + }
|
|---|
| 445 | +
|
|---|
| 446 | +}
|
|---|
| 447 | Index: /Users/mueck/dev/josm/src/org/openstreetmap/josm/io/OsmImporter.java
|
|---|
| 448 | ===================================================================
|
|---|
| 449 | --- /Users/mueck/dev/josm/src/org/openstreetmap/josm/io/OsmImporter.java (revision 0)
|
|---|
| 450 | +++ /Users/mueck/dev/josm/src/org/openstreetmap/josm/io/OsmImporter.java (revision 0)
|
|---|
| 451 | @@ -0,0 +1,53 @@
|
|---|
| 452 | +// License: GPL. For details, see LICENSE file.
|
|---|
| 453 | +package org.openstreetmap.josm.io;
|
|---|
| 454 | +
|
|---|
| 455 | +import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 456 | +
|
|---|
| 457 | +import java.awt.HeadlessException;
|
|---|
| 458 | +import java.io.File;
|
|---|
| 459 | +import java.io.FileInputStream;
|
|---|
| 460 | +import java.io.FileNotFoundException;
|
|---|
| 461 | +import java.io.IOException;
|
|---|
| 462 | +
|
|---|
| 463 | +import javax.swing.JOptionPane;
|
|---|
| 464 | +
|
|---|
| 465 | +import org.openstreetmap.josm.Main;
|
|---|
| 466 | +import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
|---|
| 467 | +import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 468 | +import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 469 | +import org.xml.sax.SAXException;
|
|---|
| 470 | +
|
|---|
| 471 | +public class OsmImporter extends FileImporter {
|
|---|
| 472 | +
|
|---|
| 473 | + public OsmImporter() {
|
|---|
| 474 | + super(new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files") + " (*.osm *.xml)"));
|
|---|
| 475 | + }
|
|---|
| 476 | +
|
|---|
| 477 | + @Override public void importData(File file) throws IOException {
|
|---|
| 478 | + try {
|
|---|
| 479 | + OsmReader osm = OsmReader.parseDataSetOsm(new FileInputStream(file), null, Main.pleaseWaitDlg);
|
|---|
| 480 | + DataSet dataSet = osm.getDs();
|
|---|
| 481 | + OsmDataLayer layer = new OsmDataLayer(dataSet, file.getName(), file);
|
|---|
| 482 | + Main.main.addLayer(layer);
|
|---|
| 483 | + layer.fireDataChange();
|
|---|
| 484 | + if (osm.getParseNotes().length() != 0) {
|
|---|
| 485 | + /* display at most five lines */
|
|---|
| 486 | + String notes = osm.getParseNotes();
|
|---|
| 487 | + int j = 0;
|
|---|
| 488 | + for (int i = 0; i < 5; i++)
|
|---|
| 489 | + j = notes.indexOf('\n', j + 1);
|
|---|
| 490 | + j = j >= 0 ? j : notes.length();
|
|---|
| 491 | + JOptionPane.showMessageDialog(Main.parent, notes.substring(0, j));
|
|---|
| 492 | + }
|
|---|
| 493 | + } catch (HeadlessException e) {
|
|---|
| 494 | + e.printStackTrace();
|
|---|
| 495 | + throw new IOException(tr("Could not read \"{0}\"", file.getName()));
|
|---|
| 496 | + } catch (FileNotFoundException e) {
|
|---|
| 497 | + e.printStackTrace();
|
|---|
| 498 | + throw new IOException(tr("Could not read \"{0}\"", file.getName()));
|
|---|
| 499 | + } catch (SAXException e) {
|
|---|
| 500 | + e.printStackTrace();
|
|---|
| 501 | + throw new IOException(tr("Could not read \"{0}\"", file.getName()));
|
|---|
| 502 | + }
|
|---|
| 503 | + }
|
|---|
| 504 | +}
|
|---|