1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.hot.sds;
|
---|
3 |
|
---|
4 | import java.io.ByteArrayInputStream;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.io.UnsupportedEncodingException;
|
---|
8 | import java.util.ArrayList;
|
---|
9 |
|
---|
10 | import javax.xml.parsers.ParserConfigurationException;
|
---|
11 | import javax.xml.parsers.SAXParserFactory;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
14 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
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.Visitor;
|
---|
20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
21 | import org.openstreetmap.josm.io.OsmServerReadPostprocessor;
|
---|
22 | import org.xml.sax.InputSource;
|
---|
23 | import org.xml.sax.SAXException;
|
---|
24 |
|
---|
25 | public class ReadPostprocessor implements OsmServerReadPostprocessor {
|
---|
26 |
|
---|
27 | private ArrayList<Long> nodeList;
|
---|
28 | private ArrayList<Long> wayList;
|
---|
29 | private ArrayList<Long> relationList;
|
---|
30 |
|
---|
31 | private SeparateDataStorePlugin plugin;
|
---|
32 |
|
---|
33 | public ReadPostprocessor(SeparateDataStorePlugin plugin) {
|
---|
34 | this.plugin = plugin;
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public void postprocessDataSet(DataSet ds, ProgressMonitor progress) {
|
---|
39 |
|
---|
40 | nodeList = new ArrayList<Long>();
|
---|
41 | wayList = new ArrayList<Long>();
|
---|
42 | relationList = new ArrayList<Long>();
|
---|
43 |
|
---|
44 | Visitor adder = new Visitor() {
|
---|
45 | @Override
|
---|
46 | public void visit(Node n) {
|
---|
47 | nodeList.add(n.getId());
|
---|
48 | plugin.originalNodes.put(n.getId(), n.save());
|
---|
49 | }
|
---|
50 | @Override
|
---|
51 | public void visit(Way w) {
|
---|
52 | wayList.add(w.getId());
|
---|
53 | plugin.originalWays.put(w.getId(), w.save());
|
---|
54 | }
|
---|
55 | @Override
|
---|
56 | public void visit(Relation e) {
|
---|
57 | relationList.add(e.getId());
|
---|
58 | plugin.originalNodes.put(e.getId(), e.save());
|
---|
59 | }
|
---|
60 | @Override
|
---|
61 | public void visit(Changeset cs) {}
|
---|
62 | };
|
---|
63 |
|
---|
64 | for (OsmPrimitive p : ds.allPrimitives()) {
|
---|
65 | p.accept(adder);
|
---|
66 | }
|
---|
67 |
|
---|
68 | SdsApi api = SdsApi.getSdsApi();
|
---|
69 | String rv = "";
|
---|
70 | try {
|
---|
71 | rv = api.requestShadowsFromSds(nodeList, wayList, relationList, progress);
|
---|
72 | } catch (SdsTransferException e) {
|
---|
73 | // TODO Auto-generated catch block
|
---|
74 | e.printStackTrace();
|
---|
75 | }
|
---|
76 |
|
---|
77 | // this is slightly inefficient, as we're re-making the string into
|
---|
78 | // an input stream when there was an input stream to be had inside the
|
---|
79 | // SdsApi already, but this encapsulates things better.
|
---|
80 | InputStream xmlStream;
|
---|
81 | try {
|
---|
82 | xmlStream = new ByteArrayInputStream(rv.getBytes("UTF-8"));
|
---|
83 | InputSource inputSource = new InputSource(xmlStream);
|
---|
84 | SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new SdsParser(ds, plugin));
|
---|
85 | } catch (UnsupportedEncodingException e1) {
|
---|
86 | // TODO Auto-generated catch block
|
---|
87 | e1.printStackTrace();
|
---|
88 | } catch (SAXException e) {
|
---|
89 | // TODO Auto-generated catch block
|
---|
90 | e.printStackTrace();
|
---|
91 | } catch (IOException e) {
|
---|
92 | // TODO Auto-generated catch block
|
---|
93 | e.printStackTrace();
|
---|
94 | } catch (ParserConfigurationException e) {
|
---|
95 | // TODO Auto-generated catch block
|
---|
96 | e.printStackTrace();
|
---|
97 | }
|
---|
98 |
|
---|
99 | }
|
---|
100 |
|
---|
101 | }
|
---|