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.HashMap;
|
---|
10 | import java.util.Map;
|
---|
11 |
|
---|
12 | import javax.xml.parsers.ParserConfigurationException;
|
---|
13 | import javax.xml.parsers.SAXParserFactory;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
18 | import org.openstreetmap.josm.data.osm.Way;
|
---|
19 | import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
|
---|
20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
21 | import org.xml.sax.Attributes;
|
---|
22 | import org.xml.sax.InputSource;
|
---|
23 | import org.xml.sax.SAXException;
|
---|
24 | import org.xml.sax.helpers.DefaultHandler;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | */
|
---|
28 | public 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 | }
|
---|