source: josm/trunk/src/org/openstreetmap/josm/io/OsmChangesetParser.java@ 6787

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

fix #9647 - filter invalid XML characters in changeset API responses

  • Property svn:eol-style set to native
File size: 8.9 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.InputStream;
7import java.io.InputStreamReader;
8import java.text.MessageFormat;
9import java.util.LinkedList;
10import java.util.List;
11
12import javax.xml.parsers.ParserConfigurationException;
13import javax.xml.parsers.SAXParserFactory;
14
15import org.openstreetmap.josm.data.coor.LatLon;
16import org.openstreetmap.josm.data.osm.Changeset;
17import org.openstreetmap.josm.data.osm.User;
18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
19import org.openstreetmap.josm.tools.DateUtils;
20import org.openstreetmap.josm.tools.Utils;
21import org.xml.sax.Attributes;
22import org.xml.sax.InputSource;
23import org.xml.sax.Locator;
24import org.xml.sax.SAXException;
25import org.xml.sax.helpers.DefaultHandler;
26
27/**
28 * Parser for a list of changesets, encapsulated in an OSM data set structure.
29 * Example:
30 * <pre>
31 * &lt;osm version="0.6" generator="OpenStreetMap server"&gt;
32 * &lt;changeset id="143" user="guggis" uid="1" created_at="2009-09-08T20:35:39Z" closed_at="2009-09-08T21:36:12Z" open="false" min_lon="7.380925" min_lat="46.9215164" max_lon="7.3984718" max_lat="46.9226502"&gt;
33 * &lt;tag k="asdfasdf" v="asdfasdf"/&gt;
34 * &lt;tag k="created_by" v="JOSM/1.5 (UNKNOWN de)"/&gt;
35 * &lt;tag k="comment" v="1234"/&gt;
36 * &lt;/changeset&gt;
37 * &lt;/osm&gt;
38 * </pre>
39 *
40 */
41public final class OsmChangesetParser {
42 private List<Changeset> changesets;
43
44 private OsmChangesetParser() {
45 changesets = new LinkedList<Changeset>();
46 }
47
48 public List<Changeset> getChangesets() {
49 return changesets;
50 }
51
52 private class Parser extends DefaultHandler {
53 private Locator locator;
54
55 @Override
56 public void setDocumentLocator(Locator locator) {
57 this.locator = locator;
58 }
59
60 protected void throwException(String msg) throws OsmDataParsingException{
61 throw new OsmDataParsingException(msg).rememberLocation(locator);
62 }
63 /**
64 * The current changeset
65 */
66 private Changeset current = null;
67
68 protected void parseChangesetAttributes(Changeset cs, Attributes atts) throws OsmDataParsingException {
69 // -- id
70 String value = atts.getValue("id");
71 if (value == null) {
72 throwException(tr("Missing mandatory attribute ''{0}''.", "id"));
73 }
74 int id = 0;
75 try {
76 id = Integer.parseInt(value);
77 } catch(NumberFormatException e) {
78 throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "id", value));
79 }
80 if (id <= 0) {
81 throwException(tr("Illegal numeric value for attribute ''{0}''. Got ''{1}''.", "id", id));
82 }
83 current.setId(id);
84
85 // -- user
86 String user = atts.getValue("user");
87 String uid = atts.getValue("uid");
88 current.setUser(createUser(uid, user));
89
90 // -- created_at
91 value = atts.getValue("created_at");
92 if (value == null) {
93 current.setCreatedAt(null);
94 } else {
95 current.setCreatedAt(DateUtils.fromString(value));
96 }
97
98 // -- closed_at
99 value = atts.getValue("closed_at");
100 if (value == null) {
101 current.setClosedAt(null);
102 } else {
103 current.setClosedAt(DateUtils.fromString(value));
104 }
105
106 // -- open
107 value = atts.getValue("open");
108 if (value == null) {
109 throwException(tr("Missing mandatory attribute ''{0}''.", "open"));
110 } else if (value.equals("true")) {
111 current.setOpen(true);
112 } else if (value.equals("false")) {
113 current.setOpen(false);
114 } else {
115 throwException(tr("Illegal boolean value for attribute ''{0}''. Got ''{1}''.", "open", value));
116 }
117
118 // -- min_lon and min_lat
119 String min_lon = atts.getValue("min_lon");
120 String min_lat = atts.getValue("min_lat");
121 String max_lon = atts.getValue("max_lon");
122 String max_lat = atts.getValue("max_lat");
123 if (min_lon != null && min_lat != null && max_lon != null && max_lat != null) {
124 double minLon = 0;
125 try {
126 minLon = Double.parseDouble(min_lon);
127 } catch(NumberFormatException e) {
128 throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "min_lon", min_lon));
129 }
130 double minLat = 0;
131 try {
132 minLat = Double.parseDouble(min_lat);
133 } catch(NumberFormatException e) {
134 throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "min_lat", min_lat));
135 }
136 current.setMin(new LatLon(minLat, minLon));
137
138 // -- max_lon and max_lat
139
140 double maxLon = 0;
141 try {
142 maxLon = Double.parseDouble(max_lon);
143 } catch(NumberFormatException e) {
144 throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "max_lon", max_lon));
145 }
146 double maxLat = 0;
147 try {
148 maxLat = Double.parseDouble(max_lat);
149 } catch(NumberFormatException e) {
150 throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "max_lat", max_lat));
151 }
152 current.setMax(new LatLon(maxLon, maxLat));
153 }
154 }
155
156 @Override
157 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
158 if (qName.equals("osm")) {
159 if (atts == null) {
160 throwException(tr("Missing mandatory attribute ''{0}'' of XML element {1}.", "version", "osm"));
161 }
162 String v = atts.getValue("version");
163 if (v == null) {
164 throwException(tr("Missing mandatory attribute ''{0}''.", "version"));
165 }
166 if (!(v.equals("0.6"))) {
167 throwException(tr("Unsupported version: {0}", v));
168 }
169 } else if (qName.equals("changeset")) {
170 current = new Changeset();
171 parseChangesetAttributes(current, atts);
172 } else if (qName.equals("tag")) {
173 String key = atts.getValue("k");
174 String value = atts.getValue("v");
175 current.put(key, value);
176 } else {
177 throwException(tr("Undefined element ''{0}'' found in input stream. Aborting.", qName));
178 }
179 }
180
181 @Override
182 public void endElement(String uri, String localName, String qName) throws SAXException {
183 if (qName.equals("changeset")) {
184 changesets.add(current);
185 }
186 }
187
188 protected User createUser(String uid, String name) throws OsmDataParsingException {
189 if (uid == null) {
190 if (name == null)
191 return null;
192 return User.createLocalUser(name);
193 }
194 try {
195 long id = Long.parseLong(uid);
196 return User.createOsmUser(id, name);
197 } catch(NumberFormatException e) {
198 throwException(MessageFormat.format("Illegal value for attribute ''uid''. Got ''{0}''.", uid));
199 }
200 return null;
201 }
202 }
203
204 /**
205 * Parse the given input source and return the list of changesets
206 *
207 * @param source the source input stream
208 * @param progressMonitor the progress monitor
209 *
210 * @return the list of changesets
211 * @throws IllegalDataException thrown if the an error was found while parsing the data from the source
212 */
213 public static List<Changeset> parse(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
214 OsmChangesetParser parser = new OsmChangesetParser();
215 try {
216 progressMonitor.beginTask("");
217 progressMonitor.indeterminateSubTask(tr("Parsing list of changesets..."));
218 InputSource inputSource = new InputSource(new InvalidXmlCharacterFilter(new InputStreamReader(source, Utils.UTF_8)));
219 SAXParserFactory.newInstance().newSAXParser().parse(inputSource, parser.new Parser());
220 return parser.getChangesets();
221 } catch(ParserConfigurationException e) {
222 throw new IllegalDataException(e.getMessage(), e);
223 } catch(SAXException e) {
224 throw new IllegalDataException(e.getMessage(), e);
225 } catch(Exception e) {
226 throw new IllegalDataException(e);
227 } finally {
228 progressMonitor.finishTask();
229 }
230 }
231}
Note: See TracBrowser for help on using the repository browser.