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