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