1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
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.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.xml.sax.Attributes;
|
---|
19 | import org.xml.sax.InputSource;
|
---|
20 | import org.xml.sax.SAXException;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Parser for OSM history data.
|
---|
24 | *
|
---|
25 | * It is slightly different from {@link OsmReader} because we don't build an internal graph of
|
---|
26 | * {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s. We use objects derived from
|
---|
27 | * {@link HistoryOsmPrimitive} instead and we keep the data in a dedicated {@link HistoryDataSet}.
|
---|
28 | * @since 1670
|
---|
29 | */
|
---|
30 | public class OsmHistoryReader {
|
---|
31 |
|
---|
32 | private final InputStream in;
|
---|
33 | private final HistoryDataSet data;
|
---|
34 |
|
---|
35 | private class Parser extends AbstractParser {
|
---|
36 |
|
---|
37 | protected String getCurrentPosition() {
|
---|
38 | if (locator == null)
|
---|
39 | return "";
|
---|
40 | return "(" + locator.getLineNumber() + "," + locator.getColumnNumber() + ")";
|
---|
41 | }
|
---|
42 |
|
---|
43 | protected void throwException(String message) throws SAXException {
|
---|
44 | throw new SAXException(getCurrentPosition() + message);
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
---|
49 | doStartElement(qName, atts);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public void endElement(String uri, String localName, String qName) throws SAXException {
|
---|
54 | if (qName.equals("node")
|
---|
55 | || qName.equals("way")
|
---|
56 | || qName.equals("relation")) {
|
---|
57 | data.put(currentPrimitive);
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Constructs a new {@code OsmHistoryReader}.
|
---|
64 | *
|
---|
65 | * @param source the input stream with the history content as XML document. Must not be null.
|
---|
66 | * @throws IllegalArgumentException if source is {@code null}.
|
---|
67 | */
|
---|
68 | public OsmHistoryReader(InputStream source) {
|
---|
69 | CheckParameterUtil.ensureParameterNotNull(source, "source");
|
---|
70 | this.in = source;
|
---|
71 | this.data = new HistoryDataSet();
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Parses the content.
|
---|
76 | * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
77 | * @return the parsed data
|
---|
78 | * @throws SAXException If any SAX errors occur during processing.
|
---|
79 | * @throws IOException If any IO errors occur.
|
---|
80 | */
|
---|
81 | public HistoryDataSet parse(ProgressMonitor progressMonitor) throws SAXException, IOException {
|
---|
82 | InputSource inputSource = new InputSource(new InputStreamReader(in, "UTF-8"));
|
---|
83 | progressMonitor.beginTask(tr("Parsing OSM history data ..."));
|
---|
84 | try {
|
---|
85 | SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new Parser());
|
---|
86 | } catch (ParserConfigurationException e) {
|
---|
87 | e.printStackTrace(); // broken SAXException chaining
|
---|
88 | throw new SAXException(e);
|
---|
89 | } finally {
|
---|
90 | progressMonitor.finishTask();
|
---|
91 | }
|
---|
92 | return data;
|
---|
93 | }
|
---|
94 | }
|
---|