1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.HeadlessException;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FileInputStream;
|
---|
9 | import java.io.FileNotFoundException;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.io.InputStream;
|
---|
12 |
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
18 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
19 | import org.xml.sax.SAXException;
|
---|
20 |
|
---|
21 | public class OsmImporter extends FileImporter {
|
---|
22 |
|
---|
23 | public OsmImporter() {
|
---|
24 | super(new ExtensionFileFilter("osm,xml", "osm", tr("OSM Server Files") + " (*.osm *.xml)"));
|
---|
25 | }
|
---|
26 |
|
---|
27 | public OsmImporter(ExtensionFileFilter filter) {
|
---|
28 | super(filter);
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override public void importData(File file) throws IOException {
|
---|
32 | try {
|
---|
33 | FileInputStream in = new FileInputStream(file);
|
---|
34 | importData(in, file);
|
---|
35 | } catch (HeadlessException e) {
|
---|
36 | e.printStackTrace();
|
---|
37 | throw new IOException(tr("Could not read \"{0}\"", file.getName()));
|
---|
38 | } catch (FileNotFoundException e) {
|
---|
39 | e.printStackTrace();
|
---|
40 | throw new IOException(tr("Could not read \"{0}\"", file.getName()));
|
---|
41 | } catch (SAXException e) {
|
---|
42 | e.printStackTrace();
|
---|
43 | throw new IOException(tr("Could not read \"{0}\"", file.getName()));
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | protected void importData(InputStream in, File associatedFile) throws SAXException, IOException {
|
---|
48 | OsmReader osm = OsmReader.parseDataSetOsm(in, null, Main.pleaseWaitDlg);
|
---|
49 | DataSet dataSet = osm.getDs();
|
---|
50 | OsmDataLayer layer = new OsmDataLayer(dataSet, associatedFile.getName(), associatedFile);
|
---|
51 | Main.main.addLayer(layer);
|
---|
52 | layer.fireDataChange();
|
---|
53 | if (osm.getParseNotes().length() != 0) {
|
---|
54 | /* display at most five lines */
|
---|
55 | String notes = osm.getParseNotes();
|
---|
56 | int j = 0;
|
---|
57 | for (int i = 0; i < 5; i++)
|
---|
58 | j = notes.indexOf('\n', j + 1);
|
---|
59 | j = j >= 0 ? j : notes.length();
|
---|
60 | JOptionPane.showMessageDialog(Main.parent, notes.substring(0, j));
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|