source: josm/trunk/src/org/openstreetmap/josm/io/OsmChangeReader.java@ 8380

Last change on this file since 8380 was 8291, checked in by Don-vip, 9 years ago

fix squid:RedundantThrowsDeclarationCheck + consistent Javadoc for exceptions

  • Property svn:eol-style set to native
File size: 3.7 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.InputStream;
7import java.util.Arrays;
8
9import javax.xml.stream.XMLStreamConstants;
10import javax.xml.stream.XMLStreamException;
11
12import org.openstreetmap.josm.data.osm.DataSet;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.gui.progress.ProgressMonitor;
15
16/**
17 * Reader for <a href="http://wiki.openstreetmap.org/wiki/OsmChange">OsmChange</a> file format.
18 */
19public class OsmChangeReader extends OsmReader {
20
21 /**
22 * List of possible actions.
23 */
24 private static final String[] ACTIONS = {"create", "modify", "delete"};
25
26 /**
27 * constructor (for private and subclasses use only)
28 *
29 * @see #parseDataSet(InputStream, ProgressMonitor)
30 */
31 protected OsmChangeReader() {
32 }
33
34 @Override
35 protected void parseRoot() throws XMLStreamException {
36 if ("osmChange".equals(parser.getLocalName())) {
37 parseOsmChange();
38 } else {
39 parseUnknown();
40 }
41 }
42
43 private void parseOsmChange() throws XMLStreamException {
44 String v = parser.getAttributeValue(null, "version");
45 if (v == null) {
46 throwException(tr("Missing mandatory attribute ''{0}''.", "version"));
47 }
48 if (!"0.6".equals(v)) {
49 throwException(tr("Unsupported version: {0}", v));
50 }
51 ds.setVersion(v);
52 while (parser.hasNext()) {
53 int event = parser.next();
54 if (event == XMLStreamConstants.START_ELEMENT) {
55 if (Arrays.asList(ACTIONS).contains(parser.getLocalName())) {
56 parseCommon(parser.getLocalName());
57 } else {
58 parseUnknown();
59 }
60 } else if (event == XMLStreamConstants.END_ELEMENT) {
61 return;
62 }
63 }
64 }
65
66 private void parseCommon(String action) throws XMLStreamException {
67 while (parser.hasNext()) {
68 int event = parser.next();
69 if (event == XMLStreamConstants.START_ELEMENT) {
70 OsmPrimitive p = null;
71 switch (parser.getLocalName()) {
72 case "node":
73 p = parseNode();
74 break;
75 case "way":
76 p = parseWay();
77 break;
78 case "relation":
79 p = parseRelation();
80 break;
81 default:
82 parseUnknown();
83 }
84 if (p != null && action != null) {
85 if ("modify".equals(action)) {
86 p.setModified(true);
87 } else if ("delete".equals(action)) {
88 p.setDeleted(true);
89 }
90 }
91 } else if (event == XMLStreamConstants.END_ELEMENT) {
92 return;
93 }
94 }
95 }
96
97 /**
98 * Parse the given input source and return the dataset.
99 *
100 * @param source the source input stream. Must not be <code>null</code>.
101 * @param progressMonitor the progress monitor. If <code>null</code>,
102 * {@link org.openstreetmap.josm.gui.progress.NullProgressMonitor#INSTANCE} is assumed
103 *
104 * @return the dataset with the parsed data
105 * @throws IllegalDataException if the an error was found while parsing the data from the source
106 * @throws IllegalArgumentException if source is <code>null</code>
107 */
108 public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
109 return new OsmChangeReader().doParseDataSet(source, progressMonitor);
110 }
111}
Note: See TracBrowser for help on using the repository browser.