1 | package org.openstreetmap.josm.io;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.io.InputStream;
|
---|
6 | import java.io.InputStreamReader;
|
---|
7 | import java.util.LinkedList;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.logging.Logger;
|
---|
10 |
|
---|
11 | import javax.xml.parsers.ParserConfigurationException;
|
---|
12 | import javax.xml.parsers.SAXParserFactory;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
15 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
16 | import org.openstreetmap.josm.data.osm.User;
|
---|
17 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
18 | import org.openstreetmap.josm.tools.DateUtils;
|
---|
19 | import org.xml.sax.Attributes;
|
---|
20 | import org.xml.sax.InputSource;
|
---|
21 | import org.xml.sax.Locator;
|
---|
22 | import org.xml.sax.SAXException;
|
---|
23 | import org.xml.sax.helpers.DefaultHandler;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Parser for a list of changesets, encapsulated in an OSM data set structure.
|
---|
27 | * Example:
|
---|
28 | * <pre>
|
---|
29 | * <osm version="0.6" generator="OpenStreetMap server">
|
---|
30 | * <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">
|
---|
31 | * <tag k="asdfasdf" v="asdfasdf"/>
|
---|
32 | * <tag k="created_by" v="JOSM/1.5 (UNKNOWN de)"/>
|
---|
33 | * <tag k="comment" v="1234"/>
|
---|
34 | * </changeset>
|
---|
35 | * </osm>
|
---|
36 | * </pre>
|
---|
37 | *
|
---|
38 | */
|
---|
39 | public class OsmChangesetParser {
|
---|
40 | static private final Logger logger = Logger.getLogger(OsmChangesetParser.class.getName());
|
---|
41 |
|
---|
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 | long id = 0;
|
---|
75 | try {
|
---|
76 | id = Long.parseLong(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 nummeric 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 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
---|
157 | if (qName.equals("osm")) {
|
---|
158 | if (atts == null) {
|
---|
159 | throwException(tr("Missing mandatory attribute ''{0}'' of XML element {1}.", "version", "osm"));
|
---|
160 | }
|
---|
161 | String v = atts.getValue("version");
|
---|
162 | if (v == null) {
|
---|
163 | throwException(tr("Missing mandatory attribute ''{0}''.", "version"));
|
---|
164 | }
|
---|
165 | if (!(v.equals("0.6"))) {
|
---|
166 | throwException(tr("Unsupported version: {0}", v));
|
---|
167 | }
|
---|
168 | } else if (qName.equals("changeset")) {
|
---|
169 | current = new Changeset();
|
---|
170 | parseChangesetAttributes(current, atts);
|
---|
171 | } else if (qName.equals("tag")) {
|
---|
172 | String key = atts.getValue("k");
|
---|
173 | String value = atts.getValue("v");
|
---|
174 | current.put(key, value);
|
---|
175 | } else {
|
---|
176 | throwException(tr("Undefined element ''{0}'' found in input stream. Aborting.", qName));
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | @Override
|
---|
181 | public void endElement(String uri, String localName, String qName) throws SAXException {
|
---|
182 | if (qName.equals("changeset")) {
|
---|
183 | changesets.add(current);
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 | protected User createUser(String uid, String name) throws OsmDataParsingException {
|
---|
188 | if (uid == null) {
|
---|
189 | if (name == null)
|
---|
190 | return null;
|
---|
191 | return User.createLocalUser(name);
|
---|
192 | }
|
---|
193 | try {
|
---|
194 | long id = Long.parseLong(uid);
|
---|
195 | return User.createOsmUser(id, name);
|
---|
196 | } catch(NumberFormatException e) {
|
---|
197 | throwException(tr("Illegal value for attribute ''uid''. Got ''{0}''.", uid));
|
---|
198 | }
|
---|
199 | return null;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Parse the given input source and return the list of changesets
|
---|
205 | *
|
---|
206 | * @param source the source input stream
|
---|
207 | * @param progressMonitor the progress monitor
|
---|
208 | *
|
---|
209 | * @return the list of changesets
|
---|
210 | * @throws IllegalDataException thrown if the an error was found while parsing the data from the source
|
---|
211 | */
|
---|
212 | public static List<Changeset> parse(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
|
---|
213 | OsmChangesetParser parser = new OsmChangesetParser();
|
---|
214 | try {
|
---|
215 | progressMonitor.beginTask(tr("Parsing list of changesets...", 1));
|
---|
216 | InputSource inputSource = new InputSource(new InputStreamReader(source, "UTF-8"));
|
---|
217 | SAXParserFactory.newInstance().newSAXParser().parse(inputSource, parser.new Parser());
|
---|
218 | return parser.getChangesets();
|
---|
219 | } catch(ParserConfigurationException e) {
|
---|
220 | throw new IllegalDataException(e.getMessage(), e);
|
---|
221 | } catch(SAXException e) {
|
---|
222 | throw new IllegalDataException(e.getMessage(), e);
|
---|
223 | } catch(Exception e) {
|
---|
224 | throw new IllegalDataException(e);
|
---|
225 | } finally {
|
---|
226 | progressMonitor.finishTask();
|
---|
227 | }
|
---|
228 | }
|
---|
229 | }
|
---|