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

Last change on this file since 7282 was 7082, checked in by Don-vip, 10 years ago

see #8465 - replace Utils.UTF_8 by StandardCharsets.UTF_8, new in Java 7

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