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

Last change on this file since 12636 was 12636, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

  • Property svn:eol-style set to native
File size: 5.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.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.MainApplication;
18import org.openstreetmap.josm.gui.layer.OsmDataLayer;
19import org.openstreetmap.josm.gui.progress.ProgressMonitor;
20import org.openstreetmap.josm.gui.util.GuiHelper;
21import org.openstreetmap.josm.tools.Logging;
22
23/**
24 * File importer that reads *.osm data files. (main storage format for OSM data
25 * in JOSM)
26 */
27public class OsmImporter extends FileImporter {
28
29 /**
30 * The OSM file filter (*.osm and *.xml files).
31 */
32 public static final ExtensionFileFilter FILE_FILTER = ExtensionFileFilter.newFilterWithArchiveExtensions(
33 "osm,xml", "osm", tr("OSM Server Files") + " (*.osm, *.osm.gz, *.osm.bz2, *.osm.zip, *.xml)",
34 ExtensionFileFilter.AddArchiveExtension.NONE, Arrays.asList("gz", "bz", "bz2", "zip"));
35
36 /**
37 * Utility class containing imported OSM layer, and a task to run after it is added to MapView.
38 */
39 public static class OsmImporterData {
40
41 private final OsmDataLayer layer;
42 private final Runnable postLayerTask;
43
44 public OsmImporterData(OsmDataLayer layer, Runnable postLayerTask) {
45 this.layer = layer;
46 this.postLayerTask = postLayerTask;
47 }
48
49 public OsmDataLayer getLayer() {
50 return layer;
51 }
52
53 public Runnable getPostLayerTask() {
54 return postLayerTask;
55 }
56 }
57
58 /**
59 * Constructs a new {@code OsmImporter}.
60 */
61 public OsmImporter() {
62 super(FILE_FILTER);
63 }
64
65 /**
66 * Constructs a new {@code OsmImporter} with the given extension file filter.
67 * @param filter The extension file filter
68 */
69 public OsmImporter(ExtensionFileFilter filter) {
70 super(filter);
71 }
72
73 /**
74 * Imports OSM data from file
75 * @param file file to read data from
76 * @param progressMonitor handler for progress monitoring and canceling
77 */
78 @Override
79 public void importData(File file, ProgressMonitor progressMonitor) throws IOException, IllegalDataException {
80 try (InputStream in = Compression.getUncompressedFileInputStream(file)) {
81 importData(in, file, progressMonitor);
82 } catch (FileNotFoundException e) {
83 Logging.error(e);
84 throw new IOException(tr("File ''{0}'' does not exist.", file.getName()), e);
85 }
86 }
87
88 /**
89 * Imports OSM data from stream
90 * @param in input stream
91 * @param associatedFile filename of data (layer name will be generated from name of file)
92 * @param pm handler for progress monitoring and canceling
93 * @throws IllegalDataException if an error was found while parsing the OSM data
94 */
95 protected void importData(InputStream in, final File associatedFile, ProgressMonitor pm) throws IllegalDataException {
96 final OsmImporterData data = loadLayer(in, associatedFile,
97 associatedFile == null ? OsmDataLayer.createNewName() : associatedFile.getName(), pm);
98
99 // FIXME: remove UI stuff from IO subsystem
100 GuiHelper.runInEDT(() -> {
101 OsmDataLayer layer = data.getLayer();
102 MainApplication.getLayerManager().addLayer(layer);
103 data.getPostLayerTask().run();
104 data.getLayer().onPostLoadFromFile();
105 });
106 }
107
108 /**
109 * Load osm data layer from InputStream.
110 * @param in input stream
111 * @param associatedFile filename of data (can be <code>null</code> if the stream does not come from a file)
112 * @param layerName name of generated layer
113 * @param progressMonitor handler for progress monitoring and canceling
114 * @return Utility class containing imported OSM layer, and a task to run after it is added to MapView
115 * @throws IllegalDataException if an error was found while parsing the OSM data
116 */
117 public OsmImporterData loadLayer(InputStream in, final File associatedFile, final String layerName, ProgressMonitor progressMonitor)
118 throws IllegalDataException {
119 final DataSet dataSet = parseDataSet(in, progressMonitor);
120 if (dataSet == null) {
121 throw new IllegalDataException(tr("Invalid dataset"));
122 }
123 OsmDataLayer layer = createLayer(dataSet, associatedFile, layerName);
124 Runnable postLayerTask = createPostLayerTask(dataSet, associatedFile, layerName, layer);
125 return new OsmImporterData(layer, postLayerTask);
126 }
127
128 protected DataSet parseDataSet(InputStream in, ProgressMonitor progressMonitor) throws IllegalDataException {
129 return OsmReader.parseDataSet(in, progressMonitor);
130 }
131
132 protected OsmDataLayer createLayer(final DataSet dataSet, final File associatedFile, final String layerName) {
133 return new OsmDataLayer(dataSet, layerName, associatedFile);
134 }
135
136 protected Runnable createPostLayerTask(final DataSet dataSet, final File associatedFile, final String layerName, final OsmDataLayer layer) {
137 return () -> {
138 if (dataSet.allPrimitives().isEmpty()) {
139 String msg;
140 if (associatedFile == null) {
141 msg = tr("No data found for layer ''{0}''.", layerName);
142 } else {
143 msg = tr("No data found in file ''{0}''.", associatedFile.getPath());
144 }
145 JOptionPane.showMessageDialog(
146 Main.parent,
147 msg,
148 tr("Open OSM file"),
149 JOptionPane.INFORMATION_MESSAGE);
150 }
151 layer.onPostLoadFromFile();
152 };
153 }
154}
Note: See TracBrowser for help on using the repository browser.