1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package reverter;
|
---|
3 |
|
---|
4 | import java.io.InputStream;
|
---|
5 | import java.io.InputStreamReader;
|
---|
6 |
|
---|
7 | import javax.xml.stream.XMLStreamException;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.osm.Node;
|
---|
10 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
11 | import org.openstreetmap.josm.data.osm.Way;
|
---|
12 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
13 | import org.openstreetmap.josm.io.OsmReader;
|
---|
14 | import org.openstreetmap.josm.io.UTFInputStreamReader;
|
---|
15 | import org.openstreetmap.josm.tools.XmlUtils;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Subclass of {@link org.openstreetmap.josm.io.OsmReader} that can handle multiple XML streams.
|
---|
19 | *
|
---|
20 | */
|
---|
21 | public class MultiOsmReader extends OsmReader {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Parse data in source and add to existing dataset.
|
---|
25 | * @param source the input stream with OSM data
|
---|
26 | * @throws IllegalDataException in case of any exception
|
---|
27 | */
|
---|
28 | public void addData(InputStream source) throws IllegalDataException {
|
---|
29 | try (InputStreamReader ir = UTFInputStreamReader.create(source)) {
|
---|
30 | setParser(XmlUtils.newSafeXMLInputFactory().createXMLStreamReader(ir));
|
---|
31 | parse();
|
---|
32 | } catch (Exception e) {
|
---|
33 | throw new IllegalDataException(e);
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void processData() throws IllegalDataException {
|
---|
38 | prepareDataSet();
|
---|
39 | }
|
---|
40 |
|
---|
41 | public ParseCallback callback;
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | protected Node parseNode() throws XMLStreamException {
|
---|
45 | Node node = super.parseNode();
|
---|
46 | if (callback != null) {
|
---|
47 | callback.primitiveParsed(node.getPrimitiveId());
|
---|
48 | }
|
---|
49 | return node;
|
---|
50 | }
|
---|
51 | @Override
|
---|
52 | protected Way parseWay() throws XMLStreamException {
|
---|
53 | Way way = super.parseWay();
|
---|
54 | if (callback != null) {
|
---|
55 | callback.primitiveParsed(way.getPrimitiveId());
|
---|
56 | }
|
---|
57 | return way;
|
---|
58 | }
|
---|
59 | @Override
|
---|
60 | protected Relation parseRelation() throws XMLStreamException {
|
---|
61 | Relation relation = super.parseRelation();
|
---|
62 | if (callback != null) {
|
---|
63 | callback.primitiveParsed(relation.getPrimitiveId());
|
---|
64 | }
|
---|
65 | return relation;
|
---|
66 | }
|
---|
67 | }
|
---|