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

Last change on this file since 7070 was 7033, checked in by Don-vip, 10 years ago

see #8465 - global use of try-with-resources, according to

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