source: josm/trunk/src/org/openstreetmap/josm/io/DiffResultReader.java@ 2474

Last change on this file since 2474 was 2070, checked in by Gubaer, 15 years ago

new: rewrite of CombineWay action
new: conflict resolution dialog for CombineWay, including conflicts for different relation memberships
cleanup: cleanup in OsmReader, reduces memory footprint and reduces parsing time
cleanup: made most of the public fields in OsmPrimitive @deprecated, added accessors and changed the code
cleanup: replaced usages of @deprecated constructors for ExtendedDialog
fixed #3208: Combine ways brokes relation order

WARNING: this changeset touches a lot of code all over the code base. "latest" might become slightly unstable in the next days. Also experience incompatibility issues with plugins in the next few days.

File size: 4.5 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.IOException;
7import java.io.StringReader;
8import java.util.Collection;
9import java.util.HashMap;
10import java.util.Map;
11
12import javax.xml.parsers.ParserConfigurationException;
13import javax.xml.parsers.SAXParserFactory;
14
15import org.openstreetmap.josm.data.osm.Node;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.Relation;
18import org.openstreetmap.josm.data.osm.Way;
19import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
20import org.openstreetmap.josm.gui.progress.ProgressMonitor;
21import org.xml.sax.Attributes;
22import org.xml.sax.InputSource;
23import org.xml.sax.SAXException;
24import org.xml.sax.helpers.DefaultHandler;
25
26/**
27 */
28public class DiffResultReader extends AbstractVisitor {
29
30 /**
31 * mapping from old id to new id/version
32 */
33 private Map<String, Long[]> versions = new HashMap<String, Long[]>();
34 private Collection<OsmPrimitive> processed;
35 private Map<OsmPrimitive,Long> newIdMap;
36
37 /**
38 * List of protocol versions that will be accepted on reading
39 */
40
41 private class Parser extends DefaultHandler {
42
43 @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
44 try {
45 if (qName.equals("osm")) {
46 } else if (qName.equals("node") || qName.equals("way") || qName.equals("relation")) {
47 String key = qName + ":" + atts.getValue("old_id");
48 String newid = atts.getValue("new_id");
49 String newver = atts.getValue("new_version");
50 Long[] value = new Long[] { newid == null ? null : new Long(newid), newver == null ? null : new Long(newver) };
51 versions.put(key, value);
52 }
53 } catch (NumberFormatException x) {
54 x.printStackTrace(); // SAXException does not chain correctly
55 throw new SAXException(x.getMessage(), x);
56 } catch (NullPointerException x) {
57 x.printStackTrace(); // SAXException does not chain correctly
58 throw new SAXException(tr("NullPointerException, possibly some missing tags."), x);
59 }
60 }
61 }
62
63 /**
64 * Parse the given input source and return the dataset.
65 */
66 public static void parseDiffResult(String source, Collection<OsmPrimitive> osm, Collection<OsmPrimitive> processed, Map<OsmPrimitive,Long> newIdMap, ProgressMonitor progressMonitor)
67 throws SAXException, IOException {
68
69 progressMonitor.beginTask(tr("Preparing data..."));
70 try {
71
72 DiffResultReader drr = new DiffResultReader();
73 drr.processed = processed;
74 drr.newIdMap = newIdMap;
75 InputSource inputSource = new InputSource(new StringReader(source));
76 try {
77 SAXParserFactory.newInstance().newSAXParser().parse(inputSource, drr.new Parser());
78 } catch (ParserConfigurationException e1) {
79 e1.printStackTrace(); // broken SAXException chaining
80 throw new SAXException(e1);
81 }
82
83 for (OsmPrimitive p : osm) {
84 //System.out.println("old: "+ p);
85 p.visit(drr);
86 //System.out.println("new: "+ p);
87 //System.out.println("");
88 }
89 } finally {
90 progressMonitor.finishTask();
91 }
92 }
93
94 public void visit(Node n) {
95 String key = "node:" + (newIdMap.containsKey(n) ? newIdMap.get(n) : n.getId());
96 Long[] nv = versions.get(key);
97 if (nv != null) {
98 processed.add(n);
99 if (!n.isDeleted()) {
100 n.setOsmId(nv[0], nv[1].intValue());
101 }
102 }
103 }
104 public void visit(Way w) {
105 String key = "way:" + (newIdMap.containsKey(w) ? newIdMap.get(w) : w.getId());
106 Long[] nv = versions.get(key);
107 if (nv != null) {
108 processed.add(w);
109 if (!w.isDeleted()) {
110 w.setOsmId(nv[0], nv[1].intValue());
111 }
112 }
113 }
114 public void visit(Relation r) {
115 String key = "relation:" + (newIdMap.containsKey(r) ? newIdMap.get(r) : r.getId());
116 Long[] nv = versions.get(key);
117 if (nv != null) {
118 processed.add(r);
119 if (!r.isDeleted()) {
120 r.setOsmId(nv[0], nv[1].intValue());
121 }
122 }
123 }
124}
Note: See TracBrowser for help on using the repository browser.