source: josm/trunk/src/org/openstreetmap/josm/io/DiffResultProcessor.java@ 9171

Last change on this file since 9171 was 9078, checked in by Don-vip, 9 years ago

sonar - Immutable Field

  • Property svn:eol-style set to native
File size: 7.0 KB
RevLine 
[8378]1// License: GPL. For details, see LICENSE file.
[1071]2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
[1523]7import java.io.StringReader;
[1071]8import java.util.Collection;
[2604]9import java.util.Collections;
[1071]10import java.util.HashMap;
[2604]11import java.util.HashSet;
[1071]12import java.util.Map;
[2604]13import java.util.Set;
[1071]14
15import javax.xml.parsers.ParserConfigurationException;
16
[2604]17import org.openstreetmap.josm.data.osm.Changeset;
[7656]18import org.openstreetmap.josm.data.osm.DataSet;
19import org.openstreetmap.josm.data.osm.OsmPrimitive;
[2604]20import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
21import org.openstreetmap.josm.data.osm.PrimitiveId;
22import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
23import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
[1811]24import org.openstreetmap.josm.gui.progress.ProgressMonitor;
[2852]25import org.openstreetmap.josm.tools.CheckParameterUtil;
[8287]26import org.openstreetmap.josm.tools.Utils;
[6906]27import org.openstreetmap.josm.tools.XmlParsingException;
[1071]28import org.xml.sax.Attributes;
29import org.xml.sax.InputSource;
[2604]30import org.xml.sax.Locator;
[1071]31import org.xml.sax.SAXException;
32import org.xml.sax.helpers.DefaultHandler;
33
[2604]34public class DiffResultProcessor {
[1169]35
[6889]36 private static class DiffResultEntry {
[8346]37 private long newId;
38 private int newVersion;
[2604]39 }
40
[1071]41 /**
[2604]42 * mapping from old id to new id and version, the result of parsing the diff result
43 * replied by the server
[1071]44 */
[9078]45 private final Map<PrimitiveId, DiffResultEntry> diffResults = new HashMap<>();
[2604]46 /**
[7656]47 * the set of processed primitives *after* the new id, the new version and the new changeset id is set
[2604]48 */
[9078]49 private final Set<OsmPrimitive> processed;
[2604]50 /**
51 * the collection of primitives being uploaded
52 */
[9078]53 private final Collection<? extends OsmPrimitive> primitives;
[1071]54
[1169]55 /**
[2604]56 * Creates a diff result reader
[2711]57 *
[2604]58 * @param primitives the collection of primitives which have been uploaded. If null,
59 * assumes an empty collection.
[1071]60 */
[7656]61 public DiffResultProcessor(Collection<? extends OsmPrimitive> primitives) {
[2604]62 if (primitives == null) {
63 primitives = Collections.emptyList();
[1071]64 }
[2604]65 this.primitives = primitives;
[7005]66 this.processed = new HashSet<>();
[1071]67 }
68
69 /**
[2604]70 * Parse the response from a diff upload to the OSM API.
[2711]71 *
[2604]72 * @param diffUploadResponse the response. Must not be null.
[5266]73 * @param progressMonitor a progress monitor. Defaults to {@link NullProgressMonitor#INSTANCE} if null
[6906]74 * @throws IllegalArgumentException if diffUploadRequest is null
75 * @throws XmlParsingException if the diffUploadRequest can't be parsed successfully
[2711]76 *
[1071]77 */
[6906]78 public void parse(String diffUploadResponse, ProgressMonitor progressMonitor) throws XmlParsingException {
[2604]79 if (progressMonitor == null) {
80 progressMonitor = NullProgressMonitor.INSTANCE;
81 }
[2852]82 CheckParameterUtil.ensureParameterNotNull(diffUploadResponse, "diffUploadResponse");
[1811]83 try {
[2604]84 progressMonitor.beginTask(tr("Parsing response from server..."));
85 InputSource inputSource = new InputSource(new StringReader(diffUploadResponse));
[8347]86 Utils.parseSafeSAX(inputSource, new Parser());
[8510]87 } catch (XmlParsingException e) {
[2604]88 throw e;
[8510]89 } catch (IOException | ParserConfigurationException | SAXException e) {
[6906]90 throw new XmlParsingException(e);
[1811]91 } finally {
92 progressMonitor.finishTask();
93 }
[1071]94 }
[1169]95
[2604]96 /**
97 * Postprocesses the diff result read and parsed from the server.
[2711]98 *
[2604]99 * Uploaded objects are assigned their new id (if they got assigned a new
100 * id by the server), their new version (if the version was incremented),
101 * and the id of the changeset to which they were uploaded.
[2711]102 *
[2604]103 * @param cs the current changeset. Ignored if null.
[5266]104 * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
[2604]105 * @return the collection of processed primitives
106 */
[7656]107 protected Set<OsmPrimitive> postProcess(Changeset cs, ProgressMonitor monitor) {
[2604]108 if (monitor == null) {
109 monitor = NullProgressMonitor.INSTANCE;
110 }
[7658]111 DataSet ds = null;
112 if (!primitives.isEmpty()) {
113 ds = primitives.iterator().next().getDataSet();
114 }
115 if (ds != null) {
116 ds.beginUpdate();
117 }
[2604]118 try {
119 monitor.beginTask("Postprocessing uploaded data ...");
120 monitor.setTicksCount(primitives.size());
121 monitor.setTicks(0);
[7656]122 for (OsmPrimitive p : primitives) {
[2604]123 monitor.worked(1);
124 DiffResultEntry entry = diffResults.get(p.getPrimitiveId());
125 if (entry == null) {
126 continue;
127 }
128 processed.add(p);
129 if (!p.isDeleted()) {
[8346]130 p.setOsmId(entry.newId, entry.newVersion);
[3336]131 p.setVisible(true);
[3422]132 } else {
133 p.setVisible(false);
[2604]134 }
135 if (cs != null && !cs.isNew()) {
136 p.setChangesetId(cs.getId());
137 }
[1071]138 }
[2604]139 return processed;
140 } finally {
[7658]141 if (ds != null) {
142 ds.endUpdate();
143 }
[2604]144 monitor.finishTask();
[1071]145 }
146 }
[2604]147
148 private class Parser extends DefaultHandler {
149 private Locator locator;
150
151 @Override
152 public void setDocumentLocator(Locator locator) {
153 this.locator = locator;
[1169]154 }
[2604]155
[6906]156 protected void throwException(String msg) throws XmlParsingException {
157 throw new XmlParsingException(msg).rememberLocation(locator);
[2604]158 }
159
[7012]160 @Override
161 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
[2604]162 try {
[7012]163 switch (qName) {
164 case "diffResult":
[2604]165 // the root element, ignore
[7012]166 break;
167 case "node":
168 case "way":
169 case "relation":
[2604]170 PrimitiveId id = new SimplePrimitiveId(
171 Long.parseLong(atts.getValue("old_id")),
172 OsmPrimitiveType.fromApiTypeName(qName)
173 );
174 DiffResultEntry entry = new DiffResultEntry();
175 if (atts.getValue("new_id") != null) {
[8346]176 entry.newId = Long.parseLong(atts.getValue("new_id"));
[2604]177 }
178 if (atts.getValue("new_version") != null) {
[8346]179 entry.newVersion = Integer.parseInt(atts.getValue("new_version"));
[2604]180 }
181 diffResults.put(id, entry);
[7012]182 break;
183 default:
[2604]184 throwException(tr("Unexpected XML element with name ''{0}''", qName));
185 }
186 } catch (NumberFormatException e) {
[6906]187 throw new XmlParsingException(e).rememberLocation(locator);
[1071]188 }
[1169]189 }
[1071]190 }
191}
Note: See TracBrowser for help on using the repository browser.