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

Last change on this file since 8332 was 8308, checked in by Don-vip, 9 years ago

fix potential NPEs and Sonar issues related to serialization

  • Property svn:eol-style set to native
File size: 3.6 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.Main;
14import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
15import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
16import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
17import org.openstreetmap.josm.gui.progress.ProgressMonitor;
18import org.openstreetmap.josm.tools.CheckParameterUtil;
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 "(" + 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 protected void throwException(String message, Exception e) throws SAXException {
52 throw new SAXException(getCurrentPosition() + message, e);
53 }
54
55 @Override
56 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
57 doStartElement(qName, atts);
58 }
59
60 @Override
61 public void endElement(String uri, String localName, String qName) throws SAXException {
62 if ("node".equals(qName)
63 || "way".equals(qName)
64 || "relation".equals(qName)) {
65 data.put(currentPrimitive);
66 }
67 }
68 }
69
70 /**
71 * Constructs a new {@code OsmHistoryReader}.
72 *
73 * @param source the input stream with the history content as XML document. Must not be null.
74 * @throws IllegalArgumentException if source is {@code null}.
75 */
76 public OsmHistoryReader(InputStream source) {
77 CheckParameterUtil.ensureParameterNotNull(source, "source");
78 this.in = source;
79 this.data = new HistoryDataSet();
80 }
81
82 /**
83 * Parses the content.
84 * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
85 * @return the parsed data
86 * @throws SAXException If any SAX errors occur during processing.
87 * @throws IOException If any IO errors occur.
88 */
89 public HistoryDataSet parse(ProgressMonitor progressMonitor) throws SAXException, IOException {
90 InputSource inputSource = new InputSource(new InputStreamReader(in, StandardCharsets.UTF_8));
91 progressMonitor.beginTask(tr("Parsing OSM history data ..."));
92 try {
93 Utils.newSafeSAXParser().parse(inputSource, new Parser());
94 } catch (ParserConfigurationException e) {
95 Main.error(e); // broken SAXException chaining
96 throw new SAXException(e);
97 } finally {
98 progressMonitor.finishTask();
99 }
100 return data;
101 }
102}
Note: See TracBrowser for help on using the repository browser.