001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.io.IOException;
007import java.io.StringReader;
008import java.util.Collection;
009import java.util.Collections;
010import java.util.Date;
011import java.util.HashMap;
012import java.util.HashSet;
013import java.util.Map;
014import java.util.Optional;
015import java.util.Set;
016
017import javax.xml.parsers.ParserConfigurationException;
018
019import org.openstreetmap.josm.data.osm.Changeset;
020import org.openstreetmap.josm.data.osm.DataSet;
021import org.openstreetmap.josm.data.osm.OsmPrimitive;
022import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
023import org.openstreetmap.josm.data.osm.PrimitiveId;
024import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
025import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
026import org.openstreetmap.josm.gui.progress.ProgressMonitor;
027import org.openstreetmap.josm.tools.CheckParameterUtil;
028import org.openstreetmap.josm.tools.Utils;
029import org.openstreetmap.josm.tools.XmlParsingException;
030import org.xml.sax.Attributes;
031import org.xml.sax.InputSource;
032import org.xml.sax.Locator;
033import org.xml.sax.SAXException;
034import org.xml.sax.helpers.DefaultHandler;
035
036/**
037 * Helper class to process the OSM API server response to a "diff" upload.
038 * <p>
039 * New primitives (uploaded with negative id) will be assigned a positive id, etc.
040 * The goal is to have a clean state, just like a fresh download (assuming no
041 * concurrent uploads by other users have happened in the meantime).
042 * <p>
043 * @see <a href="https://wiki.openstreetmap.org/wiki/API_v0.6#Response_10">API 0.6 diff upload response</a>
044 */
045public class DiffResultProcessor {
046
047    private static class DiffResultEntry {
048        private long newId;
049        private int newVersion;
050    }
051
052    /**
053     * mapping from old id to new id and version, the result of parsing the diff result
054     * replied by the server
055     */
056    private final Map<PrimitiveId, DiffResultEntry> diffResults = new HashMap<>();
057    /**
058     * the set of processed primitives *after* the new id, the new version and the new changeset id is set
059     */
060    private final Set<OsmPrimitive> processed;
061    /**
062     * the collection of primitives being uploaded
063     */
064    private final Collection<? extends OsmPrimitive> primitives;
065
066    /**
067     * Creates a diff result reader
068     *
069     * @param primitives the collection of primitives which have been uploaded. If null,
070     * assumes an empty collection.
071     */
072    public DiffResultProcessor(Collection<? extends OsmPrimitive> primitives) {
073        this.primitives = Optional.ofNullable(primitives).orElseGet(Collections::emptyList);
074        this.processed = new HashSet<>();
075    }
076
077    /**
078     * Parse the response from a diff upload to the OSM API.
079     *
080     * @param diffUploadResponse the response. Must not be null.
081     * @param progressMonitor a progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null
082     * @throws IllegalArgumentException if diffUploadRequest is null
083     * @throws XmlParsingException if the diffUploadRequest can't be parsed successfully
084     *
085     */
086    public void parse(String diffUploadResponse, ProgressMonitor progressMonitor) throws XmlParsingException {
087        if (progressMonitor == null) {
088            progressMonitor = NullProgressMonitor.INSTANCE;
089        }
090        CheckParameterUtil.ensureParameterNotNull(diffUploadResponse, "diffUploadResponse");
091        try {
092            progressMonitor.beginTask(tr("Parsing response from server..."));
093            InputSource inputSource = new InputSource(new StringReader(diffUploadResponse));
094            Utils.parseSafeSAX(inputSource, new Parser());
095        } catch (XmlParsingException e) {
096            throw e;
097        } catch (IOException | ParserConfigurationException | SAXException e) {
098            throw new XmlParsingException(e);
099        } finally {
100            progressMonitor.finishTask();
101        }
102    }
103
104    /**
105     * Postprocesses the diff result read and parsed from the server.
106     *
107     * Uploaded objects are assigned their new id (if they got assigned a new
108     * id by the server), their new version (if the version was incremented),
109     * and the id of the changeset to which they were uploaded.
110     *
111     * @param cs the current changeset. Ignored if null.
112     * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
113     * @return the collection of processed primitives
114     */
115    protected Set<OsmPrimitive> postProcess(Changeset cs, ProgressMonitor monitor) {
116        if (monitor == null) {
117            monitor = NullProgressMonitor.INSTANCE;
118        }
119        DataSet ds = null;
120        if (!primitives.isEmpty()) {
121            ds = primitives.iterator().next().getDataSet();
122        }
123        boolean readOnly = false;
124        if (ds != null) {
125            readOnly = ds.isLocked();
126            if (readOnly) {
127                ds.unlock();
128            }
129            ds.beginUpdate();
130        }
131        try {
132            monitor.beginTask("Postprocessing uploaded data ...");
133            monitor.setTicksCount(primitives.size());
134            monitor.setTicks(0);
135            for (OsmPrimitive p : primitives) {
136                monitor.worked(1);
137                DiffResultEntry entry = diffResults.get(p.getPrimitiveId());
138                if (entry == null) {
139                    continue;
140                }
141                processed.add(p);
142                if (!p.isDeleted()) {
143                    p.setOsmId(entry.newId, entry.newVersion);
144                    p.setVisible(true);
145                } else {
146                    p.setVisible(false);
147                }
148                if (cs != null && !cs.isNew()) {
149                    p.setChangesetId(cs.getId());
150                    p.setUser(cs.getUser());
151                    // TODO is there a way to obtain the timestamp for non-closed changesets?
152                    p.setTimestamp(Utils.firstNonNull(cs.getClosedAt(), new Date()));
153                }
154            }
155            return processed;
156        } finally {
157            if (ds != null) {
158                ds.endUpdate();
159                if (readOnly) {
160                    ds.lock();
161                }
162            }
163            monitor.finishTask();
164        }
165    }
166
167    private class Parser extends DefaultHandler {
168        private Locator locator;
169
170        @Override
171        public void setDocumentLocator(Locator locator) {
172            this.locator = locator;
173        }
174
175        protected void throwException(String msg) throws XmlParsingException {
176            throw new XmlParsingException(msg).rememberLocation(locator);
177        }
178
179        @Override
180        public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
181            try {
182                switch (qName) {
183                case "diffResult":
184                    // the root element, ignore
185                    break;
186                case "node":
187                case "way":
188                case "relation":
189                    PrimitiveId id = new SimplePrimitiveId(
190                            Long.parseLong(atts.getValue("old_id")),
191                            OsmPrimitiveType.fromApiTypeName(qName)
192                    );
193                    DiffResultEntry entry = new DiffResultEntry();
194                    if (atts.getValue("new_id") != null) {
195                        entry.newId = Long.parseLong(atts.getValue("new_id"));
196                    }
197                    if (atts.getValue("new_version") != null) {
198                        entry.newVersion = Integer.parseInt(atts.getValue("new_version"));
199                    }
200                    diffResults.put(id, entry);
201                    break;
202                default:
203                    throwException(tr("Unexpected XML element with name ''{0}''", qName));
204                }
205            } catch (NumberFormatException e) {
206                throw new XmlParsingException(e).rememberLocation(locator);
207            }
208        }
209    }
210}