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

Last change on this file since 6113 was 5927, checked in by bastiK, 11 years ago

add missing license information

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