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

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

refactor duplicated code - impacts some plugins (reverter, roadsigns, cmdline)

  • 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;
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 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
50 if (super.doStartElement(qName, atts)) {
51 // done
52 } else if (qName.equals("osmChange")) {
53 // do nothing
54 } else if (qName.equals("create")) {
55 currentModificationType = ChangesetModificationType.CREATED;
56 } else if (qName.equals("modify")) {
57 currentModificationType = ChangesetModificationType.UPDATED;
58 } else if (qName.equals("delete")) {
59 currentModificationType = ChangesetModificationType.DELETED;
60 } else {
61 Main.warn(tr("Unsupported start element ''{0}'' in changeset content at position ({1},{2}). Skipping.", qName, locator.getLineNumber(), locator.getColumnNumber()));
62 }
63 }
64
65 @Override
66 public void endElement(String uri, String localName, String qName) throws SAXException {
67 if (qName.equals("node")
68 || qName.equals("way")
69 || qName.equals("relation")) {
70 if (currentModificationType == null) {
71 throwException(tr("Illegal document structure. Found node, way, or relation outside of ''create'', ''modify'', or ''delete''."));
72 }
73 data.put(currentPrimitive, currentModificationType);
74 } else if (qName.equals("osmChange")) {
75 // do nothing
76 } else if (qName.equals("create")) {
77 currentModificationType = null;
78 } else if (qName.equals("modify")) {
79 currentModificationType = null;
80 } else if (qName.equals("delete")) {
81 currentModificationType = null;
82 } else if (qName.equals("tag")) {
83 // do nothing
84 } else if (qName.equals("nd")) {
85 // do nothing
86 } else if (qName.equals("member")) {
87 // do nothing
88 } else {
89 Main.warn(tr("Unsupported end element ''{0}'' in changeset content at position ({1},{2}). Skipping.", qName, locator.getLineNumber(), locator.getColumnNumber()));
90 }
91 }
92
93 @Override
94 public void error(SAXParseException e) throws SAXException {
95 throwException(e);
96 }
97
98 @Override
99 public void fatalError(SAXParseException e) throws SAXException {
100 throwException(e);
101 }
102 }
103
104 /**
105 * Constructs a new {@code OsmChangesetContentParser}.
106 *
107 * @param source the input stream with the changeset content as XML document. Must not be null.
108 * @throws IllegalArgumentException if source is {@code null}.
109 */
110 public OsmChangesetContentParser(InputStream source) {
111 CheckParameterUtil.ensureParameterNotNull(source, "source");
112 this.source = new InputSource(new InputStreamReader(source, Utils.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 XmlParsingException if something went wrong. Check for chained
132 * exceptions.
133 */
134 public ChangesetDataSet parse(ProgressMonitor progressMonitor) throws XmlParsingException {
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(XmlParsingException e) {
143 throw e;
144 } catch (ParserConfigurationException e) {
145 throw new XmlParsingException(e);
146 } catch(SAXException e) {
147 throw new XmlParsingException(e);
148 } catch(IOException e) {
149 throw new XmlParsingException(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 XmlParsingException if something went wrong. Check for chained
161 * exceptions.
162 */
163 public ChangesetDataSet parse() throws XmlParsingException {
164 return parse(null);
165 }
166}
Note: See TracBrowser for help on using the repository browser.