Changeset 6201 in josm
- Timestamp:
- 2013-08-27T03:03:11+02:00 (11 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/io
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmChangesetContentParser.java
r6090 r6201 9 9 import java.io.StringReader; 10 10 import java.io.UnsupportedEncodingException; 11 import java.util.Date;12 11 13 12 import javax.xml.parsers.ParserConfigurationException; 14 13 import javax.xml.parsers.SAXParserFactory; 15 14 16 import org.openstreetmap.josm.data.coor.LatLon;17 15 import org.openstreetmap.josm.data.osm.ChangesetDataSet; 18 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;19 import org.openstreetmap.josm.data.osm.RelationMemberData;20 import org.openstreetmap.josm.data.osm.User;21 16 import org.openstreetmap.josm.data.osm.ChangesetDataSet.ChangesetModificationType; 22 import org.openstreetmap.josm.data.osm.history.HistoryNode;23 import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;24 import org.openstreetmap.josm.data.osm.history.HistoryRelation;25 import org.openstreetmap.josm.data.osm.history.HistoryWay;26 17 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 27 18 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 28 19 import org.openstreetmap.josm.tools.CheckParameterUtil; 29 import org.openstreetmap.josm.tools.DateUtils;30 20 import org.xml.sax.Attributes; 31 21 import org.xml.sax.InputSource; 32 import org.xml.sax.Locator;33 22 import org.xml.sax.SAXException; 34 23 import org.xml.sax.SAXParseException; 35 import org.xml.sax.helpers.DefaultHandler;36 24 37 25 /** 38 26 * Parser for OSM changeset content. 39 * 27 * @since 2688 40 28 */ 41 29 public class OsmChangesetContentParser { 42 30 43 31 private InputSource source; 44 private ChangesetDataSet data;32 private final ChangesetDataSet data = new ChangesetDataSet(); 45 33 46 // FIXME: this class has many similarities with OsmHistoryReader.Parser and should be merged 47 private class Parser extends DefaultHandler { 34 private class Parser extends AbstractParser { 48 35 49 /** the current primitive to be read */50 private HistoryOsmPrimitive currentPrimitive;51 36 /** the current change modification type */ 52 37 private ChangesetDataSet.ChangesetModificationType currentModificationType; 53 38 54 private Locator locator;55 56 @Override57 public void setDocumentLocator(Locator locator) {58 this.locator = locator;59 }60 61 39 protected void throwException(String message) throws OsmDataParsingException { 62 throw new OsmDataParsingException( 63 message 64 ).rememberLocation(locator); 40 throw new OsmDataParsingException(message).rememberLocation(locator); 65 41 } 66 42 67 43 protected void throwException(Exception e) throws OsmDataParsingException { 68 throw new OsmDataParsingException( 69 e 70 ).rememberLocation(locator); 71 } 72 73 protected long getMandatoryAttributeLong(Attributes attr, String name) throws SAXException{ 74 String v = attr.getValue(name); 75 if (v == null) { 76 throwException(tr("Missing mandatory attribute ''{0}''.", name)); 77 } 78 Long l = 0L; 79 try { 80 l = Long.parseLong(v); 81 } catch(NumberFormatException e) { 82 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long. Got ''{1}''.", name, v)); 83 } 84 if (l < 0) { 85 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long (>=0). Got ''{1}''.", name, v)); 86 } 87 return l; 88 } 89 90 protected Long getAttributeLong(Attributes attr, String name) throws SAXException{ 91 String v = attr.getValue(name); 92 if (v == null) 93 return null; 94 Long l = null; 95 try { 96 l = Long.parseLong(v); 97 } catch(NumberFormatException e) { 98 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long. Got ''{1}''.", name, v)); 99 } 100 if (l < 0) { 101 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long (>=0). Got ''{1}''.", name, v)); 102 } 103 return l; 104 } 105 106 protected Double getAttributeDouble(Attributes attr, String name) throws SAXException{ 107 String v = attr.getValue(name); 108 if (v == null) { 109 return null; 110 } 111 double d = 0.0; 112 try { 113 d = Double.parseDouble(v); 114 } catch(NumberFormatException e) { 115 throwException(tr("Illegal value for attribute ''{0}'' of type double. Got ''{1}''.", name, v)); 116 } 117 return d; 118 } 119 120 protected String getMandatoryAttributeString(Attributes attr, String name) throws SAXException{ 121 String v = attr.getValue(name); 122 if (v == null) { 123 throwException(tr("Missing mandatory attribute ''{0}''.", name)); 124 } 125 return v; 126 } 127 128 protected boolean getMandatoryAttributeBoolean(Attributes attr, String name) throws SAXException{ 129 String v = attr.getValue(name); 130 if (v == null) { 131 throwException(tr("Missing mandatory attribute ''{0}''.", name)); 132 } 133 if ("true".equals(v)) return true; 134 if ("false".equals(v)) return false; 135 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type boolean. Got ''{1}''.", name, v)); 136 // not reached 137 return false; 138 } 139 140 protected HistoryOsmPrimitive createPrimitive(Attributes atts, OsmPrimitiveType type) throws SAXException { 141 long id = getMandatoryAttributeLong(atts,"id"); 142 long version = getMandatoryAttributeLong(atts,"version"); 143 long changesetId = getMandatoryAttributeLong(atts,"changeset"); 144 boolean visible= getMandatoryAttributeBoolean(atts, "visible"); 145 146 Long uid = getAttributeLong(atts, "uid"); 147 String userStr = atts.getValue("user"); 148 User user; 149 if (userStr != null) { 150 if (uid != null) { 151 user = User.createOsmUser(uid, userStr); 152 } else { 153 user = User.createLocalUser(userStr); 154 } 155 } else { 156 user = User.getAnonymous(); 157 } 158 159 String v = getMandatoryAttributeString(atts, "timestamp"); 160 Date timestamp = DateUtils.fromString(v); 161 HistoryOsmPrimitive primitive = null; 162 if (type.equals(OsmPrimitiveType.NODE)) { 163 Double lat = getAttributeDouble(atts, "lat"); 164 Double lon = getAttributeDouble(atts, "lon"); 165 LatLon coor = (lat != null && lon != null) ? new LatLon(lat,lon) : null; 166 primitive = new HistoryNode( 167 id,version,visible,user,changesetId,timestamp,coor 168 ); 169 170 } else if (type.equals(OsmPrimitiveType.WAY)) { 171 primitive = new HistoryWay( 172 id,version,visible,user,changesetId,timestamp 173 ); 174 }if (type.equals(OsmPrimitiveType.RELATION)) { 175 primitive = new HistoryRelation( 176 id,version,visible,user,changesetId,timestamp 177 ); 178 } 179 return primitive; 180 } 181 182 protected void startNode(Attributes atts) throws SAXException { 183 currentPrimitive= createPrimitive(atts, OsmPrimitiveType.NODE); 184 } 185 186 protected void startWay(Attributes atts) throws SAXException { 187 currentPrimitive= createPrimitive(atts, OsmPrimitiveType.WAY); 188 } 189 protected void startRelation(Attributes atts) throws SAXException { 190 currentPrimitive= createPrimitive(atts, OsmPrimitiveType.RELATION); 191 } 192 193 protected void handleTag(Attributes atts) throws SAXException { 194 String key= getMandatoryAttributeString(atts, "k"); 195 String value= getMandatoryAttributeString(atts, "v"); 196 currentPrimitive.put(key,value); 197 } 198 199 protected void handleNodeReference(Attributes atts) throws SAXException { 200 long ref = getMandatoryAttributeLong(atts, "ref"); 201 ((HistoryWay)currentPrimitive).addNode(ref); 202 } 203 204 protected void handleMember(Attributes atts) throws SAXException { 205 long ref = getMandatoryAttributeLong(atts, "ref"); 206 String v = getMandatoryAttributeString(atts, "type"); 207 OsmPrimitiveType type = null; 208 try { 209 type = OsmPrimitiveType.fromApiTypeName(v); 210 } catch(IllegalArgumentException e) { 211 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type OsmPrimitiveType. Got ''{1}''.", "type", v)); 212 } 213 String role = getMandatoryAttributeString(atts, "role"); 214 RelationMemberData member = new RelationMemberData(role, type,ref); 215 ((HistoryRelation)currentPrimitive).addMember(member); 44 throw new OsmDataParsingException(e).rememberLocation(locator); 216 45 } 217 46 218 47 @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 219 if (qName.equals("node")) { 220 startNode(atts); 221 } else if (qName.equals("way")) { 222 startWay(atts); 223 } else if (qName.equals("relation")) { 224 startRelation(atts); 225 } else if (qName.equals("tag")) { 226 handleTag(atts); 227 } else if (qName.equals("nd")) { 228 handleNodeReference(atts); 229 } else if (qName.equals("member")) { 230 handleMember(atts); 48 if (super.doStartElement(qName, atts)) { 49 // done 231 50 } else if (qName.equals("osmChange")) { 232 51 // do nothing … … 282 101 283 102 /** 284 * C reate a parser103 * Constructs a new {@code OsmChangesetContentParser}. 285 104 * 286 105 * @param source the input stream with the changeset content as XML document. Must not be null. 287 * @throws IllegalArgumentException thrown if source is null. 106 * @throws UnsupportedEncodingException if {@code UTF-8} charset is missing 107 * @throws IllegalArgumentException if source is {@code null}. 288 108 */ 289 109 public OsmChangesetContentParser(InputStream source) throws UnsupportedEncodingException { 290 110 CheckParameterUtil.ensureParameterNotNull(source, "source"); 291 111 this.source = new InputSource(new InputStreamReader(source, "UTF-8")); 292 data = new ChangesetDataSet();293 112 } 294 113 114 /** 115 * Constructs a new {@code OsmChangesetContentParser}. 116 * 117 * @param source the input stream with the changeset content as XML document. Must not be null. 118 * @throws IllegalArgumentException if source is {@code null}. 119 */ 295 120 public OsmChangesetContentParser(String source) { 296 121 CheckParameterUtil.ensureParameterNotNull(source, "source"); 297 122 this.source = new InputSource(new StringReader(source)); 298 data = new ChangesetDataSet();299 123 } 300 124 301 125 /** 302 * Parses the content 126 * Parses the content. 303 127 * 304 * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} 305 * if null 128 * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null 306 129 * @return the parsed data 307 130 * @throws OsmDataParsingException thrown if something went wrong. Check for chained -
trunk/src/org/openstreetmap/josm/io/OsmHistoryReader.java
r6090 r6201 7 7 import java.io.InputStream; 8 8 import java.io.InputStreamReader; 9 import java.util.Date;10 9 11 10 import javax.xml.parsers.ParserConfigurationException; 12 11 import javax.xml.parsers.SAXParserFactory; 13 12 14 import org.openstreetmap.josm.data.coor.LatLon;15 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;16 import org.openstreetmap.josm.data.osm.RelationMemberData;17 import org.openstreetmap.josm.data.osm.User;18 13 import org.openstreetmap.josm.data.osm.history.HistoryDataSet; 19 import org.openstreetmap.josm.data.osm.history.HistoryNode;20 14 import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive; 21 import org.openstreetmap.josm.data.osm.history.HistoryRelation; 22 import org.openstreetmap.josm.data.osm.history.HistoryWay; 15 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 23 16 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 24 import org.openstreetmap.josm.tools. DateUtils;17 import org.openstreetmap.josm.tools.CheckParameterUtil; 25 18 import org.xml.sax.Attributes; 26 19 import org.xml.sax.InputSource; 27 import org.xml.sax.Locator;28 20 import org.xml.sax.SAXException; 29 import org.xml.sax.helpers.DefaultHandler;30 21 31 22 /** … … 35 26 * {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s. We use objects derived from 36 27 * {@link HistoryOsmPrimitive} instead and we keep the data in a dedicated {@link HistoryDataSet}. 37 * 28 * @since 1670 38 29 */ 39 30 public class OsmHistoryReader { … … 42 33 private final HistoryDataSet data; 43 34 44 // FIXME: this class has many similarities with OsmChangesetContentParser.Parser and should be merged 45 private class Parser extends DefaultHandler { 46 47 /** the current primitive to be read */ 48 private HistoryOsmPrimitive current; 49 private Locator locator; 50 51 @Override 52 public void setDocumentLocator(Locator locator) { 53 this.locator = locator; 54 } 35 private class Parser extends AbstractParser { 55 36 56 37 protected String getCurrentPosition() { … … 61 42 62 43 protected void throwException(String message) throws SAXException { 63 throw new SAXException( 64 getCurrentPosition() 65 + message 66 ); 44 throw new SAXException(getCurrentPosition() + message); 67 45 } 68 46 69 protected long getMandatoryAttributeLong(Attributes attr, String name) throws SAXException{ 70 String v = attr.getValue(name); 71 if (v == null) { 72 throwException(tr("Missing mandatory attribute ''{0}''.", name)); 73 } 74 Long l = 0L; 75 try { 76 l = Long.parseLong(v); 77 } catch(NumberFormatException e) { 78 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long. Got ''{1}''.", name, v)); 79 } 80 if (l < 0) { 81 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long (>=0). Got ''{1}''.", name, v)); 82 } 83 return l; 84 } 85 86 protected Long getAttributeLong(Attributes attr, String name) throws SAXException { 87 String v = attr.getValue(name); 88 if (v == null) 89 return null; 90 Long l = null; 91 try { 92 l = Long.parseLong(v); 93 } catch(NumberFormatException e) { 94 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long. Got ''{1}''.", name, v)); 95 } 96 if (l < 0) { 97 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long (>=0). Got ''{1}''.", name, v)); 98 } 99 return l; 100 } 101 102 protected Double getAttributeDouble(Attributes attr, String name) throws SAXException{ 103 String v = attr.getValue(name); 104 if (v == null) { 105 return null; 106 } 107 double d = 0.0; 108 try { 109 d = Double.parseDouble(v); 110 } catch(NumberFormatException e) { 111 throwException(tr("Illegal value for attribute ''{0}'' of type double. Got ''{1}''.", name, v)); 112 } 113 return d; 114 } 115 116 protected String getMandatoryAttributeString(Attributes attr, String name) throws SAXException{ 117 String v = attr.getValue(name); 118 if (v == null) { 119 throwException(tr("Missing mandatory attribute ''{0}''.", name)); 120 } 121 return v; 122 } 123 124 protected boolean getMandatoryAttributeBoolean(Attributes attr, String name) throws SAXException{ 125 String v = attr.getValue(name); 126 if (v == null) { 127 throwException(tr("Missing mandatory attribute ''{0}''.", name)); 128 } 129 if ("true".equals(v)) return true; 130 if ("false".equals(v)) return false; 131 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type boolean. Got ''{1}''.", name, v)); 132 // not reached 133 return false; 134 } 135 136 protected HistoryOsmPrimitive createPrimitive(Attributes atts, OsmPrimitiveType type) throws SAXException { 137 long id = getMandatoryAttributeLong(atts,"id"); 138 long version = getMandatoryAttributeLong(atts,"version"); 139 long changesetId = getMandatoryAttributeLong(atts,"changeset"); 140 boolean visible= getMandatoryAttributeBoolean(atts, "visible"); 141 Long uid = getAttributeLong(atts, "uid"); 142 String userStr = atts.getValue("user"); 143 User user; 144 if (userStr != null) { 145 if (uid != null) { 146 user = User.createOsmUser(uid, userStr); 147 } else { 148 user = User.createLocalUser(userStr); 149 } 150 } else { 151 user = User.getAnonymous(); 152 } 153 String v = getMandatoryAttributeString(atts, "timestamp"); 154 Date timestamp = DateUtils.fromString(v); 155 HistoryOsmPrimitive primitive = null; 156 if (type.equals(OsmPrimitiveType.NODE)) { 157 Double lat = getAttributeDouble(atts, "lat"); 158 Double lon = getAttributeDouble(atts, "lon"); 159 LatLon coord = (lat != null && lon != null) ? new LatLon(lat,lon) : null; 160 primitive = new HistoryNode( 161 id,version,visible,user,changesetId,timestamp,coord 162 ); 163 164 } else if (type.equals(OsmPrimitiveType.WAY)) { 165 primitive = new HistoryWay( 166 id,version,visible,user,changesetId,timestamp 167 ); 168 }if (type.equals(OsmPrimitiveType.RELATION)) { 169 primitive = new HistoryRelation( 170 id,version,visible,user,changesetId,timestamp 171 ); 172 } 173 return primitive; 174 } 175 176 protected void startNode(Attributes atts) throws SAXException { 177 current= createPrimitive(atts, OsmPrimitiveType.NODE); 178 } 179 180 protected void startWay(Attributes atts) throws SAXException { 181 current= createPrimitive(atts, OsmPrimitiveType.WAY); 182 } 183 protected void startRelation(Attributes atts) throws SAXException { 184 current= createPrimitive(atts, OsmPrimitiveType.RELATION); 185 } 186 187 protected void handleTag(Attributes atts) throws SAXException { 188 String key= getMandatoryAttributeString(atts, "k"); 189 String value= getMandatoryAttributeString(atts, "v"); 190 current.put(key,value); 191 } 192 193 protected void handleNodeReference(Attributes atts) throws SAXException { 194 long ref = getMandatoryAttributeLong(atts, "ref"); 195 ((HistoryWay)current).addNode(ref); 196 } 197 198 protected void handleMember(Attributes atts) throws SAXException { 199 long ref = getMandatoryAttributeLong(atts, "ref"); 200 String v = getMandatoryAttributeString(atts, "type"); 201 OsmPrimitiveType type = null; 202 try { 203 type = OsmPrimitiveType.fromApiTypeName(v); 204 } catch(IllegalArgumentException e) { 205 throwException(tr("Illegal value for mandatory attribute ''{0}'' of type OsmPrimitiveType. Got ''{1}''.", "type", v)); 206 } 207 String role = getMandatoryAttributeString(atts, "role"); 208 RelationMemberData member = new RelationMemberData(role, type,ref); 209 ((HistoryRelation)current).addMember(member); 210 } 211 212 @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 213 if (qName.equals("node")) { 214 startNode(atts); 215 } else if (qName.equals("way")) { 216 startWay(atts); 217 } else if (qName.equals("relation")) { 218 startRelation(atts); 219 } else if (qName.equals("tag")) { 220 handleTag(atts); 221 } else if (qName.equals("nd")) { 222 handleNodeReference(atts); 223 } else if (qName.equals("member")) { 224 handleMember(atts); 225 } 47 @Override 48 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 49 doStartElement(qName, atts); 226 50 } 227 51 … … 231 55 || qName.equals("way") 232 56 || qName.equals("relation")) { 233 data.put(current );57 data.put(currentPrimitive); 234 58 } 235 59 } 236 60 } 237 61 62 /** 63 * Constructs a new {@code OsmHistoryReader}. 64 * 65 * @param source the input stream with the history content as XML document. Must not be null. 66 * @throws IllegalArgumentException if source is {@code null}. 67 */ 238 68 public OsmHistoryReader(InputStream source) { 69 CheckParameterUtil.ensureParameterNotNull(source, "source"); 239 70 this.in = source; 240 71 this.data = new HistoryDataSet(); 241 72 } 242 73 74 /** 75 * Parses the content. 76 * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null 77 * @return the parsed data 78 * @throws SAXException If any SAX errors occur during processing. 79 * @throws IOException If any IO errors occur. 80 */ 243 81 public HistoryDataSet parse(ProgressMonitor progressMonitor) throws SAXException, IOException { 244 82 InputSource inputSource = new InputSource(new InputStreamReader(in, "UTF-8")); … … 246 84 try { 247 85 SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new Parser()); 248 } catch (ParserConfigurationException e 1) {249 e 1.printStackTrace(); // broken SAXException chaining250 throw new SAXException(e 1);86 } catch (ParserConfigurationException e) { 87 e.printStackTrace(); // broken SAXException chaining 88 throw new SAXException(e); 251 89 } finally { 252 90 progressMonitor.finishTask();
Note:
See TracChangeset
for help on using the changeset viewer.