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 |
|
---|
10 | import javax.xml.parsers.ParserConfigurationException;
|
---|
11 | import javax.xml.parsers.SAXParserFactory;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
|
---|
15 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
16 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
17 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
18 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
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 "(" + locator.getLineNumber() + "," + locator.getColumnNumber() + ")";
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | protected void throwException(String message) throws SAXException {
|
---|
47 | throw new SAXException(getCurrentPosition() + message);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
---|
52 | doStartElement(qName, atts);
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public void endElement(String uri, String localName, String qName) throws SAXException {
|
---|
57 | if (qName.equals("node")
|
---|
58 | || qName.equals("way")
|
---|
59 | || qName.equals("relation")) {
|
---|
60 | data.put(currentPrimitive);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Constructs a new {@code OsmHistoryReader}.
|
---|
67 | *
|
---|
68 | * @param source the input stream with the history content as XML document. Must not be null.
|
---|
69 | * @throws IllegalArgumentException if source is {@code null}.
|
---|
70 | */
|
---|
71 | public OsmHistoryReader(InputStream source) {
|
---|
72 | CheckParameterUtil.ensureParameterNotNull(source, "source");
|
---|
73 | this.in = source;
|
---|
74 | this.data = new HistoryDataSet();
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Parses the content.
|
---|
79 | * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
80 | * @return the parsed data
|
---|
81 | * @throws SAXException If any SAX errors occur during processing.
|
---|
82 | * @throws IOException If any IO errors occur.
|
---|
83 | */
|
---|
84 | public HistoryDataSet parse(ProgressMonitor progressMonitor) throws SAXException, IOException {
|
---|
85 | InputSource inputSource = new InputSource(new InputStreamReader(in, Utils.UTF_8));
|
---|
86 | progressMonitor.beginTask(tr("Parsing OSM history data ..."));
|
---|
87 | try {
|
---|
88 | SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new Parser());
|
---|
89 | } catch (ParserConfigurationException e) {
|
---|
90 | Main.error(e); // broken SAXException chaining
|
---|
91 | throw new SAXException(e);
|
---|
92 | } finally {
|
---|
93 | progressMonitor.finishTask();
|
---|
94 | }
|
---|
95 | return data;
|
---|
96 | }
|
---|
97 | }
|
---|