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.io.InputStream;
|
---|
7 | import java.util.Arrays;
|
---|
8 |
|
---|
9 | import javax.xml.stream.XMLStreamConstants;
|
---|
10 | import javax.xml.stream.XMLStreamException;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Reader for <a href="http://wiki.openstreetmap.org/wiki/OsmChange">OsmChange</a> file format.
|
---|
18 | */
|
---|
19 | public class OsmChangeReader extends OsmReader {
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * List of possible actions.
|
---|
23 | */
|
---|
24 | private static final String[] ACTIONS = {"create", "modify", "delete"};
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * constructor (for private and subclasses use only)
|
---|
28 | *
|
---|
29 | * @see #parseDataSet(InputStream, ProgressMonitor)
|
---|
30 | */
|
---|
31 | protected OsmChangeReader() {
|
---|
32 | // Restricts visibility
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | protected void parseRoot() throws XMLStreamException {
|
---|
37 | if ("osmChange".equals(parser.getLocalName())) {
|
---|
38 | parseOsmChange();
|
---|
39 | } else {
|
---|
40 | parseUnknown();
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | private void parseOsmChange() throws XMLStreamException {
|
---|
45 | String v = parser.getAttributeValue(null, "version");
|
---|
46 | if (v == null) {
|
---|
47 | throwException(tr("Missing mandatory attribute ''{0}''.", "version"));
|
---|
48 | }
|
---|
49 | if (!"0.6".equals(v)) {
|
---|
50 | throwException(tr("Unsupported version: {0}", v));
|
---|
51 | }
|
---|
52 | ds.setVersion(v);
|
---|
53 | while (parser.hasNext()) {
|
---|
54 | int event = parser.next();
|
---|
55 | if (event == XMLStreamConstants.START_ELEMENT) {
|
---|
56 | if (Arrays.asList(ACTIONS).contains(parser.getLocalName())) {
|
---|
57 | parseCommon(parser.getLocalName());
|
---|
58 | } else {
|
---|
59 | parseUnknown();
|
---|
60 | }
|
---|
61 | } else if (event == XMLStreamConstants.END_ELEMENT) {
|
---|
62 | return;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | private void parseCommon(String action) throws XMLStreamException {
|
---|
68 | while (parser.hasNext()) {
|
---|
69 | int event = parser.next();
|
---|
70 | if (event == XMLStreamConstants.START_ELEMENT) {
|
---|
71 | OsmPrimitive p = null;
|
---|
72 | switch (parser.getLocalName()) {
|
---|
73 | case "node":
|
---|
74 | p = parseNode();
|
---|
75 | break;
|
---|
76 | case "way":
|
---|
77 | p = parseWay();
|
---|
78 | break;
|
---|
79 | case "relation":
|
---|
80 | p = parseRelation();
|
---|
81 | break;
|
---|
82 | default:
|
---|
83 | parseUnknown();
|
---|
84 | }
|
---|
85 | if (p != null && action != null) {
|
---|
86 | if ("modify".equals(action)) {
|
---|
87 | p.setModified(true);
|
---|
88 | } else if ("delete".equals(action)) {
|
---|
89 | p.setDeleted(true);
|
---|
90 | }
|
---|
91 | }
|
---|
92 | } else if (event == XMLStreamConstants.END_ELEMENT) {
|
---|
93 | return;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Parse the given input source and return the dataset.
|
---|
100 | *
|
---|
101 | * @param source the source input stream. Must not be <code>null</code>.
|
---|
102 | * @param progressMonitor the progress monitor. If <code>null</code>,
|
---|
103 | * {@link org.openstreetmap.josm.gui.progress.NullProgressMonitor#INSTANCE} is assumed
|
---|
104 | *
|
---|
105 | * @return the dataset with the parsed data
|
---|
106 | * @throws IllegalDataException if the an error was found while parsing the data from the source
|
---|
107 | * @throws IllegalArgumentException if source is <code>null</code>
|
---|
108 | */
|
---|
109 | public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
|
---|
110 | return new OsmChangeReader().doParseDataSet(source, progressMonitor);
|
---|
111 | }
|
---|
112 | }
|
---|