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.coor.LatLon;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
14 | import org.openstreetmap.josm.data.osm.NoteData;
|
---|
15 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
17 | import org.openstreetmap.josm.tools.Pair;
|
---|
18 | import org.openstreetmap.josm.tools.Utils;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Reader for <a href="http://wiki.openstreetmap.org/wiki/OsmChange">OsmChange</a> file format.
|
---|
22 | */
|
---|
23 | public class OsmChangeReader extends OsmReader {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * List of possible actions.
|
---|
27 | */
|
---|
28 | private static final String[] ACTIONS = {"create", "modify", "delete"};
|
---|
29 |
|
---|
30 | protected final NoteData noteData = new NoteData();
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * constructor (for private and subclasses use only)
|
---|
34 | *
|
---|
35 | * @see #parseDataSet(InputStream, ProgressMonitor)
|
---|
36 | */
|
---|
37 | protected OsmChangeReader() {
|
---|
38 | // Restricts visibility
|
---|
39 | }
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | protected void parseRoot() throws XMLStreamException {
|
---|
43 | if ("osmChange".equals(parser.getLocalName())) {
|
---|
44 | parseOsmChange();
|
---|
45 | } else {
|
---|
46 | parseUnknown();
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | private void parseOsmChange() throws XMLStreamException {
|
---|
51 | String v = parser.getAttributeValue(null, "version");
|
---|
52 | if (v == null) {
|
---|
53 | throwException(tr("Missing mandatory attribute ''{0}''.", "version"));
|
---|
54 | }
|
---|
55 | if (!"0.6".equals(v)) {
|
---|
56 | throwException(tr("Unsupported version: {0}", v));
|
---|
57 | }
|
---|
58 | ds.setVersion(v);
|
---|
59 | while (parser.hasNext()) {
|
---|
60 | int event = parser.next();
|
---|
61 | if (event == XMLStreamConstants.START_ELEMENT) {
|
---|
62 | if (Arrays.asList(ACTIONS).contains(parser.getLocalName())) {
|
---|
63 | parseCommon(parser.getLocalName());
|
---|
64 | } else {
|
---|
65 | parseUnknown();
|
---|
66 | }
|
---|
67 | } else if (event == XMLStreamConstants.END_ELEMENT) {
|
---|
68 | return;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | private void parseCommon(String action) throws XMLStreamException {
|
---|
74 | while (parser.hasNext()) {
|
---|
75 | int event = parser.next();
|
---|
76 | if (event == XMLStreamConstants.START_ELEMENT) {
|
---|
77 | OsmPrimitive p = null;
|
---|
78 | switch (parser.getLocalName()) {
|
---|
79 | case "node":
|
---|
80 | p = parseNode();
|
---|
81 | break;
|
---|
82 | case "way":
|
---|
83 | p = parseWay();
|
---|
84 | break;
|
---|
85 | case "relation":
|
---|
86 | p = parseRelation();
|
---|
87 | break;
|
---|
88 | case "note":
|
---|
89 | parseNote();
|
---|
90 | break;
|
---|
91 | default:
|
---|
92 | parseUnknown();
|
---|
93 | }
|
---|
94 | if (p != null && action != null) {
|
---|
95 | if ("modify".equals(action)) {
|
---|
96 | p.setModified(true);
|
---|
97 | } else if ("delete".equals(action)) {
|
---|
98 | p.setDeleted(true);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | } else if (event == XMLStreamConstants.END_ELEMENT) {
|
---|
102 | return;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | private void parseNote() throws XMLStreamException {
|
---|
108 | LatLon location = NoteReader.parseLatLon(s -> parser.getAttributeValue(null, s));
|
---|
109 | String text = null;
|
---|
110 | while (parser.hasNext()) {
|
---|
111 | int event = parser.next();
|
---|
112 | if (event == XMLStreamConstants.START_ELEMENT) {
|
---|
113 | switch (parser.getLocalName()) {
|
---|
114 | case "comment":
|
---|
115 | text = parser.getAttributeValue(null, "text");
|
---|
116 | jumpToEnd();
|
---|
117 | break;
|
---|
118 | default:
|
---|
119 | parseUnknown();
|
---|
120 | }
|
---|
121 | } else if (event == XMLStreamConstants.END_ELEMENT) {
|
---|
122 | break;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | if (location != null && !Utils.isEmpty(text)) {
|
---|
126 | noteData.createNote(location, text);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Replies the parsed notes data.
|
---|
132 | * @return the parsed notes data
|
---|
133 | * @since 14101
|
---|
134 | */
|
---|
135 | public final NoteData getNoteData() {
|
---|
136 | return noteData;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Parse the given input source and return the dataset.
|
---|
141 | *
|
---|
142 | * @param source the source input stream. Must not be <code>null</code>.
|
---|
143 | * @param progressMonitor the progress monitor. If <code>null</code>,
|
---|
144 | * {@link org.openstreetmap.josm.gui.progress.NullProgressMonitor#INSTANCE} is assumed
|
---|
145 | *
|
---|
146 | * @return the dataset with the parsed data
|
---|
147 | * @throws IllegalDataException if the an error was found while parsing the data from the source
|
---|
148 | * @throws IllegalArgumentException if source is <code>null</code>
|
---|
149 | */
|
---|
150 | public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
|
---|
151 | return new OsmChangeReader().doParseDataSet(source, progressMonitor);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Parse the given input source and return the dataset and notes, if any (OsmAnd extends the osmChange format by adding notes).
|
---|
156 | *
|
---|
157 | * @param source the source input stream. Must not be <code>null</code>.
|
---|
158 | * @param progressMonitor the progress monitor. If <code>null</code>,
|
---|
159 | * {@link org.openstreetmap.josm.gui.progress.NullProgressMonitor#INSTANCE} is assumed
|
---|
160 | *
|
---|
161 | * @return the dataset with the parsed data
|
---|
162 | * @throws IllegalDataException if the an error was found while parsing the data from the source
|
---|
163 | * @throws IllegalArgumentException if source is <code>null</code>
|
---|
164 | * @since 14101
|
---|
165 | */
|
---|
166 | public static Pair<DataSet, NoteData> parseDataSetAndNotes(InputStream source, ProgressMonitor progressMonitor)
|
---|
167 | throws IllegalDataException {
|
---|
168 | OsmChangeReader osmChangeReader = new OsmChangeReader();
|
---|
169 | osmChangeReader.doParseDataSet(source, progressMonitor);
|
---|
170 | return new Pair<>(osmChangeReader.getDataSet(), osmChangeReader.getNoteData());
|
---|
171 | }
|
---|
172 | }
|
---|