1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.nio.charset.StandardCharsets;
|
---|
10 |
|
---|
11 | import javax.xml.parsers.ParserConfigurationException;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
|
---|
14 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
16 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
17 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
18 | import org.openstreetmap.josm.tools.Logging;
|
---|
19 | import org.openstreetmap.josm.tools.Utils;
|
---|
20 | import org.xml.sax.Attributes;
|
---|
21 | import org.xml.sax.InputSource;
|
---|
22 | import org.xml.sax.SAXException;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Parser for OSM history data.
|
---|
26 | *
|
---|
27 | * It is slightly different from {@link OsmReader} because we don't build an internal graph of
|
---|
28 | * {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s. We use objects derived from
|
---|
29 | * {@link HistoryOsmPrimitive} instead and we keep the data in a dedicated {@link HistoryDataSet}.
|
---|
30 | * @since 1670
|
---|
31 | */
|
---|
32 | public class OsmHistoryReader {
|
---|
33 |
|
---|
34 | private final InputStream in;
|
---|
35 | private final HistoryDataSet data;
|
---|
36 |
|
---|
37 | private class Parser extends AbstractParser {
|
---|
38 |
|
---|
39 | protected String getCurrentPosition() {
|
---|
40 | if (locator == null)
|
---|
41 | return "";
|
---|
42 | return new StringBuilder().append('(').append(locator.getLineNumber())
|
---|
43 | .append(',').append(locator.getColumnNumber()).append(')').toString();
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | protected void throwException(String message) throws SAXException {
|
---|
48 | throw new SAXException(getCurrentPosition() + message);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | protected void throwException(String message, Exception e) throws SAXException {
|
---|
53 | throw new SAXException(getCurrentPosition() + message, e);
|
---|
54 | }
|
---|
55 |
|
---|
56 | @Override
|
---|
57 | public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
---|
58 | doStartElement(qName, atts);
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public void endElement(String uri, String localName, String qName) throws SAXException {
|
---|
63 | if ("node".equals(qName)
|
---|
64 | || "way".equals(qName)
|
---|
65 | || "relation".equals(qName)) {
|
---|
66 | data.put(currentPrimitive);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Constructs a new {@code OsmHistoryReader}.
|
---|
73 | *
|
---|
74 | * @param source the input stream with the history content as XML document. Must not be null.
|
---|
75 | * @throws IllegalArgumentException if source is {@code null}.
|
---|
76 | */
|
---|
77 | public OsmHistoryReader(InputStream source) {
|
---|
78 | CheckParameterUtil.ensureParameterNotNull(source, "source");
|
---|
79 | this.in = source;
|
---|
80 | this.data = new HistoryDataSet();
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Parses the content.
|
---|
85 | * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
86 | * @return the parsed data
|
---|
87 | * @throws SAXException If any SAX errors occur during processing.
|
---|
88 | * @throws IOException If any IO errors occur.
|
---|
89 | */
|
---|
90 | public HistoryDataSet parse(ProgressMonitor progressMonitor) throws SAXException, IOException {
|
---|
91 | InputSource inputSource = new InputSource(new InputStreamReader(in, StandardCharsets.UTF_8));
|
---|
92 | progressMonitor.beginTask(tr("Parsing OSM history data ..."));
|
---|
93 | try {
|
---|
94 | Utils.parseSafeSAX(inputSource, new Parser());
|
---|
95 | } catch (ParserConfigurationException e) {
|
---|
96 | Logging.error(e); // broken SAXException chaining
|
---|
97 | throw new SAXException(e);
|
---|
98 | } finally {
|
---|
99 | progressMonitor.finishTask();
|
---|
100 | }
|
---|
101 | return data;
|
---|
102 | }
|
---|
103 | }
|
---|