[1071] | 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;
|
---|
[1523] | 7 | import java.io.StringReader;
|
---|
[1071] | 8 | import java.util.Collection;
|
---|
[2604] | 9 | import java.util.Collections;
|
---|
[1071] | 10 | import java.util.HashMap;
|
---|
[2604] | 11 | import java.util.HashSet;
|
---|
[1071] | 12 | import java.util.Map;
|
---|
[2604] | 13 | import java.util.Set;
|
---|
[1071] | 14 |
|
---|
| 15 | import javax.xml.parsers.ParserConfigurationException;
|
---|
| 16 | import javax.xml.parsers.SAXParserFactory;
|
---|
| 17 |
|
---|
[2604] | 18 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
[1071] | 19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[2604] | 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;
|
---|
[1811] | 24 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[2852] | 25 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[1071] | 26 | import org.xml.sax.Attributes;
|
---|
| 27 | import org.xml.sax.InputSource;
|
---|
[2604] | 28 | import org.xml.sax.Locator;
|
---|
[1071] | 29 | import org.xml.sax.SAXException;
|
---|
| 30 | import org.xml.sax.helpers.DefaultHandler;
|
---|
| 31 |
|
---|
[2604] | 32 | public class DiffResultProcessor {
|
---|
[1169] | 33 |
|
---|
[2604] | 34 | static private class DiffResultEntry {
|
---|
| 35 | public long new_id;
|
---|
| 36 | public int new_version;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[1071] | 39 | /**
|
---|
[2604] | 40 | * mapping from old id to new id and version, the result of parsing the diff result
|
---|
| 41 | * replied by the server
|
---|
[1071] | 42 | */
|
---|
[2604] | 43 | private Map<PrimitiveId, DiffResultEntry> diffResults = new HashMap<PrimitiveId, DiffResultEntry>();
|
---|
| 44 | /**
|
---|
| 45 | * the set of processed primitives *after* the new id, the new version and the new changeset id
|
---|
| 46 | * is set
|
---|
| 47 | */
|
---|
| 48 | private Set<OsmPrimitive> processed;
|
---|
| 49 | /**
|
---|
| 50 | * the collection of primitives being uploaded
|
---|
| 51 | */
|
---|
| 52 | private Collection<OsmPrimitive> primitives;
|
---|
[1071] | 53 |
|
---|
[1169] | 54 | /**
|
---|
[2604] | 55 | * Creates a diff result reader
|
---|
[2711] | 56 | *
|
---|
[2604] | 57 | * @param primitives the collection of primitives which have been uploaded. If null,
|
---|
| 58 | * assumes an empty collection.
|
---|
[1071] | 59 | */
|
---|
[2604] | 60 | public DiffResultProcessor(Collection<OsmPrimitive> primitives) {
|
---|
| 61 | if (primitives == null) {
|
---|
| 62 | primitives = Collections.emptyList();
|
---|
[1071] | 63 | }
|
---|
[2604] | 64 | this.primitives = primitives;
|
---|
| 65 | this.processed = new HashSet<OsmPrimitive>();
|
---|
[1071] | 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
[2604] | 69 | * Parse the response from a diff upload to the OSM API.
|
---|
[2711] | 70 | *
|
---|
[2604] | 71 | * @param diffUploadResponse the response. Must not be null.
|
---|
| 72 | * @param progressMonitor a progress monitor. Defaults to {@see NullProgressMonitor#INSTANCE} if null
|
---|
| 73 | * @throws IllegalArgumentException thrown if diffUploadRequest is null
|
---|
| 74 | * @throws OsmDataParsingException thrown if the diffUploadRequest can't be parsed successfully
|
---|
[2711] | 75 | *
|
---|
[1071] | 76 | */
|
---|
[2604] | 77 | public void parse(String diffUploadResponse, ProgressMonitor progressMonitor) throws OsmDataParsingException {
|
---|
| 78 | if (progressMonitor == null) {
|
---|
| 79 | progressMonitor = NullProgressMonitor.INSTANCE;
|
---|
| 80 | }
|
---|
[2852] | 81 | CheckParameterUtil.ensureParameterNotNull(diffUploadResponse, "diffUploadResponse");
|
---|
[1811] | 82 | try {
|
---|
[2604] | 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);
|
---|
[1811] | 94 | } finally {
|
---|
| 95 | progressMonitor.finishTask();
|
---|
| 96 | }
|
---|
[1071] | 97 | }
|
---|
[1169] | 98 |
|
---|
[2604] | 99 | /**
|
---|
| 100 | * Postprocesses the diff result read and parsed from the server.
|
---|
[2711] | 101 | *
|
---|
[2604] | 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.
|
---|
[2711] | 105 | *
|
---|
[2604] | 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);
|
---|
[3336] | 127 | p.setVisible(true);
|
---|
[3422] | 128 | } else {
|
---|
| 129 | p.setVisible(false);
|
---|
[2604] | 130 | }
|
---|
| 131 | if (cs != null && !cs.isNew()) {
|
---|
| 132 | p.setChangesetId(cs.getId());
|
---|
| 133 | }
|
---|
[1071] | 134 | }
|
---|
[2604] | 135 | return processed;
|
---|
| 136 | } finally {
|
---|
| 137 | monitor.finishTask();
|
---|
[1071] | 138 | }
|
---|
| 139 | }
|
---|
[2604] | 140 |
|
---|
| 141 | private class Parser extends DefaultHandler {
|
---|
| 142 | private Locator locator;
|
---|
| 143 |
|
---|
| 144 | @Override
|
---|
| 145 | public void setDocumentLocator(Locator locator) {
|
---|
| 146 | this.locator = locator;
|
---|
[1169] | 147 | }
|
---|
[2604] | 148 |
|
---|
| 149 | protected void throwException(String msg) throws OsmDataParsingException{
|
---|
| 150 | throw new OsmDataParsingException(msg).rememberLocation(locator);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
---|
| 154 | try {
|
---|
| 155 | if (qName.equals("diffResult")) {
|
---|
| 156 | // the root element, ignore
|
---|
| 157 | } else if (qName.equals("node") || qName.equals("way") || qName.equals("relation")) {
|
---|
| 158 |
|
---|
| 159 | PrimitiveId id = new SimplePrimitiveId(
|
---|
| 160 | Long.parseLong(atts.getValue("old_id")),
|
---|
| 161 | OsmPrimitiveType.fromApiTypeName(qName)
|
---|
| 162 | );
|
---|
| 163 | DiffResultEntry entry = new DiffResultEntry();
|
---|
| 164 | if (atts.getValue("new_id") != null) {
|
---|
| 165 | entry.new_id = Long.parseLong(atts.getValue("new_id"));
|
---|
| 166 | }
|
---|
| 167 | if (atts.getValue("new_version") != null) {
|
---|
| 168 | entry.new_version = Integer.parseInt(atts.getValue("new_version"));
|
---|
| 169 | }
|
---|
| 170 | diffResults.put(id, entry);
|
---|
| 171 | } else {
|
---|
| 172 | throwException(tr("Unexpected XML element with name ''{0}''", qName));
|
---|
| 173 | }
|
---|
| 174 | } catch (NumberFormatException e) {
|
---|
| 175 | throw new OsmDataParsingException(e).rememberLocation(locator);
|
---|
[1071] | 176 | }
|
---|
[1169] | 177 | }
|
---|
[1071] | 178 | }
|
---|
| 179 | }
|
---|