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

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

see #8465 - replace Utils.UTF_8 by StandardCharsets.UTF_8, new in Java 7

  • Property svn:eol-style set to native
File size: 6.4 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 "osmChange":
87 // do nothing
88 break;
89 case "create":
90 currentModificationType = null;
91 break;
92 case "modify":
93 currentModificationType = null;
94 break;
95 case "delete":
96 currentModificationType = null;
97 break;
98 case "tag":
99 case "nd":
100 case "member":
101 // do nothing
102 break;
103 default:
104 Main.warn(tr("Unsupported end element ''{0}'' in changeset content at position ({1},{2}). Skipping.",
105 qName, locator.getLineNumber(), locator.getColumnNumber()));
106 }
107 }
108
109 @Override
110 public void error(SAXParseException e) throws SAXException {
111 throwException(e);
112 }
113
114 @Override
115 public void fatalError(SAXParseException e) throws SAXException {
116 throwException(e);
117 }
118 }
119
120 /**
121 * Constructs a new {@code OsmChangesetContentParser}.
122 *
123 * @param source the input stream with the changeset content as XML document. Must not be null.
124 * @throws IllegalArgumentException if source is {@code null}.
125 */
126 @SuppressWarnings("resource")
127 public OsmChangesetContentParser(InputStream source) {
128 CheckParameterUtil.ensureParameterNotNull(source, "source");
129 this.source = new InputSource(new InputStreamReader(source, StandardCharsets.UTF_8));
130 }
131
132 /**
133 * Constructs a new {@code OsmChangesetContentParser}.
134 *
135 * @param source the input stream with the changeset content as XML document. Must not be null.
136 * @throws IllegalArgumentException if source is {@code null}.
137 */
138 public OsmChangesetContentParser(String source) {
139 CheckParameterUtil.ensureParameterNotNull(source, "source");
140 this.source = new InputSource(new StringReader(source));
141 }
142
143 /**
144 * Parses the content.
145 *
146 * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
147 * @return the parsed data
148 * @throws XmlParsingException if something went wrong. Check for chained
149 * exceptions.
150 */
151 public ChangesetDataSet parse(ProgressMonitor progressMonitor) throws XmlParsingException {
152 if (progressMonitor == null) {
153 progressMonitor = NullProgressMonitor.INSTANCE;
154 }
155 try {
156 progressMonitor.beginTask("");
157 progressMonitor.indeterminateSubTask(tr("Parsing changeset content ..."));
158 SAXParserFactory.newInstance().newSAXParser().parse(source, new Parser());
159 } catch(XmlParsingException e) {
160 throw e;
161 } catch (ParserConfigurationException | SAXException | IOException e) {
162 throw new XmlParsingException(e);
163 } finally {
164 progressMonitor.finishTask();
165 }
166 return data;
167 }
168
169 /**
170 * Parses the content from the input source
171 *
172 * @return the parsed data
173 * @throws XmlParsingException if something went wrong. Check for chained
174 * exceptions.
175 */
176 public ChangesetDataSet parse() throws XmlParsingException {
177 return parse(null);
178 }
179}
Note: See TracBrowser for help on using the repository browser.