[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[2512] | 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;
|
---|
[7082] | 9 | import java.nio.charset.StandardCharsets;
|
---|
[2512] | 10 |
|
---|
| 11 | import javax.xml.parsers.ParserConfigurationException;
|
---|
| 12 |
|
---|
[6643] | 13 | import org.openstreetmap.josm.Main;
|
---|
[2512] | 14 | import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
|
---|
| 15 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
[6201] | 16 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
[2512] | 17 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[6201] | 18 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[8287] | 19 | import org.openstreetmap.josm.tools.Utils;
|
---|
[2512] | 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 | *
|
---|
[5266] | 27 | * It is slightly different from {@link OsmReader} because we don't build an internal graph of
|
---|
[5881] | 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}.
|
---|
[6201] | 30 | * @since 1670
|
---|
[2512] | 31 | */
|
---|
| 32 | public class OsmHistoryReader {
|
---|
| 33 |
|
---|
[2936] | 34 | private final InputStream in;
|
---|
| 35 | private final HistoryDataSet data;
|
---|
[2512] | 36 |
|
---|
[6201] | 37 | private class Parser extends AbstractParser {
|
---|
[2512] | 38 |
|
---|
| 39 | protected String getCurrentPosition() {
|
---|
| 40 | if (locator == null)
|
---|
| 41 | return "";
|
---|
[10462] | 42 | return new StringBuilder().append('(').append(locator.getLineNumber())
|
---|
| 43 | .append(',').append(locator.getColumnNumber()).append(')').toString();
|
---|
[2512] | 44 | }
|
---|
| 45 |
|
---|
[6643] | 46 | @Override
|
---|
[2512] | 47 | protected void throwException(String message) throws SAXException {
|
---|
[6201] | 48 | throw new SAXException(getCurrentPosition() + message);
|
---|
[2512] | 49 | }
|
---|
| 50 |
|
---|
[6201] | 51 | @Override
|
---|
[8308] | 52 | protected void throwException(String message, Exception e) throws SAXException {
|
---|
| 53 | throw new SAXException(getCurrentPosition() + message, e);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | @Override
|
---|
[6201] | 57 | public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
---|
| 58 | doStartElement(qName, atts);
|
---|
[2512] | 59 | }
|
---|
| 60 |
|
---|
| 61 | @Override
|
---|
| 62 | public void endElement(String uri, String localName, String qName) throws SAXException {
|
---|
[7012] | 63 | if ("node".equals(qName)
|
---|
| 64 | || "way".equals(qName)
|
---|
| 65 | || "relation".equals(qName)) {
|
---|
[6201] | 66 | data.put(currentPrimitive);
|
---|
[2512] | 67 | }
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[6201] | 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 | */
|
---|
[2512] | 77 | public OsmHistoryReader(InputStream source) {
|
---|
[6201] | 78 | CheckParameterUtil.ensureParameterNotNull(source, "source");
|
---|
[2512] | 79 | this.in = source;
|
---|
[2936] | 80 | this.data = new HistoryDataSet();
|
---|
[2512] | 81 | }
|
---|
| 82 |
|
---|
[6201] | 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 | */
|
---|
[2512] | 90 | public HistoryDataSet parse(ProgressMonitor progressMonitor) throws SAXException, IOException {
|
---|
[7082] | 91 | InputSource inputSource = new InputSource(new InputStreamReader(in, StandardCharsets.UTF_8));
|
---|
[2512] | 92 | progressMonitor.beginTask(tr("Parsing OSM history data ..."));
|
---|
| 93 | try {
|
---|
[8347] | 94 | Utils.parseSafeSAX(inputSource, new Parser());
|
---|
[6201] | 95 | } catch (ParserConfigurationException e) {
|
---|
[6643] | 96 | Main.error(e); // broken SAXException chaining
|
---|
[6201] | 97 | throw new SAXException(e);
|
---|
[2512] | 98 | } finally {
|
---|
| 99 | progressMonitor.finishTask();
|
---|
| 100 | }
|
---|
| 101 | return data;
|
---|
| 102 | }
|
---|
| 103 | }
|
---|