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

Last change on this file since 6361 was 6201, checked in by Don-vip, 11 years ago

history/changeset parsing: refactor lots of duplicated code + update javadoc

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
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;
9
10import javax.xml.parsers.ParserConfigurationException;
11import javax.xml.parsers.SAXParserFactory;
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.xml.sax.Attributes;
19import org.xml.sax.InputSource;
20import org.xml.sax.SAXException;
21
22/**
23 * Parser for OSM history data.
24 *
25 * It is slightly different from {@link OsmReader} because we don't build an internal graph of
26 * {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s. We use objects derived from
27 * {@link HistoryOsmPrimitive} instead and we keep the data in a dedicated {@link HistoryDataSet}.
28 * @since 1670
29 */
30public class OsmHistoryReader {
31
32 private final InputStream in;
33 private final HistoryDataSet data;
34
35 private class Parser extends AbstractParser {
36
37 protected String getCurrentPosition() {
38 if (locator == null)
39 return "";
40 return "(" + locator.getLineNumber() + "," + locator.getColumnNumber() + ")";
41 }
42
43 protected void throwException(String message) throws SAXException {
44 throw new SAXException(getCurrentPosition() + message);
45 }
46
47 @Override
48 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
49 doStartElement(qName, atts);
50 }
51
52 @Override
53 public void endElement(String uri, String localName, String qName) throws SAXException {
54 if (qName.equals("node")
55 || qName.equals("way")
56 || qName.equals("relation")) {
57 data.put(currentPrimitive);
58 }
59 }
60 }
61
62 /**
63 * Constructs a new {@code OsmHistoryReader}.
64 *
65 * @param source the input stream with the history content as XML document. Must not be null.
66 * @throws IllegalArgumentException if source is {@code null}.
67 */
68 public OsmHistoryReader(InputStream source) {
69 CheckParameterUtil.ensureParameterNotNull(source, "source");
70 this.in = source;
71 this.data = new HistoryDataSet();
72 }
73
74 /**
75 * Parses the content.
76 * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
77 * @return the parsed data
78 * @throws SAXException If any SAX errors occur during processing.
79 * @throws IOException If any IO errors occur.
80 */
81 public HistoryDataSet parse(ProgressMonitor progressMonitor) throws SAXException, IOException {
82 InputSource inputSource = new InputSource(new InputStreamReader(in, "UTF-8"));
83 progressMonitor.beginTask(tr("Parsing OSM history data ..."));
84 try {
85 SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new Parser());
86 } catch (ParserConfigurationException e) {
87 e.printStackTrace(); // broken SAXException chaining
88 throw new SAXException(e);
89 } finally {
90 progressMonitor.finishTask();
91 }
92 return data;
93 }
94}
Note: See TracBrowser for help on using the repository browser.