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.InputStream;
|
---|
8 | import java.io.InputStreamReader;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.HashSet;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.Map;
|
---|
16 | import java.util.Map.Entry;
|
---|
17 |
|
---|
18 | import javax.xml.parsers.ParserConfigurationException;
|
---|
19 | import javax.xml.parsers.SAXParserFactory;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.data.Bounds;
|
---|
23 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
24 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
25 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
26 | import org.openstreetmap.josm.data.osm.Node;
|
---|
27 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
28 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
29 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
30 | import org.openstreetmap.josm.data.osm.User;
|
---|
31 | import org.openstreetmap.josm.data.osm.Way;
|
---|
32 | import org.openstreetmap.josm.data.osm.visitor.AddVisitor;
|
---|
33 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
34 | import org.openstreetmap.josm.gui.PleaseWaitDialog;
|
---|
35 | import org.xml.sax.Attributes;
|
---|
36 | import org.xml.sax.InputSource;
|
---|
37 | import org.xml.sax.SAXException;
|
---|
38 | import org.xml.sax.helpers.DefaultHandler;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | */
|
---|
42 | public class DiffResultReader implements Visitor {
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * mapping from old id to new id/version
|
---|
46 | */
|
---|
47 | private Map<String, Long[]> versions = new HashMap<String, Long[]>();
|
---|
48 | private Collection<OsmPrimitive> processed;
|
---|
49 | private Map<OsmPrimitive,Long> newIdMap;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * List of protocol versions that will be accepted on reading
|
---|
53 | */
|
---|
54 |
|
---|
55 | private class Parser extends DefaultHandler {
|
---|
56 |
|
---|
57 | @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
|
---|
58 | try {
|
---|
59 | if (qName.equals("osm")) {
|
---|
60 | } else if (qName.equals("node") || qName.equals("way") || qName.equals("relation")) {
|
---|
61 | String key = qName + ":" + atts.getValue("old_id");
|
---|
62 | String newid = atts.getValue("new_id");
|
---|
63 | String newver = atts.getValue("new_version");
|
---|
64 | Long[] value = new Long[] { newid == null ? null : new Long(newid), newver == null ? null : new Long(newver) };
|
---|
65 | versions.put(key, value);
|
---|
66 | }
|
---|
67 | } catch (NumberFormatException x) {
|
---|
68 | x.printStackTrace(); // SAXException does not chain correctly
|
---|
69 | throw new SAXException(x.getMessage(), x);
|
---|
70 | } catch (NullPointerException x) {
|
---|
71 | x.printStackTrace(); // SAXException does not chain correctly
|
---|
72 | throw new SAXException(tr("NullPointerException, Possibly some missing tags."), x);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Parse the given input source and return the dataset.
|
---|
79 | * @param ref The dataset that is search in for references first. If
|
---|
80 | * the Reference is not found here, Main.ds is searched and a copy of the
|
---|
81 | * elemet found there is returned.
|
---|
82 | */
|
---|
83 | public static void parseDiffResult(InputStream source, Collection<OsmPrimitive> osm, Collection<OsmPrimitive> processed, Map<OsmPrimitive,Long> newIdMap, PleaseWaitDialog pleaseWaitDlg)
|
---|
84 | throws SAXException, IOException {
|
---|
85 |
|
---|
86 | DiffResultReader drr = new DiffResultReader();
|
---|
87 | drr.processed = processed;
|
---|
88 | drr.newIdMap = newIdMap;
|
---|
89 | InputSource inputSource = new InputSource(new InputStreamReader(source, "UTF-8"));
|
---|
90 | try {
|
---|
91 | SAXParserFactory.newInstance().newSAXParser().parse(inputSource, drr.new Parser());
|
---|
92 | } catch (ParserConfigurationException e1) {
|
---|
93 | e1.printStackTrace(); // broken SAXException chaining
|
---|
94 | throw new SAXException(e1);
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (pleaseWaitDlg != null) {
|
---|
98 | pleaseWaitDlg.progress.setValue(0);
|
---|
99 | pleaseWaitDlg.currentAction.setText(tr("Preparing data..."));
|
---|
100 | }
|
---|
101 |
|
---|
102 | for (OsmPrimitive p : osm) {
|
---|
103 | System.out.println("old: "+ p);
|
---|
104 | p.visit(drr);
|
---|
105 | System.out.println("new: "+ p);
|
---|
106 | System.out.println("");
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | public void visit(Node n) {
|
---|
111 | String key = "node:" + (newIdMap.containsKey(n) ? newIdMap.get(n) : n.id);
|
---|
112 | System.out.println("key: "+key);
|
---|
113 | Long[] nv = versions.get(key);
|
---|
114 | if (nv != null) {
|
---|
115 | processed.add(n);
|
---|
116 | if (!n.deleted) {
|
---|
117 | n.id = nv[0]; n.version = nv[1].intValue();
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 | public void visit(Way w) {
|
---|
122 | String key = "way:" + (newIdMap.containsKey(w) ? newIdMap.get(w) : w.id);
|
---|
123 | Long[] nv = versions.get(key);
|
---|
124 | if (nv != null) {
|
---|
125 | processed.add(w);
|
---|
126 | if (!w.deleted) {
|
---|
127 | w.id = nv[0]; w.version = nv[1].intValue();
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 | public void visit(Relation r) {
|
---|
132 | String key = "relation:" + (newIdMap.containsKey(r) ? newIdMap.get(r) : r.id);
|
---|
133 | Long[] nv = versions.get(key);
|
---|
134 | if (nv != null) {
|
---|
135 | processed.add(r);
|
---|
136 | if (!r.deleted) {
|
---|
137 | r.id = nv[0]; r.version = nv[1].intValue();
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|