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(new Runnable() { 095 @Override 096 public void run() { 097 OsmDataLayer layer = data.getLayer(); 098 Main.getLayerManager().addLayer(layer); 099 data.getPostLayerTask().run(); 100 data.getLayer().onPostLoadFromFile(); 101 } 102 }); 103 } 104 105 /** 106 * Load osm data layer from InputStream. 107 * @param in input stream 108 * @param associatedFile filename of data (can be <code>null</code> if the stream does not come from a file) 109 * @param layerName name of generated layer 110 * @param progressMonitor handler for progress monitoring and canceling 111 * @return Utility class containing imported OSM layer, and a task to run after it is added to MapView 112 * @throws IllegalDataException if an error was found while parsing the OSM data 113 */ 114 public OsmImporterData loadLayer(InputStream in, final File associatedFile, final String layerName, ProgressMonitor progressMonitor) 115 throws IllegalDataException { 116 final DataSet dataSet = parseDataSet(in, progressMonitor); 117 if (dataSet == null) { 118 throw new IllegalDataException(tr("Invalid dataset")); 119 } 120 OsmDataLayer layer = createLayer(dataSet, associatedFile, layerName); 121 Runnable postLayerTask = createPostLayerTask(dataSet, associatedFile, layerName, layer); 122 return new OsmImporterData(layer, postLayerTask); 123 } 124 125 protected DataSet parseDataSet(InputStream in, ProgressMonitor progressMonitor) throws IllegalDataException { 126 return OsmReader.parseDataSet(in, progressMonitor); 127 } 128 129 protected OsmDataLayer createLayer(final DataSet dataSet, final File associatedFile, final String layerName) { 130 return new OsmDataLayer(dataSet, layerName, associatedFile); 131 } 132 133 protected Runnable createPostLayerTask(final DataSet dataSet, final File associatedFile, final String layerName, final OsmDataLayer layer) { 134 return new Runnable() { 135 @Override 136 public void run() { 137 if (dataSet.allPrimitives().isEmpty()) { 138 String msg; 139 if (associatedFile == null) { 140 msg = tr("No data found for layer ''{0}''.", layerName); 141 } else { 142 msg = tr("No data found in file ''{0}''.", associatedFile.getPath()); 143 } 144 JOptionPane.showMessageDialog( 145 Main.parent, 146 msg, 147 tr("Open OSM file"), 148 JOptionPane.INFORMATION_MESSAGE); 149 } 150 layer.onPostLoadFromFile(); 151 } 152 }; 153 } 154}