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

Last change on this file since 1102 was 1071, checked in by framm, 15 years ago

Support for changeset uploads in 0.6. Error handling is not fully done but basics are working. Full changeset upload is now the default if version is 0.6; set osm-server.atomic-upload=false in your configuration file to go back to individual object upload.

File size: 5.2 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.InputStream;
8import java.io.InputStreamReader;
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.HashMap;
13import java.util.HashSet;
14import java.util.LinkedList;
15import java.util.Map;
16import java.util.Map.Entry;
17
18import javax.xml.parsers.ParserConfigurationException;
19import javax.xml.parsers.SAXParserFactory;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.data.Bounds;
23import org.openstreetmap.josm.data.coor.LatLon;
24import org.openstreetmap.josm.data.osm.DataSet;
25import org.openstreetmap.josm.data.osm.DataSource;
26import org.openstreetmap.josm.data.osm.Node;
27import org.openstreetmap.josm.data.osm.OsmPrimitive;
28import org.openstreetmap.josm.data.osm.Relation;
29import org.openstreetmap.josm.data.osm.RelationMember;
30import org.openstreetmap.josm.data.osm.User;
31import org.openstreetmap.josm.data.osm.Way;
32import org.openstreetmap.josm.data.osm.visitor.AddVisitor;
33import org.openstreetmap.josm.data.osm.visitor.Visitor;
34import org.openstreetmap.josm.gui.PleaseWaitDialog;
35import org.xml.sax.Attributes;
36import org.xml.sax.InputSource;
37import org.xml.sax.SAXException;
38import org.xml.sax.helpers.DefaultHandler;
39
40/**
41 */
42public 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}
Note: See TracBrowser for help on using the repository browser.