source: josm/trunk/src/org/openstreetmap/josm/io/AbstractParser.java@ 15160

Last change on this file since 15160 was 14946, checked in by GerdP, 5 years ago

see #17459: remove duplicated code in reverter corehacks
Step 1: Add needed code to core methods.
Major changes:
1) If a changeset contains multiple versions for a primitive, class ChangesetDataSet keeps the first and the last version (not just the last) and provides methods to access them, unused method getPrimitivesByModificationType() was removed
2) The changeset reader classes have a new field useAnonymousUser. If set, the user found in a changeset is replaced by the anonymous user.

  • Property svn:eol-style set to native
File size: 8.0 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.util.Date;
7
8import org.openstreetmap.josm.data.coor.LatLon;
9import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
10import org.openstreetmap.josm.data.osm.RelationMemberData;
11import org.openstreetmap.josm.data.osm.User;
12import org.openstreetmap.josm.data.osm.history.HistoryNode;
13import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
14import org.openstreetmap.josm.data.osm.history.HistoryRelation;
15import org.openstreetmap.josm.data.osm.history.HistoryWay;
16import org.openstreetmap.josm.tools.date.DateUtils;
17import org.xml.sax.Attributes;
18import org.xml.sax.Locator;
19import org.xml.sax.SAXException;
20import org.xml.sax.helpers.DefaultHandler;
21
22/**
23 * Base class of {@link OsmChangesetContentParser} and {@link OsmHistoryReader} internal parsers.
24 * @since 6201
25 */
26public abstract class AbstractParser extends DefaultHandler {
27
28 /** the current primitive to be read */
29 protected HistoryOsmPrimitive currentPrimitive;
30 protected Locator locator;
31 /** if true, replace user information in input by anonymous user */
32 protected boolean useAnonymousUser;
33
34 @Override
35 public void setDocumentLocator(Locator locator) {
36 this.locator = locator;
37 }
38
39 protected abstract void throwException(String message) throws SAXException;
40
41 protected abstract void throwException(String message, Exception e) throws SAXException;
42
43 protected final long getMandatoryAttributeLong(Attributes attr, String name) throws SAXException {
44 String v = attr.getValue(name);
45 if (v == null) {
46 throwException(tr("Missing mandatory attribute ''{0}''.", name));
47 }
48 long l = 0L;
49 try {
50 l = Long.parseLong(v);
51 } catch (NumberFormatException e) {
52 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long. Got ''{1}''.", name, v), e);
53 }
54 if (l < 0) {
55 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long (>=0). Got ''{1}''.", name, v));
56 }
57 return l;
58 }
59
60 protected final Long getAttributeLong(Attributes attr, String name) throws SAXException {
61 String v = attr.getValue(name);
62 if (v == null)
63 return null;
64 Long l = 0L;
65 try {
66 l = Long.valueOf(v);
67 } catch (NumberFormatException e) {
68 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long. Got ''{1}''.", name, v), e);
69 }
70 if (l < 0) {
71 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long (>=0). Got ''{1}''.", name, v));
72 }
73 return l;
74 }
75
76 protected final Double getAttributeDouble(Attributes attr, String name) throws SAXException {
77 String v = attr.getValue(name);
78 if (v == null) {
79 return null;
80 }
81 double d = 0.0;
82 try {
83 d = Double.parseDouble(v);
84 } catch (NumberFormatException e) {
85 throwException(tr("Illegal value for attribute ''{0}'' of type double. Got ''{1}''.", name, v), e);
86 }
87 return d;
88 }
89
90 protected final String getMandatoryAttributeString(Attributes attr, String name) throws SAXException {
91 String v = attr.getValue(name);
92 if (v == null) {
93 throwException(tr("Missing mandatory attribute ''{0}''.", name));
94 }
95 return v;
96 }
97
98 protected boolean getMandatoryAttributeBoolean(Attributes attr, String name) throws SAXException {
99 String v = attr.getValue(name);
100 if (v == null) {
101 throwException(tr("Missing mandatory attribute ''{0}''.", name));
102 }
103 if ("true".equals(v)) return true;
104 if ("false".equals(v)) return false;
105 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type boolean. Got ''{1}''.", name, v));
106 return false; // not reached
107 }
108
109 protected final HistoryOsmPrimitive createPrimitive(Attributes atts, OsmPrimitiveType type) throws SAXException {
110 long id = getMandatoryAttributeLong(atts, "id");
111 long version = getMandatoryAttributeLong(atts, "version");
112 Long changeset = getAttributeLong(atts, "changeset");
113 long changesetId = changeset != null ? changeset : 0L;
114 boolean visible = getMandatoryAttributeBoolean(atts, "visible");
115
116 User user = null;
117 if (!useAnonymousUser) {
118 Long uid = getAttributeLong(atts, "uid");
119 String userStr = atts.getValue("user");
120 if (userStr != null) {
121 if (uid != null) {
122 user = User.createOsmUser(uid, userStr);
123 user.setPreferredName(userStr);
124 } else {
125 user = User.createLocalUser(userStr);
126 }
127 }
128 }
129 if (user == null) {
130 user = User.getAnonymous();
131 }
132
133 String v = getMandatoryAttributeString(atts, "timestamp");
134 Date timestamp = DateUtils.fromString(v);
135 HistoryOsmPrimitive primitive = null;
136 if (type == OsmPrimitiveType.NODE) {
137 Double lat = getAttributeDouble(atts, "lat");
138 Double lon = getAttributeDouble(atts, "lon");
139 LatLon coor = (lat != null && lon != null) ? new LatLon(lat, lon) : null;
140 primitive = new HistoryNode(id, version, visible, user, changesetId, timestamp, coor, changeset != null);
141
142 } else if (type == OsmPrimitiveType.WAY) {
143 primitive = new HistoryWay(id, version, visible, user, changesetId, timestamp, changeset != null);
144 } else if (type == OsmPrimitiveType.RELATION) {
145 primitive = new HistoryRelation(id, version, visible, user, changesetId, timestamp, changeset != null);
146 }
147 return primitive;
148 }
149
150 protected final void startNode(Attributes atts) throws SAXException {
151 currentPrimitive = createPrimitive(atts, OsmPrimitiveType.NODE);
152 }
153
154 protected final void startWay(Attributes atts) throws SAXException {
155 currentPrimitive = createPrimitive(atts, OsmPrimitiveType.WAY);
156 }
157
158 protected final void startRelation(Attributes atts) throws SAXException {
159 currentPrimitive = createPrimitive(atts, OsmPrimitiveType.RELATION);
160 }
161
162 protected final void handleTag(Attributes atts) throws SAXException {
163 String key = getMandatoryAttributeString(atts, "k");
164 String value = getMandatoryAttributeString(atts, "v");
165 currentPrimitive.put(key, value);
166 }
167
168 protected final void handleNodeReference(Attributes atts) throws SAXException {
169 long ref = getMandatoryAttributeLong(atts, "ref");
170 ((HistoryWay) currentPrimitive).addNode(ref);
171 }
172
173 protected void handleMember(Attributes atts) throws SAXException {
174 long ref = getMandatoryAttributeLong(atts, "ref");
175 String v = getMandatoryAttributeString(atts, "type");
176 OsmPrimitiveType type = null;
177 try {
178 type = OsmPrimitiveType.fromApiTypeName(v);
179 } catch (IllegalArgumentException e) {
180 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type OsmPrimitiveType. Got ''{1}''.", "type", v), e);
181 }
182 String role = getMandatoryAttributeString(atts, "role");
183 RelationMemberData member = new RelationMemberData(role, type, ref);
184 ((HistoryRelation) currentPrimitive).addMember(member);
185 }
186
187 protected final boolean doStartElement(String qName, Attributes atts) throws SAXException {
188 switch (qName) {
189 case "node":
190 startNode(atts);
191 return true;
192 case "way":
193 startWay(atts);
194 return true;
195 case "relation":
196 startRelation(atts);
197 return true;
198 case "tag":
199 handleTag(atts);
200 return true;
201 case "nd":
202 handleNodeReference(atts);
203 return true;
204 case "member":
205 handleMember(atts);
206 return true;
207 default:
208 return false;
209 }
210 }
211}
Note: See TracBrowser for help on using the repository browser.