source: josm/trunk/src/org/openstreetmap/josm/io/OsmChangesetContentParser.java@ 7801

Last change on this file since 7801 was 7801, checked in by Don-vip, 9 years ago

fix some Sonar issues

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