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