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

Last change on this file since 3422 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

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