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

Last change on this file since 11343 was 9078, checked in by Don-vip, 8 years ago

sonar - Immutable Field

  • Property svn:eol-style set to native
File size: 6.3 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;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.data.osm.ChangesetDataSet;
16import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetModificationType;
17import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20import org.openstreetmap.josm.tools.Utils;
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 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}
Note: See TracBrowser for help on using the repository browser.