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

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

javadoc/code style/minor refactorization

File size: 3.8 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 public 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 (parser.getLocalName().equals("osmChange")) {
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 if (parser.getLocalName().equals("node")) {
72 p = parseNode();
73 } else if (parser.getLocalName().equals("way")) {
74 p = parseWay();
75 } else if (parser.getLocalName().equals("relation")) {
76 p = parseRelation();
77 } else {
78 parseUnknown();
79 }
80 if (p != null && action != null) {
81 if (action.equals("modify")) {
82 p.setModified(true);
83 } else if (action.equals("delete")) {
84 p.setDeleted(true);
85 }
86 }
87 } else if (event == XMLStreamConstants.END_ELEMENT) {
88 return;
89 }
90 }
91 }
92
93 /**
94 * Parse the given input source and return the dataset.
95 *
96 * @param source the source input stream. Must not be <code>null</code>.
97 * @param progressMonitor the progress monitor. If <code>null</code>,
98 * {@link org.openstreetmap.josm.gui.progress.NullProgressMonitor#INSTANCE} is assumed
99 *
100 * @return the dataset with the parsed data
101 * @throws IllegalDataException thrown if the an error was found while parsing the data from the source
102 * @throws IllegalArgumentException thrown if source is <code>null</code>
103 */
104 public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
105 return new OsmChangeReader().doParseDataSet(source, progressMonitor);
106 }
107}
Note: See TracBrowser for help on using the repository browser.