source: josm/trunk/src/org/openstreetmap/josm/io/OsmHistoryReader.java@ 13777

Last change on this file since 13777 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.InputStreamReader;
9import java.nio.charset.StandardCharsets;
10
11import javax.xml.parsers.ParserConfigurationException;
12
13import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
14import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
15import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
16import org.openstreetmap.josm.gui.progress.ProgressMonitor;
17import org.openstreetmap.josm.tools.CheckParameterUtil;
18import org.openstreetmap.josm.tools.Logging;
19import org.openstreetmap.josm.tools.Utils;
20import org.xml.sax.Attributes;
21import org.xml.sax.InputSource;
22import 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 */
32public 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}
Note: See TracBrowser for help on using the repository browser.