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.io.InputStreamReader;
|
---|
8 | import java.nio.charset.StandardCharsets;
|
---|
9 | import java.text.MessageFormat;
|
---|
10 | import java.util.Date;
|
---|
11 | import java.util.LinkedList;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import javax.xml.parsers.ParserConfigurationException;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
17 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
18 | import org.openstreetmap.josm.data.osm.ChangesetDiscussionComment;
|
---|
19 | import org.openstreetmap.josm.data.osm.User;
|
---|
20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
21 | import org.openstreetmap.josm.tools.Utils;
|
---|
22 | import org.openstreetmap.josm.tools.XmlParsingException;
|
---|
23 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
24 | import org.xml.sax.Attributes;
|
---|
25 | import org.xml.sax.InputSource;
|
---|
26 | import org.xml.sax.Locator;
|
---|
27 | import org.xml.sax.SAXException;
|
---|
28 | import org.xml.sax.helpers.DefaultHandler;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Parser for a list of changesets, encapsulated in an OSM data set structure.
|
---|
32 | * Example:
|
---|
33 | * <pre>
|
---|
34 | * <osm version="0.6" generator="OpenStreetMap server">
|
---|
35 | * <changeset id="143" user="guggis" uid="1" created_at="2009-09-08T20:35:39Z" closed_at="2009-09-08T21:36:12Z" open="false" min_lon="7.380925" min_lat="46.9215164" max_lon="7.3984718" max_lat="46.9226502">
|
---|
36 | * <tag k="asdfasdf" v="asdfasdf"/>
|
---|
37 | * <tag k="created_by" v="JOSM/1.5 (UNKNOWN de)"/>
|
---|
38 | * <tag k="comment" v="1234"/>
|
---|
39 | * </changeset>
|
---|
40 | * </osm>
|
---|
41 | * </pre>
|
---|
42 | *
|
---|
43 | */
|
---|
44 | public final class OsmChangesetParser {
|
---|
45 | private final List<Changeset> changesets;
|
---|
46 |
|
---|
47 | private OsmChangesetParser() {
|
---|
48 | changesets = new LinkedList<>();
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Returns the parsed changesets.
|
---|
53 | * @return the parsed changesets
|
---|
54 | */
|
---|
55 | public List<Changeset> getChangesets() {
|
---|
56 | return changesets;
|
---|
57 | }
|
---|
58 |
|
---|
59 | private class Parser extends DefaultHandler {
|
---|
60 | private Locator locator;
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public void setDocumentLocator(Locator locator) {
|
---|
64 | this.locator = locator;
|
---|
65 | }
|
---|
66 |
|
---|
67 | protected void throwException(String msg) throws XmlParsingException {
|
---|
68 | throw new XmlParsingException(msg).rememberLocation(locator);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** The current changeset */
|
---|
72 | private Changeset current = null;
|
---|
73 |
|
---|
74 | /** The current comment */
|
---|
75 | private ChangesetDiscussionComment comment = null;
|
---|
76 |
|
---|
77 | /** The current comment text */
|
---|
78 | private StringBuilder text = null;
|
---|
79 |
|
---|
80 | protected void parseChangesetAttributes(Changeset cs, Attributes atts) throws XmlParsingException {
|
---|
81 | // -- id
|
---|
82 | String value = atts.getValue("id");
|
---|
83 | if (value == null) {
|
---|
84 | throwException(tr("Missing mandatory attribute ''{0}''.", "id"));
|
---|
85 | }
|
---|
86 | current.setId(parseNumericAttribute(value, 1));
|
---|
87 |
|
---|
88 | // -- user / uid
|
---|
89 | current.setUser(createUser(atts));
|
---|
90 |
|
---|
91 | // -- created_at
|
---|
92 | value = atts.getValue("created_at");
|
---|
93 | if (value == null) {
|
---|
94 | current.setCreatedAt(null);
|
---|
95 | } else {
|
---|
96 | current.setCreatedAt(DateUtils.fromString(value));
|
---|
97 | }
|
---|
98 |
|
---|
99 | // -- closed_at
|
---|
100 | value = atts.getValue("closed_at");
|
---|
101 | if (value == null) {
|
---|
102 | current.setClosedAt(null);
|
---|
103 | } else {
|
---|
104 | current.setClosedAt(DateUtils.fromString(value));
|
---|
105 | }
|
---|
106 |
|
---|
107 | // -- open
|
---|
108 | value = atts.getValue("open");
|
---|
109 | if (value == null) {
|
---|
110 | throwException(tr("Missing mandatory attribute ''{0}''.", "open"));
|
---|
111 | } else if ("true".equals(value)) {
|
---|
112 | current.setOpen(true);
|
---|
113 | } else if ("false".equals(value)) {
|
---|
114 | current.setOpen(false);
|
---|
115 | } else {
|
---|
116 | throwException(tr("Illegal boolean value for attribute ''{0}''. Got ''{1}''.", "open", value));
|
---|
117 | }
|
---|
118 |
|
---|
119 | // -- min_lon and min_lat
|
---|
120 | String min_lon = atts.getValue("min_lon");
|
---|
121 | String min_lat = atts.getValue("min_lat");
|
---|
122 | String max_lon = atts.getValue("max_lon");
|
---|
123 | String max_lat = atts.getValue("max_lat");
|
---|
124 | if (min_lon != null && min_lat != null && max_lon != null && max_lat != null) {
|
---|
125 | double minLon = 0;
|
---|
126 | try {
|
---|
127 | minLon = Double.parseDouble(min_lon);
|
---|
128 | } catch(NumberFormatException e) {
|
---|
129 | throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "min_lon", min_lon));
|
---|
130 | }
|
---|
131 | double minLat = 0;
|
---|
132 | try {
|
---|
133 | minLat = Double.parseDouble(min_lat);
|
---|
134 | } catch(NumberFormatException e) {
|
---|
135 | throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "min_lat", min_lat));
|
---|
136 | }
|
---|
137 | current.setMin(new LatLon(minLat, minLon));
|
---|
138 |
|
---|
139 | // -- max_lon and max_lat
|
---|
140 |
|
---|
141 | double maxLon = 0;
|
---|
142 | try {
|
---|
143 | maxLon = Double.parseDouble(max_lon);
|
---|
144 | } catch(NumberFormatException e) {
|
---|
145 | throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "max_lon", max_lon));
|
---|
146 | }
|
---|
147 | double maxLat = 0;
|
---|
148 | try {
|
---|
149 | maxLat = Double.parseDouble(max_lat);
|
---|
150 | } catch(NumberFormatException e) {
|
---|
151 | throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "max_lat", max_lat));
|
---|
152 | }
|
---|
153 | current.setMax(new LatLon(maxLon, maxLat));
|
---|
154 | }
|
---|
155 |
|
---|
156 | // -- comments_count
|
---|
157 | String commentsCount = atts.getValue("comments_count");
|
---|
158 | if (commentsCount != null) {
|
---|
159 | current.setCommentsCount(parseNumericAttribute(commentsCount, 0));
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | private void parseCommentAttributes(Attributes atts) throws XmlParsingException {
|
---|
164 | // -- date
|
---|
165 | String value = atts.getValue("date");
|
---|
166 | Date date = null;
|
---|
167 | if (value != null) {
|
---|
168 | date = DateUtils.fromString(value);
|
---|
169 | }
|
---|
170 |
|
---|
171 | comment = new ChangesetDiscussionComment(date, createUser(atts));
|
---|
172 | }
|
---|
173 |
|
---|
174 | private int parseNumericAttribute(String value, int minAllowed) throws XmlParsingException {
|
---|
175 | int att = 0;
|
---|
176 | try {
|
---|
177 | att = Integer.parseInt(value);
|
---|
178 | } catch(NumberFormatException e) {
|
---|
179 | throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "id", value));
|
---|
180 | }
|
---|
181 | if (att < minAllowed) {
|
---|
182 | throwException(tr("Illegal numeric value for attribute ''{0}''. Got ''{1}''.", "id", att));
|
---|
183 | }
|
---|
184 | return att;
|
---|
185 | }
|
---|
186 |
|
---|
187 | @Override
|
---|
188 | public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
---|
189 | switch (qName) {
|
---|
190 | case "osm":
|
---|
191 | if (atts == null) {
|
---|
192 | throwException(tr("Missing mandatory attribute ''{0}'' of XML element {1}.", "version", "osm"));
|
---|
193 | return;
|
---|
194 | }
|
---|
195 | String v = atts.getValue("version");
|
---|
196 | if (v == null) {
|
---|
197 | throwException(tr("Missing mandatory attribute ''{0}''.", "version"));
|
---|
198 | }
|
---|
199 | if (!("0.6".equals(v))) {
|
---|
200 | throwException(tr("Unsupported version: {0}", v));
|
---|
201 | }
|
---|
202 | break;
|
---|
203 | case "changeset":
|
---|
204 | current = new Changeset();
|
---|
205 | parseChangesetAttributes(current, atts);
|
---|
206 | break;
|
---|
207 | case "tag":
|
---|
208 | String key = atts.getValue("k");
|
---|
209 | String value = atts.getValue("v");
|
---|
210 | current.put(key, value);
|
---|
211 | break;
|
---|
212 | case "discussion":
|
---|
213 | break;
|
---|
214 | case "comment":
|
---|
215 | parseCommentAttributes(atts);
|
---|
216 | break;
|
---|
217 | case "text":
|
---|
218 | text = new StringBuilder();
|
---|
219 | break;
|
---|
220 | default:
|
---|
221 | throwException(tr("Undefined element ''{0}'' found in input stream. Aborting.", qName));
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | @Override
|
---|
226 | public void characters(char[] ch, int start, int length) throws SAXException {
|
---|
227 | if (text != null) {
|
---|
228 | text.append(ch, start, length);
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | @Override
|
---|
233 | public void endElement(String uri, String localName, String qName) throws SAXException {
|
---|
234 | if ("changeset".equals(qName)) {
|
---|
235 | changesets.add(current);
|
---|
236 | current = null;
|
---|
237 | } else if ("comment".equals(qName)) {
|
---|
238 | current.addDiscussionComment(comment);
|
---|
239 | comment = null;
|
---|
240 | } else if ("text".equals(qName)) {
|
---|
241 | comment.setText(text.toString());
|
---|
242 | text = null;
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | protected User createUser(Attributes atts) throws XmlParsingException {
|
---|
247 | String name = atts.getValue("user");
|
---|
248 | String uid = atts.getValue("uid");
|
---|
249 | if (uid == null) {
|
---|
250 | if (name == null)
|
---|
251 | return null;
|
---|
252 | return User.createLocalUser(name);
|
---|
253 | }
|
---|
254 | try {
|
---|
255 | long id = Long.parseLong(uid);
|
---|
256 | return User.createOsmUser(id, name);
|
---|
257 | } catch(NumberFormatException e) {
|
---|
258 | throwException(MessageFormat.format("Illegal value for attribute ''uid''. Got ''{0}''.", uid));
|
---|
259 | }
|
---|
260 | return null;
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Parse the given input source and return the list of changesets
|
---|
266 | *
|
---|
267 | * @param source the source input stream
|
---|
268 | * @param progressMonitor the progress monitor
|
---|
269 | *
|
---|
270 | * @return the list of changesets
|
---|
271 | * @throws IllegalDataException if the an error was found while parsing the data from the source
|
---|
272 | */
|
---|
273 | @SuppressWarnings("resource")
|
---|
274 | public static List<Changeset> parse(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
|
---|
275 | OsmChangesetParser parser = new OsmChangesetParser();
|
---|
276 | try {
|
---|
277 | progressMonitor.beginTask("");
|
---|
278 | progressMonitor.indeterminateSubTask(tr("Parsing list of changesets..."));
|
---|
279 | InputSource inputSource = new InputSource(new InvalidXmlCharacterFilter(new InputStreamReader(source, StandardCharsets.UTF_8)));
|
---|
280 | Utils.parseSafeSAX(inputSource, parser.new Parser());
|
---|
281 | return parser.getChangesets();
|
---|
282 | } catch(ParserConfigurationException | SAXException e) {
|
---|
283 | throw new IllegalDataException(e.getMessage(), e);
|
---|
284 | } catch(Exception e) {
|
---|
285 | throw new IllegalDataException(e);
|
---|
286 | } finally {
|
---|
287 | progressMonitor.finishTask();
|
---|
288 | }
|
---|
289 | }
|
---|
290 | }
|
---|