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

Last change on this file since 2907 was 2852, checked in by mjulius, 14 years ago

fix messages for io

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 static private final Logger logger = Logger.getLogger(OsmChangesetParser.class.getName());
42
43 private List<Changeset> changesets;
44
45 private OsmChangesetParser() {
46 changesets = new LinkedList<Changeset>();
47 }
48
49 public List<Changeset> getChangesets() {
50 return changesets;
51 }
52
53 private class Parser extends DefaultHandler {
54 private Locator locator;
55
56 @Override
57 public void setDocumentLocator(Locator locator) {
58 this.locator = locator;
59 }
60
61 protected void throwException(String msg) throws OsmDataParsingException{
62 throw new OsmDataParsingException(msg).rememberLocation(locator);
63 }
64 /**
65 * The current changeset
66 */
67 private Changeset current = null;
68
69 protected void parseChangesetAttributes(Changeset cs, Attributes atts) throws OsmDataParsingException {
70 // -- id
71 String value = atts.getValue("id");
72 if (value == null) {
73 throwException(tr("Missing mandatory attribute ''{0}''.", "id"));
74 }
75 int id = 0;
76 try {
77 id = Integer.parseInt(value);
78 } catch(NumberFormatException e) {
79 throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "id", value));
80 }
81 if (id <= 0) {
82 throwException(tr("Illegal nummeric value for attribute ''{0}''. Got ''{1}''.", "id", id));
83 }
84 current.setId(id);
85
86 // -- user
87 String user = atts.getValue("user");
88 String uid = atts.getValue("uid");
89 current.setUser(createUser(uid, user));
90
91 // -- created_at
92 value = atts.getValue("created_at");
93 if (value == null) {
94 current.setCreatedAt(null);
95 } else {
96 current.setCreatedAt(DateUtils.fromString(value));
97 }
98
99 // -- closed_at
100 value = atts.getValue("closed_at");
101 if (value == null) {
102 current.setClosedAt(null);
103 } else {
104 current.setClosedAt(DateUtils.fromString(value));
105 }
106
107 // -- open
108 value = atts.getValue("open");
109 if (value == null) {
110 throwException(tr("Missing mandatory attribute ''{0}''.", "open"));
111 } else if (value.equals("true")) {
112 current.setOpen(true);
113 } else if (value.equals("false")) {
114 current.setOpen(false);
115 } else {
116 throwException(tr("Illegal boolean value for attribute ''{0}''. Got ''{1}''.", "open", value));
117 }
118
119 // -- min_lon and min_lat
120 String min_lon = atts.getValue("min_lon");
121 String min_lat = atts.getValue("min_lat");
122 String max_lon = atts.getValue("max_lon");
123 String max_lat = atts.getValue("max_lat");
124 if (min_lon != null && min_lat != null && max_lon != null && max_lat != null) {
125 double minLon = 0;
126 try {
127 minLon = Double.parseDouble(min_lon);
128 } catch(NumberFormatException e) {
129 throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "min_lon", min_lon));
130 }
131 double minLat = 0;
132 try {
133 minLat = Double.parseDouble(min_lat);
134 } catch(NumberFormatException e) {
135 throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "min_lat", min_lat));
136 }
137 current.setMin(new LatLon(minLat, minLon));
138
139 // -- max_lon and max_lat
140
141 double maxLon = 0;
142 try {
143 maxLon = Double.parseDouble(max_lon);
144 } catch(NumberFormatException e) {
145 throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "max_lon", max_lon));
146 }
147 double maxLat = 0;
148 try {
149 maxLat = Double.parseDouble(max_lat);
150 } catch(NumberFormatException e) {
151 throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "max_lat", max_lat));
152 }
153 current.setMax(new LatLon(maxLon, maxLat));
154 }
155 }
156
157 @Override 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 InputStreamReader(source, "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.