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

Last change on this file since 8347 was 8308, checked in by Don-vip, 9 years ago

fix potential NPEs and Sonar issues related to serialization

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