[7937] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.util.Date;
|
---|
| 7 |
|
---|
| 8 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
| 9 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
| 10 | import org.openstreetmap.josm.data.osm.RelationMemberData;
|
---|
| 11 | import org.openstreetmap.josm.data.osm.User;
|
---|
| 12 | import org.openstreetmap.josm.data.osm.history.HistoryNode;
|
---|
| 13 | import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
|
---|
| 14 | import org.openstreetmap.josm.data.osm.history.HistoryRelation;
|
---|
| 15 | import org.openstreetmap.josm.data.osm.history.HistoryWay;
|
---|
| 16 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
| 17 | import org.xml.sax.Attributes;
|
---|
| 18 | import org.xml.sax.Locator;
|
---|
| 19 | import org.xml.sax.SAXException;
|
---|
| 20 | import org.xml.sax.helpers.DefaultHandler;
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * Base class of {@link OsmChangesetContentParser} and {@link OsmHistoryReader} internal parsers.
|
---|
| 24 | * @since 6201
|
---|
| 25 | */
|
---|
| 26 | public abstract class AbstractParser extends DefaultHandler {
|
---|
[8308] | 27 |
|
---|
[7937] | 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 | }
|
---|
[8308] | 36 |
|
---|
[7937] | 37 | protected abstract void throwException(String message) throws SAXException;
|
---|
| 38 |
|
---|
[8308] | 39 | protected abstract void throwException(String message, Exception e) throws SAXException;
|
---|
| 40 |
|
---|
[7937] | 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 | }
|
---|
[8148] | 46 | long l = 0L;
|
---|
[7937] | 47 | try {
|
---|
| 48 | l = Long.parseLong(v);
|
---|
[8510] | 49 | } catch (NumberFormatException e) {
|
---|
[8308] | 50 | throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long. Got ''{1}''.", name, v), e);
|
---|
[7937] | 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 {
|
---|
[8390] | 64 | l = Long.valueOf(v);
|
---|
[8510] | 65 | } catch (NumberFormatException e) {
|
---|
[8308] | 66 | throwException(tr("Illegal value for mandatory attribute ''{0}'' of type long. Got ''{1}''.", name, v), e);
|
---|
[7937] | 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);
|
---|
[8510] | 82 | } catch (NumberFormatException e) {
|
---|
[8308] | 83 | throwException(tr("Illegal value for attribute ''{0}'' of type double. Got ''{1}''.", name, v), e);
|
---|
[7937] | 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 | }
|
---|
[8308] | 106 |
|
---|
[7937] | 107 | protected final HistoryOsmPrimitive createPrimitive(Attributes atts, OsmPrimitiveType type) throws SAXException {
|
---|
[8510] | 108 | long id = getMandatoryAttributeLong(atts, "id");
|
---|
| 109 | long version = getMandatoryAttributeLong(atts, "version");
|
---|
| 110 | long changesetId = getMandatoryAttributeLong(atts, "changeset");
|
---|
| 111 | boolean visible = getMandatoryAttributeBoolean(atts, "visible");
|
---|
[7937] | 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);
|
---|
[10841] | 119 | user.setPreferredName(userStr);
|
---|
[7937] | 120 | } else {
|
---|
| 121 | user = User.createLocalUser(userStr);
|
---|
| 122 | }
|
---|
| 123 | } else {
|
---|
| 124 | user = User.getAnonymous();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | String v = getMandatoryAttributeString(atts, "timestamp");
|
---|
| 128 | Date timestamp = DateUtils.fromString(v);
|
---|
| 129 | HistoryOsmPrimitive primitive = null;
|
---|
| 130 | if (type.equals(OsmPrimitiveType.NODE)) {
|
---|
| 131 | Double lat = getAttributeDouble(atts, "lat");
|
---|
| 132 | Double lon = getAttributeDouble(atts, "lon");
|
---|
[8510] | 133 | LatLon coor = (lat != null && lon != null) ? new LatLon(lat, lon) : null;
|
---|
| 134 | primitive = new HistoryNode(id, version, visible, user, changesetId, timestamp, coor);
|
---|
[7937] | 135 |
|
---|
| 136 | } else if (type.equals(OsmPrimitiveType.WAY)) {
|
---|
[8510] | 137 | primitive = new HistoryWay(id, version, visible, user, changesetId, timestamp);
|
---|
[7937] | 138 | } else if (type.equals(OsmPrimitiveType.RELATION)) {
|
---|
[8510] | 139 | primitive = new HistoryRelation(id, version, visible, user, changesetId, timestamp);
|
---|
[7937] | 140 | }
|
---|
| 141 | return primitive;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | protected final void startNode(Attributes atts) throws SAXException {
|
---|
| 145 | currentPrimitive = createPrimitive(atts, OsmPrimitiveType.NODE);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | protected final void startWay(Attributes atts) throws SAXException {
|
---|
| 149 | currentPrimitive = createPrimitive(atts, OsmPrimitiveType.WAY);
|
---|
| 150 | }
|
---|
[8308] | 151 |
|
---|
[7937] | 152 | protected final void startRelation(Attributes atts) throws SAXException {
|
---|
| 153 | currentPrimitive = createPrimitive(atts, OsmPrimitiveType.RELATION);
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | protected final void handleTag(Attributes atts) throws SAXException {
|
---|
| 157 | String key = getMandatoryAttributeString(atts, "k");
|
---|
| 158 | String value = getMandatoryAttributeString(atts, "v");
|
---|
[8510] | 159 | currentPrimitive.put(key, value);
|
---|
[7937] | 160 | }
|
---|
| 161 |
|
---|
| 162 | protected final void handleNodeReference(Attributes atts) throws SAXException {
|
---|
| 163 | long ref = getMandatoryAttributeLong(atts, "ref");
|
---|
[8510] | 164 | ((HistoryWay) currentPrimitive).addNode(ref);
|
---|
[7937] | 165 | }
|
---|
| 166 |
|
---|
| 167 | protected void handleMember(Attributes atts) throws SAXException {
|
---|
| 168 | long ref = getMandatoryAttributeLong(atts, "ref");
|
---|
| 169 | String v = getMandatoryAttributeString(atts, "type");
|
---|
| 170 | OsmPrimitiveType type = null;
|
---|
| 171 | try {
|
---|
| 172 | type = OsmPrimitiveType.fromApiTypeName(v);
|
---|
[8510] | 173 | } catch (IllegalArgumentException e) {
|
---|
[8308] | 174 | throwException(tr("Illegal value for mandatory attribute ''{0}'' of type OsmPrimitiveType. Got ''{1}''.", "type", v), e);
|
---|
[7937] | 175 | }
|
---|
| 176 | String role = getMandatoryAttributeString(atts, "role");
|
---|
[8510] | 177 | RelationMemberData member = new RelationMemberData(role, type, ref);
|
---|
| 178 | ((HistoryRelation) currentPrimitive).addMember(member);
|
---|
[7937] | 179 | }
|
---|
[8308] | 180 |
|
---|
[7937] | 181 | protected final boolean doStartElement(String qName, Attributes atts) throws SAXException {
|
---|
| 182 | switch (qName) {
|
---|
| 183 | case "node":
|
---|
| 184 | startNode(atts);
|
---|
| 185 | return true;
|
---|
| 186 | case "way":
|
---|
| 187 | startWay(atts);
|
---|
| 188 | return true;
|
---|
| 189 | case "relation":
|
---|
| 190 | startRelation(atts);
|
---|
| 191 | return true;
|
---|
| 192 | case "tag":
|
---|
| 193 | handleTag(atts);
|
---|
| 194 | return true;
|
---|
| 195 | case "nd":
|
---|
| 196 | handleNodeReference(atts);
|
---|
| 197 | return true;
|
---|
| 198 | case "member":
|
---|
| 199 | handleMember(atts);
|
---|
| 200 | return true;
|
---|
| 201 | default:
|
---|
| 202 | return false;
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | }
|
---|