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

Last change on this file since 13783 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 6.3 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[2688]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;
[7082]10import java.nio.charset.StandardCharsets;
[2688]11
12import javax.xml.parsers.ParserConfigurationException;
13
14import org.openstreetmap.josm.data.osm.ChangesetDataSet;
15import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetModificationType;
16import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
17import org.openstreetmap.josm.gui.progress.ProgressMonitor;
18import org.openstreetmap.josm.tools.CheckParameterUtil;
[12620]19import org.openstreetmap.josm.tools.Logging;
[8287]20import org.openstreetmap.josm.tools.Utils;
[6906]21import org.openstreetmap.josm.tools.XmlParsingException;
[2688]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.
[6201]29 * @since 2688
[2688]30 */
31public class OsmChangesetContentParser {
32
[9078]33 private final InputSource source;
[6201]34 private final ChangesetDataSet data = new ChangesetDataSet();
[2688]35
[6201]36 private class Parser extends AbstractParser {
[2688]37
38 /** the current change modification type */
39 private ChangesetDataSet.ChangesetModificationType currentModificationType;
40
[7082]41 @Override
[6906]42 protected void throwException(String message) throws XmlParsingException {
43 throw new XmlParsingException(message).rememberLocation(locator);
[2688]44 }
45
[8308]46 @Override
47 protected void throwException(String message, Exception e) throws XmlParsingException {
48 throw new XmlParsingException(message, e).rememberLocation(locator);
[2688]49 }
50
[7012]51 @Override
52 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
[6201]53 if (super.doStartElement(qName, atts)) {
54 // done
[7012]55 return;
56 }
[7082]57 switch (qName) {
[7012]58 case "osmChange":
[2688]59 // do nothing
[7012]60 break;
61 case "create":
[2688]62 currentModificationType = ChangesetModificationType.CREATED;
[7012]63 break;
64 case "modify":
[2688]65 currentModificationType = ChangesetModificationType.UPDATED;
[7012]66 break;
67 case "delete":
[2688]68 currentModificationType = ChangesetModificationType.DELETED;
[7012]69 break;
70 default:
[12620]71 Logging.warn(tr("Unsupported start element ''{0}'' in changeset content at position ({1},{2}). Skipping.",
[7012]72 qName, locator.getLineNumber(), locator.getColumnNumber()));
[2688]73 }
74 }
75
76 @Override
77 public void endElement(String uri, String localName, String qName) throws SAXException {
[7012]78 switch (qName) {
79 case "node":
80 case "way":
81 case "relation":
[2688]82 if (currentModificationType == null) {
[8540]83 // CHECKSTYLE.OFF: LineLength
[2852]84 throwException(tr("Illegal document structure. Found node, way, or relation outside of ''create'', ''modify'', or ''delete''."));
[8540]85 // CHECKSTYLE.ON: LineLength
[2688]86 }
87 data.put(currentPrimitive, currentModificationType);
[7012]88 break;
89 case "create":
90 case "modify":
91 case "delete":
[2688]92 currentModificationType = null;
[7012]93 break;
[7801]94 case "osmChange":
[7012]95 case "tag":
96 case "nd":
97 case "member":
[2688]98 // do nothing
[7012]99 break;
100 default:
[12620]101 Logging.warn(tr("Unsupported end element ''{0}'' in changeset content at position ({1},{2}). Skipping.",
[7012]102 qName, locator.getLineNumber(), locator.getColumnNumber()));
[2688]103 }
104 }
105
106 @Override
107 public void error(SAXParseException e) throws SAXException {
[8308]108 throwException(null, e);
[2688]109 }
110
111 @Override
112 public void fatalError(SAXParseException e) throws SAXException {
[8308]113 throwException(null, e);
[2688]114 }
115 }
116
117 /**
[6201]118 * Constructs a new {@code OsmChangesetContentParser}.
[2711]119 *
[2688]120 * @param source the input stream with the changeset content as XML document. Must not be null.
[6201]121 * @throws IllegalArgumentException if source is {@code null}.
[2688]122 */
[7033]123 @SuppressWarnings("resource")
[6552]124 public OsmChangesetContentParser(InputStream source) {
[2688]125 CheckParameterUtil.ensureParameterNotNull(source, "source");
[7082]126 this.source = new InputSource(new InputStreamReader(source, StandardCharsets.UTF_8));
[2688]127 }
128
[6201]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 */
[2688]135 public OsmChangesetContentParser(String source) {
136 CheckParameterUtil.ensureParameterNotNull(source, "source");
137 this.source = new InputSource(new StringReader(source));
138 }
139
140 /**
[6201]141 * Parses the content.
[2711]142 *
[6201]143 * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
[2688]144 * @return the parsed data
[6906]145 * @throws XmlParsingException if something went wrong. Check for chained
[2688]146 * exceptions.
147 */
[6906]148 public ChangesetDataSet parse(ProgressMonitor progressMonitor) throws XmlParsingException {
[2688]149 if (progressMonitor == null) {
150 progressMonitor = NullProgressMonitor.INSTANCE;
151 }
152 try {
[2828]153 progressMonitor.beginTask("");
[2688]154 progressMonitor.indeterminateSubTask(tr("Parsing changeset content ..."));
[8347]155 Utils.parseSafeSAX(source, new Parser());
[8510]156 } catch (XmlParsingException e) {
[2688]157 throw e;
[7004]158 } catch (ParserConfigurationException | SAXException | IOException e) {
[6906]159 throw new XmlParsingException(e);
[2688]160 } finally {
161 progressMonitor.finishTask();
162 }
163 return data;
164 }
165
166 /**
167 * Parses the content from the input source
[2711]168 *
[2688]169 * @return the parsed data
[6906]170 * @throws XmlParsingException if something went wrong. Check for chained
[2688]171 * exceptions.
172 */
[6906]173 public ChangesetDataSet parse() throws XmlParsingException {
[2688]174 return parse(null);
175 }
176}
Note: See TracBrowser for help on using the repository browser.