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

Last change on this file since 7082 was 7012, checked in by Don-vip, 10 years ago

see #8465 - use String switch/case where applicable

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