[5927] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[4530] | 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.io.InputStream;
|
---|
[5345] | 7 | import java.util.Arrays;
|
---|
[4530] | 8 |
|
---|
| 9 | import javax.xml.stream.XMLStreamConstants;
|
---|
| 10 | import javax.xml.stream.XMLStreamException;
|
---|
| 11 |
|
---|
| 12 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[4532] | 13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[4530] | 14 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
| 15 |
|
---|
| 16 | public class OsmChangeReader extends OsmReader {
|
---|
| 17 |
|
---|
[5345] | 18 | public static final String[] ACTIONS = {"create", "modify", "delete"};
|
---|
| 19 |
|
---|
[4530] | 20 | /**
|
---|
| 21 | * constructor (for private and subclasses use only)
|
---|
| 22 | *
|
---|
[5881] | 23 | * @see #parseDataSet(InputStream, ProgressMonitor)
|
---|
[4530] | 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) {
|
---|
[5345] | 52 | if (Arrays.asList(ACTIONS).contains(parser.getLocalName())) {
|
---|
| 53 | parseCommon(parser.getLocalName());
|
---|
[4530] | 54 | } else {
|
---|
| 55 | parseUnknown();
|
---|
| 56 | }
|
---|
| 57 | } else if (event == XMLStreamConstants.END_ELEMENT) {
|
---|
| 58 | return;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[5345] | 63 | private void parseCommon(String action) throws XMLStreamException {
|
---|
[4530] | 64 | while (parser.hasNext()) {
|
---|
| 65 | int event = parser.next();
|
---|
| 66 | if (event == XMLStreamConstants.START_ELEMENT) {
|
---|
[4532] | 67 | OsmPrimitive p = null;
|
---|
[4530] | 68 | if (parser.getLocalName().equals("node")) {
|
---|
[4532] | 69 | p = parseNode();
|
---|
[4530] | 70 | } else if (parser.getLocalName().equals("way")) {
|
---|
[4532] | 71 | p = parseWay();
|
---|
[4530] | 72 | } else if (parser.getLocalName().equals("relation")) {
|
---|
[4532] | 73 | p = parseRelation();
|
---|
[4530] | 74 | } else {
|
---|
| 75 | parseUnknown();
|
---|
| 76 | }
|
---|
[5345] | 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 | }
|
---|
[4532] | 83 | }
|
---|
[4530] | 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 | *
|
---|
[5881] | 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
|
---|
[4530] | 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
|
---|
[5881] | 99 | * @throws IllegalArgumentException thrown if source is <code>null</code>
|
---|
[4530] | 100 | */
|
---|
| 101 | public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
|
---|
| 102 | return new OsmChangeReader().doParseDataSet(source, progressMonitor);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|