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

Last change on this file since 6887 was 6552, checked in by simon04, 10 years ago

Refactoring: introduce Utils.UTF_8 charset to avoid handling of UnsupportedEncodingException

According to the Javadoc of Charset, every implementation of the Java
platform is required to support UTF-8.

  • 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.xml.sax.Attributes;
22import org.xml.sax.InputSource;
23import org.xml.sax.SAXException;
24import org.xml.sax.SAXParseException;
25
26/**
27 * Parser for OSM changeset content.
28 * @since 2688
29 */
30public class OsmChangesetContentParser {
31
32 private InputSource source;
33 private final ChangesetDataSet data = new ChangesetDataSet();
34
35 private class Parser extends AbstractParser {
36
37 /** the current change modification type */
38 private ChangesetDataSet.ChangesetModificationType currentModificationType;
39
40 protected void throwException(String message) throws OsmDataParsingException {
41 throw new OsmDataParsingException(message).rememberLocation(locator);
42 }
43
44 protected void throwException(Exception e) throws OsmDataParsingException {
45 throw new OsmDataParsingException(e).rememberLocation(locator);
46 }
47
48 @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
49 if (super.doStartElement(qName, atts)) {
50 // done
51 } else if (qName.equals("osmChange")) {
52 // do nothing
53 } else if (qName.equals("create")) {
54 currentModificationType = ChangesetModificationType.CREATED;
55 } else if (qName.equals("modify")) {
56 currentModificationType = ChangesetModificationType.UPDATED;
57 } else if (qName.equals("delete")) {
58 currentModificationType = ChangesetModificationType.DELETED;
59 } else {
60 Main.warn(tr("Unsupported start element ''{0}'' in changeset content at position ({1},{2}). Skipping.", qName, locator.getLineNumber(), locator.getColumnNumber()));
61 }
62 }
63
64 @Override
65 public void endElement(String uri, String localName, String qName) throws SAXException {
66 if (qName.equals("node")
67 || qName.equals("way")
68 || qName.equals("relation")) {
69 if (currentModificationType == null) {
70 throwException(tr("Illegal document structure. Found node, way, or relation outside of ''create'', ''modify'', or ''delete''."));
71 }
72 data.put(currentPrimitive, currentModificationType);
73 } else if (qName.equals("osmChange")) {
74 // do nothing
75 } else if (qName.equals("create")) {
76 currentModificationType = null;
77 } else if (qName.equals("modify")) {
78 currentModificationType = null;
79 } else if (qName.equals("delete")) {
80 currentModificationType = null;
81 } else if (qName.equals("tag")) {
82 // do nothing
83 } else if (qName.equals("nd")) {
84 // do nothing
85 } else if (qName.equals("member")) {
86 // do nothing
87 } else {
88 Main.warn(tr("Unsupported end element ''{0}'' in changeset content at position ({1},{2}). Skipping.", qName, locator.getLineNumber(), locator.getColumnNumber()));
89 }
90 }
91
92 @Override
93 public void error(SAXParseException e) throws SAXException {
94 throwException(e);
95 }
96
97 @Override
98 public void fatalError(SAXParseException e) throws SAXException {
99 throwException(e);
100 }
101 }
102
103 /**
104 * Constructs a new {@code OsmChangesetContentParser}.
105 *
106 * @param source the input stream with the changeset content as XML document. Must not be null.
107 * @throws IllegalArgumentException if source is {@code null}.
108 */
109 public OsmChangesetContentParser(InputStream source) {
110 CheckParameterUtil.ensureParameterNotNull(source, "source");
111 this.source = new InputSource(new InputStreamReader(source, Utils.UTF_8));
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 public OsmChangesetContentParser(String source) {
121 CheckParameterUtil.ensureParameterNotNull(source, "source");
122 this.source = new InputSource(new StringReader(source));
123 }
124
125 /**
126 * Parses the content.
127 *
128 * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
129 * @return the parsed data
130 * @throws OsmDataParsingException thrown if something went wrong. Check for chained
131 * exceptions.
132 */
133 public ChangesetDataSet parse(ProgressMonitor progressMonitor) throws OsmDataParsingException {
134 if (progressMonitor == null) {
135 progressMonitor = NullProgressMonitor.INSTANCE;
136 }
137 try {
138 progressMonitor.beginTask("");
139 progressMonitor.indeterminateSubTask(tr("Parsing changeset content ..."));
140 SAXParserFactory.newInstance().newSAXParser().parse(source, new Parser());
141 } catch(OsmDataParsingException e){
142 throw e;
143 } catch (ParserConfigurationException e) {
144 throw new OsmDataParsingException(e);
145 } catch(SAXException e) {
146 throw new OsmDataParsingException(e);
147 } catch(IOException e) {
148 throw new OsmDataParsingException(e);
149 } finally {
150 progressMonitor.finishTask();
151 }
152 return data;
153 }
154
155 /**
156 * Parses the content from the input source
157 *
158 * @return the parsed data
159 * @throws OsmDataParsingException thrown if something went wrong. Check for chained
160 * exceptions.
161 */
162 public ChangesetDataSet parse() throws OsmDataParsingException {
163 return parse(null);
164 }
165}
Note: See TracBrowser for help on using the repository browser.