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

Last change on this file since 14273 was 13901, checked in by Don-vip, 6 years ago

add new XmlUtils class with more "safe factories" methods

  • Property svn:eol-style set to native
File size: 3.8 KB
RevLine 
[6380]1// License: GPL. For details, see LICENSE file.
[2512]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;
[7082]9import java.nio.charset.StandardCharsets;
[2512]10
11import javax.xml.parsers.ParserConfigurationException;
12
13import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
14import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
[6201]15import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
[2512]16import org.openstreetmap.josm.gui.progress.ProgressMonitor;
[6201]17import org.openstreetmap.josm.tools.CheckParameterUtil;
[12620]18import org.openstreetmap.josm.tools.Logging;
[13901]19import org.openstreetmap.josm.tools.XmlUtils;
[2512]20import org.xml.sax.Attributes;
21import org.xml.sax.InputSource;
22import 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 */
32public 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 {
[13901]94 XmlUtils.parseSafeSAX(inputSource, new Parser());
[6201]95 } catch (ParserConfigurationException e) {
[12620]96 Logging.error(e); // broken SAXException chaining
[6201]97 throw new SAXException(e);
[2512]98 } finally {
99 progressMonitor.finishTask();
100 }
101 return data;
102 }
103}
Note: See TracBrowser for help on using the repository browser.