1 | package org.openstreetmap.josm.io;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.util.Collection;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
|
---|
13 | import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
|
---|
14 | import org.xml.sax.SAXException;
|
---|
15 |
|
---|
16 | public class ObjectListDownloader extends OsmServerReader {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * All the objects to download (if not downloading bounding boxes instead)
|
---|
20 | */
|
---|
21 | private final Collection<OsmPrimitive> toDownload;
|
---|
22 | private final DataSet ds = new DataSet();
|
---|
23 | private final MergeVisitor merger = new MergeVisitor(ds);
|
---|
24 |
|
---|
25 | public ObjectListDownloader(Collection<OsmPrimitive> toDownload) {
|
---|
26 | this.toDownload = toDownload;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public DataSet parse() throws SAXException, IOException {
|
---|
30 | Main.pleaseWaitDlg.progress.setMaximum(toDownload.size());
|
---|
31 | Main.pleaseWaitDlg.progress.setValue(0);
|
---|
32 | try {
|
---|
33 | final NameVisitor namer = new NameVisitor();
|
---|
34 | for (OsmPrimitive osm : toDownload) {
|
---|
35 | osm.visit(namer);
|
---|
36 | download(tr(namer.className), osm.id);
|
---|
37 | if (cancel)
|
---|
38 | break;
|
---|
39 | }
|
---|
40 | if (!merger.conflicts.isEmpty())
|
---|
41 | throw new RuntimeException(tr("Conflicts in disjunct objects"));
|
---|
42 | return ds;
|
---|
43 | } catch (IOException e) {
|
---|
44 | if (cancel)
|
---|
45 | return null;
|
---|
46 | throw e;
|
---|
47 | } catch (SAXException e) {
|
---|
48 | throw e;
|
---|
49 | } catch (Exception e) {
|
---|
50 | if (cancel)
|
---|
51 | return null;
|
---|
52 | if (e instanceof RuntimeException)
|
---|
53 | throw (RuntimeException)e;
|
---|
54 | throw new RuntimeException(e);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | private void download(String className, long id) throws IOException, SAXException {
|
---|
59 | Main.pleaseWaitDlg.currentAction.setText(tr("Downloading {0} {1}", className, id));
|
---|
60 | InputStream in = getInputStream(className+"/"+id);
|
---|
61 | if (in == null)
|
---|
62 | return;
|
---|
63 | DataSet data = OsmReader.parseDataSet(in, null, null);
|
---|
64 | Main.pleaseWaitDlg.progress.setValue(Main.pleaseWaitDlg.progress.getValue()+1);
|
---|
65 | if (data.allPrimitives().size() > 1)
|
---|
66 | throw new SAXException(tr("Got more than one object when expecting only one."));
|
---|
67 | for (OsmPrimitive osm : data.allPrimitives())
|
---|
68 | osm.visit(merger);
|
---|
69 | }
|
---|
70 | }
|
---|