source: josm/trunk/src/org/openstreetmap/josm/io/OsmImporter.java@ 12162

Last change on this file since 12162 was 10615, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

  • Property svn:eol-style set to native
File size: 5.7 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.FileNotFoundException;
8import java.io.IOException;
9import java.io.InputStream;
10import java.util.Arrays;
11
12import javax.swing.JOptionPane;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.actions.ExtensionFileFilter;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
19import org.openstreetmap.josm.gui.util.GuiHelper;
20
21public class OsmImporter extends FileImporter {
22
23 /**
24 * The OSM file filter (*.osm and *.xml files).
25 */
26 public static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions(
27 "osm,xml", "osm", tr("OSM Server Files") + " (*.osm, *.osm.gz, *.osm.bz2, *.osm.zip, *.xml)",
28 ExtensionFileFilter.AddArchiveExtension.NONE, Arrays.asList("gz", "bz", "bz2", "zip"));
29
30 /**
31 * Utility class containing imported OSM layer, and a task to run after it is added to MapView.
32 */
33 public static class OsmImporterData {
34
35 private final OsmDataLayer layer;
36 private final Runnable postLayerTask;
37
38 public OsmImporterData(OsmDataLayer layer, Runnable postLayerTask) {
39 this.layer = layer;
40 this.postLayerTask = postLayerTask;
41 }
42
43 public OsmDataLayer getLayer() {
44 return layer;
45 }
46
47 public Runnable getPostLayerTask() {
48 return postLayerTask;
49 }
50 }
51
52 /**
53 * Constructs a new {@code OsmImporter}.
54 */
55 public OsmImporter() {
56 super(FILE_FILTER);
57 }
58
59 /**
60 * Constructs a new {@code OsmImporter} with the given extension file filter.
61 * @param filter The extension file filter
62 */
63 public OsmImporter(ExtensionFileFilter filter) {
64 super(filter);
65 }
66
67 /**
68 * Imports OSM data from file
69 * @param file file to read data from
70 * @param progressMonitor handler for progress monitoring and canceling
71 */
72 @Override
73 public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
74 try (InputStream in = Compression.getUncompressedFileInputStream(file)) {
75 importData(in, file, progressMonitor);
76 } catch (FileNotFoundException e) {
77 Main.error(e);
78 throw new IOException(tr("File ''{0}'' does not exist.", file.getName()), e);
79 }
80 }
81
82 /**
83 * Imports OSM data from stream
84 * @param in input stream
85 * @param associatedFile filename of data (layer name will be generated from name of file)
86 * @param pm handler for progress monitoring and canceling
87 * @throws IllegalDataException if an error was found while parsing the OSM data
88 */
89 protected void importData(InputStream in, final File associatedFile, ProgressMonitor pm) throws IllegalDataException {
90 final OsmImporterData data = loadLayer(in, associatedFile,
91 associatedFile == null ? OsmDataLayer.createNewName() : associatedFile.getName(), pm);
92
93 // FIXME: remove UI stuff from IO subsystem
94 GuiHelper.runInEDT(() -> {
95 OsmDataLayer layer = data.getLayer();
96 Main.getLayerManager().addLayer(layer);
97 data.getPostLayerTask().run();
98 data.getLayer().onPostLoadFromFile();
99 });
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}
Note: See TracBrowser for help on using the repository browser.