| 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.io.IOException;
|
|---|
| 7 | import java.io.StringReader;
|
|---|
| 8 | import java.time.Instant;
|
|---|
| 9 | import java.util.Collection;
|
|---|
| 10 | import java.util.Collections;
|
|---|
| 11 | import java.util.HashMap;
|
|---|
| 12 | import java.util.HashSet;
|
|---|
| 13 | import java.util.Map;
|
|---|
| 14 | import java.util.Optional;
|
|---|
| 15 | import java.util.Set;
|
|---|
| 16 |
|
|---|
| 17 | import javax.xml.parsers.ParserConfigurationException;
|
|---|
| 18 |
|
|---|
| 19 | import org.openstreetmap.josm.data.osm.Changeset;
|
|---|
| 20 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 21 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 22 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
|---|
| 23 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
|---|
| 24 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
|---|
| 25 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
|---|
| 26 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
|---|
| 27 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
|---|
| 28 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 29 | import org.openstreetmap.josm.tools.XmlParsingException;
|
|---|
| 30 | import org.openstreetmap.josm.tools.XmlUtils;
|
|---|
| 31 | import org.xml.sax.Attributes;
|
|---|
| 32 | import org.xml.sax.InputSource;
|
|---|
| 33 | import org.xml.sax.Locator;
|
|---|
| 34 | import org.xml.sax.SAXException;
|
|---|
| 35 | import org.xml.sax.helpers.DefaultHandler;
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Helper class to process the OSM API server response to a "diff" upload.
|
|---|
| 39 | * <p>
|
|---|
| 40 | * New primitives (uploaded with negative id) will be assigned a positive id, etc.
|
|---|
| 41 | * The goal is to have a clean state, just like a fresh download (assuming no
|
|---|
| 42 | * concurrent uploads by other users have happened in the meantime).
|
|---|
| 43 | * <p>
|
|---|
| 44 | * @see <a href="https://wiki.openstreetmap.org/wiki/API_v0.6#Response_10">API 0.6 diff upload response</a>
|
|---|
| 45 | */
|
|---|
| 46 | public class DiffResultProcessor {
|
|---|
| 47 |
|
|---|
| 48 | static class DiffResultEntry {
|
|---|
| 49 | long newId;
|
|---|
| 50 | int newVersion;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * mapping from old id to new id and version, the result of parsing the diff result
|
|---|
| 55 | * replied by the server
|
|---|
| 56 | */
|
|---|
| 57 | private final Map<PrimitiveId, DiffResultEntry> diffResults = new HashMap<>();
|
|---|
| 58 | /**
|
|---|
| 59 | * the set of processed primitives *after* the new id, the new version and the new changeset id is set
|
|---|
| 60 | */
|
|---|
| 61 | private final Set<OsmPrimitive> processed;
|
|---|
| 62 | /**
|
|---|
| 63 | * the collection of primitives being uploaded
|
|---|
| 64 | */
|
|---|
| 65 | private final Collection<? extends OsmPrimitive> primitives;
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Creates a diff result reader
|
|---|
| 69 | *
|
|---|
| 70 | * @param primitives the collection of primitives which have been uploaded. If null,
|
|---|
| 71 | * assumes an empty collection.
|
|---|
| 72 | */
|
|---|
| 73 | public DiffResultProcessor(Collection<? extends OsmPrimitive> primitives) {
|
|---|
| 74 | this.primitives = Optional.ofNullable(primitives).orElseGet(Collections::emptyList);
|
|---|
| 75 | this.processed = new HashSet<>();
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * Parse the response from a diff upload to the OSM API.
|
|---|
| 80 | *
|
|---|
| 81 | * @param diffUploadResponse the response. Must not be null.
|
|---|
| 82 | * @param progressMonitor a progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null
|
|---|
| 83 | * @throws IllegalArgumentException if diffUploadRequest is null
|
|---|
| 84 | * @throws XmlParsingException if the diffUploadRequest can't be parsed successfully
|
|---|
| 85 | *
|
|---|
| 86 | */
|
|---|
| 87 | public void parse(String diffUploadResponse, ProgressMonitor progressMonitor) throws XmlParsingException {
|
|---|
| 88 | if (progressMonitor == null) {
|
|---|
| 89 | progressMonitor = NullProgressMonitor.INSTANCE;
|
|---|
| 90 | }
|
|---|
| 91 | CheckParameterUtil.ensureParameterNotNull(diffUploadResponse, "diffUploadResponse");
|
|---|
| 92 | try {
|
|---|
| 93 | progressMonitor.beginTask(tr("Parsing response from server..."));
|
|---|
| 94 | InputSource inputSource = new InputSource(new StringReader(diffUploadResponse));
|
|---|
| 95 | XmlUtils.parseSafeSAX(inputSource, new Parser());
|
|---|
| 96 | } catch (XmlParsingException e) {
|
|---|
| 97 | throw e;
|
|---|
| 98 | } catch (IOException | ParserConfigurationException | SAXException e) {
|
|---|
| 99 | throw new XmlParsingException(e);
|
|---|
| 100 | } finally {
|
|---|
| 101 | progressMonitor.finishTask();
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /**
|
|---|
| 106 | * Postprocesses the diff result read and parsed from the server.
|
|---|
| 107 | * <p>
|
|---|
| 108 | * Uploaded objects are assigned their new id (if they got assigned a new
|
|---|
| 109 | * id by the server), their new version (if the version was incremented),
|
|---|
| 110 | * and the id of the changeset to which they were uploaded.
|
|---|
| 111 | *
|
|---|
| 112 | * @param cs the current changeset. Ignored if null.
|
|---|
| 113 | * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
|---|
| 114 | * @return the collection of processed primitives
|
|---|
| 115 | */
|
|---|
| 116 | protected Set<OsmPrimitive> postProcess(Changeset cs, ProgressMonitor monitor) {
|
|---|
| 117 | if (monitor == null) {
|
|---|
| 118 | monitor = NullProgressMonitor.INSTANCE;
|
|---|
| 119 | }
|
|---|
| 120 | DataSet ds = null;
|
|---|
| 121 | if (!primitives.isEmpty()) {
|
|---|
| 122 | ds = primitives.iterator().next().getDataSet();
|
|---|
| 123 | }
|
|---|
| 124 | boolean readOnly = false;
|
|---|
| 125 | if (ds != null) {
|
|---|
| 126 | readOnly = ds.isLocked();
|
|---|
| 127 | if (readOnly) {
|
|---|
| 128 | ds.unlock();
|
|---|
| 129 | }
|
|---|
| 130 | ds.beginUpdate();
|
|---|
| 131 | }
|
|---|
| 132 | try {
|
|---|
| 133 | monitor.beginTask(tr("Postprocessing uploaded data..."));
|
|---|
| 134 | monitor.setTicksCount(primitives.size());
|
|---|
| 135 | monitor.setTicks(0);
|
|---|
| 136 | for (OsmPrimitive p : primitives) {
|
|---|
| 137 | monitor.worked(1);
|
|---|
| 138 | DiffResultEntry entry = diffResults.get(p.getPrimitiveId());
|
|---|
| 139 | if (entry == null) {
|
|---|
| 140 | continue;
|
|---|
| 141 | }
|
|---|
| 142 | processed.add(p);
|
|---|
| 143 | if (!p.isDeleted()) {
|
|---|
| 144 | p.setOsmId(entry.newId, entry.newVersion);
|
|---|
| 145 | p.setVisible(true);
|
|---|
| 146 | } else {
|
|---|
| 147 | p.setVisible(false);
|
|---|
| 148 | }
|
|---|
| 149 | if (cs != null && !cs.isNew()) {
|
|---|
| 150 | p.setChangesetId(cs.getId());
|
|---|
| 151 | p.setUser(cs.getUser());
|
|---|
| 152 | // TODO is there a way to obtain the timestamp for non-closed changesets?
|
|---|
| 153 | Instant instant = Utils.firstNonNull(cs.getClosedAt(), Instant.now());
|
|---|
| 154 | p.setInstant(instant);
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 | return processed;
|
|---|
| 158 | } finally {
|
|---|
| 159 | if (ds != null) {
|
|---|
| 160 | ds.endUpdate();
|
|---|
| 161 | if (readOnly) {
|
|---|
| 162 | ds.lock();
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 | monitor.finishTask();
|
|---|
| 166 | }
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | private final class Parser extends DefaultHandler {
|
|---|
| 170 | private Locator locator;
|
|---|
| 171 |
|
|---|
| 172 | @Override
|
|---|
| 173 | public void setDocumentLocator(Locator locator) {
|
|---|
| 174 | this.locator = locator;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | void throwException(String msg) throws XmlParsingException {
|
|---|
| 178 | throw new XmlParsingException(msg).rememberLocation(locator);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | @Override
|
|---|
| 182 | public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
|---|
| 183 | try {
|
|---|
| 184 | switch (qName) {
|
|---|
| 185 | case "diffResult":
|
|---|
| 186 | // the root element, ignore
|
|---|
| 187 | break;
|
|---|
| 188 | case "node":
|
|---|
| 189 | case "way":
|
|---|
| 190 | case "relation":
|
|---|
| 191 | PrimitiveId id = new SimplePrimitiveId(
|
|---|
| 192 | Long.parseLong(atts.getValue("old_id")),
|
|---|
| 193 | OsmPrimitiveType.fromApiTypeName(qName)
|
|---|
| 194 | );
|
|---|
| 195 | DiffResultEntry entry = new DiffResultEntry();
|
|---|
| 196 | if (atts.getValue("new_id") != null) {
|
|---|
| 197 | entry.newId = Long.parseLong(atts.getValue("new_id"));
|
|---|
| 198 | }
|
|---|
| 199 | if (atts.getValue("new_version") != null) {
|
|---|
| 200 | entry.newVersion = Integer.parseInt(atts.getValue("new_version"));
|
|---|
| 201 | }
|
|---|
| 202 | diffResults.put(id, entry);
|
|---|
| 203 | break;
|
|---|
| 204 | default:
|
|---|
| 205 | throwException(tr("Unexpected XML element with name ''{0}''", qName));
|
|---|
| 206 | }
|
|---|
| 207 | } catch (NumberFormatException e) {
|
|---|
| 208 | throw new XmlParsingException(e).rememberLocation(locator);
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | final Map<PrimitiveId, DiffResultEntry> getDiffResults() {
|
|---|
| 214 | return new HashMap<>(diffResults);
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|