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

Last change on this file since 1811 was 1811, checked in by jttt, 15 years ago

PleaseWait refactoring. Progress is now reported using ProgressMonitor interface, that is available through PleaseWaitRunnable.

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.id);
96 System.out.println("key: "+key);
97 Long[] nv = versions.get(key);
98 if (nv != null) {
99 processed.add(n);
100 if (!n.deleted) {
101 n.id = nv[0]; n.version = nv[1].intValue();
102 }
103 }
104 }
105 public void visit(Way w) {
106 String key = "way:" + (newIdMap.containsKey(w) ? newIdMap.get(w) : w.id);
107 Long[] nv = versions.get(key);
108 if (nv != null) {
109 processed.add(w);
110 if (!w.deleted) {
111 w.id = nv[0]; w.version = nv[1].intValue();
112 }
113 }
114 }
115 public void visit(Relation r) {
116 String key = "relation:" + (newIdMap.containsKey(r) ? newIdMap.get(r) : r.id);
117 Long[] nv = versions.get(key);
118 if (nv != null) {
119 processed.add(r);
120 if (!r.deleted) {
121 r.id = nv[0]; r.version = nv[1].intValue();
122 }
123 }
124 }
125}
Note: See TracBrowser for help on using the repository browser.