001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.io; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.io.File; 007import java.io.FileNotFoundException; 008import java.io.IOException; 009import java.io.InputStream; 010import java.util.Arrays; 011 012import javax.swing.JOptionPane; 013 014import org.openstreetmap.josm.Main; 015import org.openstreetmap.josm.actions.ExtensionFileFilter; 016import org.openstreetmap.josm.data.osm.DataSet; 017import org.openstreetmap.josm.gui.layer.OsmDataLayer; 018import org.openstreetmap.josm.gui.progress.ProgressMonitor; 019import org.openstreetmap.josm.gui.util.GuiHelper; 020 021public class OsmImporter extends FileImporter { 022 023 /** 024 * The OSM file filter (*.osm and *.xml files). 025 */ 026 public static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions( 027 "osm,xml", "osm", tr("OSM Server Files") + " (*.osm, *.osm.gz, *.osm.bz2, *.osm.zip, *.xml)", 028 ExtensionFileFilter.AddArchiveExtension.NONE, Arrays.asList("gz", "bz", "bz2", "zip")); 029 030 /** 031 * Utility class containing imported OSM layer, and a task to run after it is added to MapView. 032 */ 033 public static class OsmImporterData { 034 035 private final OsmDataLayer layer; 036 private final Runnable postLayerTask; 037 038 public OsmImporterData(OsmDataLayer layer, Runnable postLayerTask) { 039 this.layer = layer; 040 this.postLayerTask = postLayerTask; 041 } 042 043 public OsmDataLayer getLayer() { 044 return layer; 045 } 046 047 public Runnable getPostLayerTask() { 048 return postLayerTask; 049 } 050 } 051 052 /** 053 * Constructs a new {@code OsmImporter}. 054 */ 055 public OsmImporter() { 056 super(FILE_FILTER); 057 } 058 059 /** 060 * Constructs a new {@code OsmImporter} with the given extension file filter. 061 * @param filter The extension file filter 062 */ 063 public OsmImporter(ExtensionFileFilter filter) { 064 super(filter); 065 } 066 067 /** 068 * Imports OSM data from file 069 * @param file file to read data from 070 * @param progressMonitor handler for progress monitoring and canceling 071 */ 072 @Override 073 public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException { 074 try (InputStream in = Compression.getUncompressedFileInputStream(file)) { 075 importData(in, file, progressMonitor); 076 } catch (FileNotFoundException e) { 077 Main.error(e); 078 throw new IOException(tr("File ''{0}'' does not exist.", file.getName()), e); 079 } 080 } 081 082 /** 083 * Imports OSM data from stream 084 * @param in input stream 085 * @param associatedFile filename of data (layer name will be generated from name of file) 086 * @param pm handler for progress monitoring and canceling 087 * @throws IllegalDataException if an error was found while parsing the OSM data 088 */ 089 protected void importData(InputStream in, final File associatedFile, ProgressMonitor pm) throws IllegalDataException { 090 final OsmImporterData data = loadLayer(in, associatedFile, 091 associatedFile == null ? OsmDataLayer.createNewName() : associatedFile.getName(), pm); 092 093 // FIXME: remove UI stuff from IO subsystem 094 GuiHelper.runInEDT(() -> { 095 OsmDataLayer layer = data.getLayer(); 096 Main.getLayerManager().addLayer(layer); 097 data.getPostLayerTask().run(); 098 data.getLayer().onPostLoadFromFile(); 099 }); 100 } 101 102 /** 103 * Load osm data layer from InputStream. 104 * @param in input stream 105 * @param associatedFile filename of data (can be <code>null</code> if the stream does not come from a file) 106 * @param layerName name of generated layer 107 * @param progressMonitor handler for progress monitoring and canceling 108 * @return Utility class containing imported OSM layer, and a task to run after it is added to MapView 109 * @throws IllegalDataException if an error was found while parsing the OSM data 110 */ 111 public OsmImporterData loadLayer(InputStream in, final File associatedFile, final String layerName, ProgressMonitor progressMonitor) 112 throws IllegalDataException { 113 final DataSet dataSet = parseDataSet(in, progressMonitor); 114 if (dataSet == null) { 115 throw new IllegalDataException(tr("Invalid dataset")); 116 } 117 OsmDataLayer layer = createLayer(dataSet, associatedFile, layerName); 118 Runnable postLayerTask = createPostLayerTask(dataSet, associatedFile, layerName, layer); 119 return new OsmImporterData(layer, postLayerTask); 120 } 121 122 protected DataSet parseDataSet(InputStream in, ProgressMonitor progressMonitor) throws IllegalDataException { 123 return OsmReader.parseDataSet(in, progressMonitor); 124 } 125 126 protected OsmDataLayer createLayer(final DataSet dataSet, final File associatedFile, final String layerName) { 127 return new OsmDataLayer(dataSet, layerName, associatedFile); 128 } 129 130 protected Runnable createPostLayerTask(final DataSet dataSet, final File associatedFile, final String layerName, final OsmDataLayer layer) { 131 return () -> { 132 if (dataSet.allPrimitives().isEmpty()) { 133 String msg; 134 if (associatedFile == null) { 135 msg = tr("No data found for layer ''{0}''.", layerName); 136 } else { 137 msg = tr("No data found in file ''{0}''.", associatedFile.getPath()); 138 } 139 JOptionPane.showMessageDialog( 140 Main.parent, 141 msg, 142 tr("Open OSM file"), 143 JOptionPane.INFORMATION_MESSAGE); 144 } 145 layer.onPostLoadFromFile(); 146 }; 147 } 148}