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.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.io.StringReader;
|
---|
10 | import java.nio.charset.StandardCharsets;
|
---|
11 |
|
---|
12 | import javax.xml.parsers.ParserConfigurationException;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.data.osm.ChangesetDataSet;
|
---|
16 | import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetModificationType;
|
---|
17 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
18 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
20 | import org.openstreetmap.josm.tools.Utils;
|
---|
21 | import org.openstreetmap.josm.tools.XmlParsingException;
|
---|
22 | import org.xml.sax.Attributes;
|
---|
23 | import org.xml.sax.InputSource;
|
---|
24 | import org.xml.sax.SAXException;
|
---|
25 | import org.xml.sax.SAXParseException;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Parser for OSM changeset content.
|
---|
29 | * @since 2688
|
---|
30 | */
|
---|
31 | public class OsmChangesetContentParser {
|
---|
32 |
|
---|
33 | private final InputSource source;
|
---|
34 | private final ChangesetDataSet data = new ChangesetDataSet();
|
---|
35 |
|
---|
36 | private class Parser extends AbstractParser {
|
---|
37 |
|
---|
38 | /** the current change modification type */
|
---|
39 | private ChangesetDataSet.ChangesetModificationType currentModificationType;
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | protected void throwException(String message) throws XmlParsingException {
|
---|
43 | throw new XmlParsingException(message).rememberLocation(locator);
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | protected void throwException(String message, Exception e) throws XmlParsingException {
|
---|
48 | throw new XmlParsingException(message, e).rememberLocation(locator);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
---|
53 | if (super.doStartElement(qName, atts)) {
|
---|
54 | // done
|
---|
55 | return;
|
---|
56 | }
|
---|
57 | switch (qName) {
|
---|
58 | case "osmChange":
|
---|
59 | // do nothing
|
---|
60 | break;
|
---|
61 | case "create":
|
---|
62 | currentModificationType = ChangesetModificationType.CREATED;
|
---|
63 | break;
|
---|
64 | case "modify":
|
---|
65 | currentModificationType = ChangesetModificationType.UPDATED;
|
---|
66 | break;
|
---|
67 | case "delete":
|
---|
68 | currentModificationType = ChangesetModificationType.DELETED;
|
---|
69 | break;
|
---|
70 | default:
|
---|
71 | Main.warn(tr("Unsupported start element ''{0}'' in changeset content at position ({1},{2}). Skipping.",
|
---|
72 | qName, locator.getLineNumber(), locator.getColumnNumber()));
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public void endElement(String uri, String localName, String qName) throws SAXException {
|
---|
78 | switch (qName) {
|
---|
79 | case "node":
|
---|
80 | case "way":
|
---|
81 | case "relation":
|
---|
82 | if (currentModificationType == null) {
|
---|
83 | // CHECKSTYLE.OFF: LineLength
|
---|
84 | throwException(tr("Illegal document structure. Found node, way, or relation outside of ''create'', ''modify'', or ''delete''."));
|
---|
85 | // CHECKSTYLE.ON: LineLength
|
---|
86 | }
|
---|
87 | data.put(currentPrimitive, currentModificationType);
|
---|
88 | break;
|
---|
89 | case "create":
|
---|
90 | case "modify":
|
---|
91 | case "delete":
|
---|
92 | currentModificationType = null;
|
---|
93 | break;
|
---|
94 | case "osmChange":
|
---|
95 | case "tag":
|
---|
96 | case "nd":
|
---|
97 | case "member":
|
---|
98 | // do nothing
|
---|
99 | break;
|
---|
100 | default:
|
---|
101 | Main.warn(tr("Unsupported end element ''{0}'' in changeset content at position ({1},{2}). Skipping.",
|
---|
102 | qName, locator.getLineNumber(), locator.getColumnNumber()));
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public void error(SAXParseException e) throws SAXException {
|
---|
108 | throwException(null, e);
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public void fatalError(SAXParseException e) throws SAXException {
|
---|
113 | throwException(null, e);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Constructs a new {@code OsmChangesetContentParser}.
|
---|
119 | *
|
---|
120 | * @param source the input stream with the changeset content as XML document. Must not be null.
|
---|
121 | * @throws IllegalArgumentException if source is {@code null}.
|
---|
122 | */
|
---|
123 | @SuppressWarnings("resource")
|
---|
124 | public OsmChangesetContentParser(InputStream source) {
|
---|
125 | CheckParameterUtil.ensureParameterNotNull(source, "source");
|
---|
126 | this.source = new InputSource(new InputStreamReader(source, StandardCharsets.UTF_8));
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Constructs a new {@code OsmChangesetContentParser}.
|
---|
131 | *
|
---|
132 | * @param source the input stream with the changeset content as XML document. Must not be null.
|
---|
133 | * @throws IllegalArgumentException if source is {@code null}.
|
---|
134 | */
|
---|
135 | public OsmChangesetContentParser(String source) {
|
---|
136 | CheckParameterUtil.ensureParameterNotNull(source, "source");
|
---|
137 | this.source = new InputSource(new StringReader(source));
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * Parses the content.
|
---|
142 | *
|
---|
143 | * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
144 | * @return the parsed data
|
---|
145 | * @throws XmlParsingException if something went wrong. Check for chained
|
---|
146 | * exceptions.
|
---|
147 | */
|
---|
148 | public ChangesetDataSet parse(ProgressMonitor progressMonitor) throws XmlParsingException {
|
---|
149 | if (progressMonitor == null) {
|
---|
150 | progressMonitor = NullProgressMonitor.INSTANCE;
|
---|
151 | }
|
---|
152 | try {
|
---|
153 | progressMonitor.beginTask("");
|
---|
154 | progressMonitor.indeterminateSubTask(tr("Parsing changeset content ..."));
|
---|
155 | Utils.parseSafeSAX(source, new Parser());
|
---|
156 | } catch (XmlParsingException e) {
|
---|
157 | throw e;
|
---|
158 | } catch (ParserConfigurationException | SAXException | IOException e) {
|
---|
159 | throw new XmlParsingException(e);
|
---|
160 | } finally {
|
---|
161 | progressMonitor.finishTask();
|
---|
162 | }
|
---|
163 | return data;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Parses the content from the input source
|
---|
168 | *
|
---|
169 | * @return the parsed data
|
---|
170 | * @throws XmlParsingException if something went wrong. Check for chained
|
---|
171 | * exceptions.
|
---|
172 | */
|
---|
173 | public ChangesetDataSet parse() throws XmlParsingException {
|
---|
174 | return parse(null);
|
---|
175 | }
|
---|
176 | }
|
---|