| 1 | //License: GPL. Copyright 2007 by Immanuel Scholz and others
|
|---|
| 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.util.Collection;
|
|---|
| 9 | import java.util.Collections;
|
|---|
| 10 | import java.util.HashMap;
|
|---|
| 11 | import java.util.HashSet;
|
|---|
| 12 | import java.util.Map;
|
|---|
| 13 | import java.util.Set;
|
|---|
| 14 |
|
|---|
| 15 | import javax.xml.parsers.ParserConfigurationException;
|
|---|
| 16 | import javax.xml.parsers.SAXParserFactory;
|
|---|
| 17 |
|
|---|
| 18 | import org.openstreetmap.josm.data.osm.Changeset;
|
|---|
| 19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 20 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
|---|
| 21 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
|---|
| 22 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
|---|
| 23 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
|---|
| 24 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
|---|
| 25 | import org.xml.sax.Attributes;
|
|---|
| 26 | import org.xml.sax.InputSource;
|
|---|
| 27 | import org.xml.sax.Locator;
|
|---|
| 28 | import org.xml.sax.SAXException;
|
|---|
| 29 | import org.xml.sax.helpers.DefaultHandler;
|
|---|
| 30 |
|
|---|
| 31 | public class DiffResultProcessor {
|
|---|
| 32 |
|
|---|
| 33 | static private class DiffResultEntry {
|
|---|
| 34 | public long new_id;
|
|---|
| 35 | public int new_version;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * mapping from old id to new id and version, the result of parsing the diff result
|
|---|
| 40 | * replied by the server
|
|---|
| 41 | */
|
|---|
| 42 | private Map<PrimitiveId, DiffResultEntry> diffResults = new HashMap<PrimitiveId, DiffResultEntry>();
|
|---|
| 43 | /**
|
|---|
| 44 | * the set of processed primitives *after* the new id, the new version and the new changeset id
|
|---|
| 45 | * is set
|
|---|
| 46 | */
|
|---|
| 47 | private Set<OsmPrimitive> processed;
|
|---|
| 48 | /**
|
|---|
| 49 | * the collection of primitives being uploaded
|
|---|
| 50 | */
|
|---|
| 51 | private Collection<OsmPrimitive> primitives;
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Creates a diff result reader
|
|---|
| 55 | *
|
|---|
| 56 | * @param primitives the collection of primitives which have been uploaded. If null,
|
|---|
| 57 | * assumes an empty collection.
|
|---|
| 58 | */
|
|---|
| 59 | public DiffResultProcessor(Collection<OsmPrimitive> primitives) {
|
|---|
| 60 | if (primitives == null) {
|
|---|
| 61 | primitives = Collections.emptyList();
|
|---|
| 62 | }
|
|---|
| 63 | this.primitives = primitives;
|
|---|
| 64 | this.processed = new HashSet<OsmPrimitive>();
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Parse the response from a diff upload to the OSM API.
|
|---|
| 69 | *
|
|---|
| 70 | * @param diffUploadResponse the response. Must not be null.
|
|---|
| 71 | * @param progressMonitor a progress monitor. Defaults to {@see NullProgressMonitor#INSTANCE} if null
|
|---|
| 72 | * @throws IllegalArgumentException thrown if diffUploadRequest is null
|
|---|
| 73 | * @throws OsmDataParsingException thrown if the diffUploadRequest can't be parsed successfully
|
|---|
| 74 | *
|
|---|
| 75 | */
|
|---|
| 76 | public void parse(String diffUploadResponse, ProgressMonitor progressMonitor) throws OsmDataParsingException {
|
|---|
| 77 | if (progressMonitor == null) {
|
|---|
| 78 | progressMonitor = NullProgressMonitor.INSTANCE;
|
|---|
| 79 | }
|
|---|
| 80 | if (diffUploadResponse == null)
|
|---|
| 81 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "source"));
|
|---|
| 82 | try {
|
|---|
| 83 | progressMonitor.beginTask(tr("Parsing response from server..."));
|
|---|
| 84 | InputSource inputSource = new InputSource(new StringReader(diffUploadResponse));
|
|---|
| 85 | SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new Parser());
|
|---|
| 86 | } catch(IOException e) {
|
|---|
| 87 | throw new OsmDataParsingException(e);
|
|---|
| 88 | } catch(ParserConfigurationException e) {
|
|---|
| 89 | throw new OsmDataParsingException(e);
|
|---|
| 90 | } catch(OsmDataParsingException e) {
|
|---|
| 91 | throw e;
|
|---|
| 92 | } catch(SAXException e) {
|
|---|
| 93 | throw new OsmDataParsingException(e);
|
|---|
| 94 | } finally {
|
|---|
| 95 | progressMonitor.finishTask();
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * Postprocesses the diff result read and parsed from the server.
|
|---|
| 101 | *
|
|---|
| 102 | * Uploaded objects are assigned their new id (if they got assigned a new
|
|---|
| 103 | * id by the server), their new version (if the version was incremented),
|
|---|
| 104 | * and the id of the changeset to which they were uploaded.
|
|---|
| 105 | *
|
|---|
| 106 | * @param cs the current changeset. Ignored if null.
|
|---|
| 107 | * @param monitor the progress monitor. Set to {@see NullProgressMonitor#INSTANCE} if null
|
|---|
| 108 | * @return the collection of processed primitives
|
|---|
| 109 | */
|
|---|
| 110 | protected Set<OsmPrimitive> postProcess(Changeset cs,ProgressMonitor monitor) {
|
|---|
| 111 | if (monitor == null) {
|
|---|
| 112 | monitor = NullProgressMonitor.INSTANCE;
|
|---|
| 113 | }
|
|---|
| 114 | try {
|
|---|
| 115 | monitor.beginTask("Postprocessing uploaded data ...");
|
|---|
| 116 | monitor.setTicksCount(primitives.size());
|
|---|
| 117 | monitor.setTicks(0);
|
|---|
| 118 | for (OsmPrimitive p: primitives) {
|
|---|
| 119 | monitor.worked(1);
|
|---|
| 120 | DiffResultEntry entry = diffResults.get(p.getPrimitiveId());
|
|---|
| 121 | if (entry == null) {
|
|---|
| 122 | continue;
|
|---|
| 123 | }
|
|---|
| 124 | processed.add(p);
|
|---|
| 125 | if (!p.isDeleted()) {
|
|---|
| 126 | p.setOsmId(entry.new_id, entry.new_version);
|
|---|
| 127 | }
|
|---|
| 128 | if (cs != null && !cs.isNew()) {
|
|---|
| 129 | p.setChangesetId(cs.getId());
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | return processed;
|
|---|
| 133 | } finally {
|
|---|
| 134 | monitor.finishTask();
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | private class Parser extends DefaultHandler {
|
|---|
| 139 | private Locator locator;
|
|---|
| 140 |
|
|---|
| 141 | @Override
|
|---|
| 142 | public void setDocumentLocator(Locator locator) {
|
|---|
| 143 | this.locator = locator;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | protected void throwException(String msg) throws OsmDataParsingException{
|
|---|
| 147 | throw new OsmDataParsingException(msg).rememberLocation(locator);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
|---|
| 151 | try {
|
|---|
| 152 | if (qName.equals("diffResult")) {
|
|---|
| 153 | // the root element, ignore
|
|---|
| 154 | } else if (qName.equals("node") || qName.equals("way") || qName.equals("relation")) {
|
|---|
| 155 |
|
|---|
| 156 | PrimitiveId id = new SimplePrimitiveId(
|
|---|
| 157 | Long.parseLong(atts.getValue("old_id")),
|
|---|
| 158 | OsmPrimitiveType.fromApiTypeName(qName)
|
|---|
| 159 | );
|
|---|
| 160 | DiffResultEntry entry = new DiffResultEntry();
|
|---|
| 161 | if (atts.getValue("new_id") != null) {
|
|---|
| 162 | entry.new_id = Long.parseLong(atts.getValue("new_id"));
|
|---|
| 163 | }
|
|---|
| 164 | if (atts.getValue("new_version") != null) {
|
|---|
| 165 | entry.new_version = Integer.parseInt(atts.getValue("new_version"));
|
|---|
| 166 | }
|
|---|
| 167 | diffResults.put(id, entry);
|
|---|
| 168 | } else {
|
|---|
| 169 | throwException(tr("Unexpected XML element with name ''{0}''", qName));
|
|---|
| 170 | }
|
|---|
| 171 | } catch (NumberFormatException e) {
|
|---|
| 172 | throw new OsmDataParsingException(e).rememberLocation(locator);
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|