1 | //License: GPL. For details, see LICENSE file.
|
---|
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 |
|
---|
9 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
11 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
12 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
13 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
14 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
15 | import org.xml.sax.SAXException;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * OsmServerObjectReader reads an individual object from the OSM server.
|
---|
19 | *
|
---|
20 | * It can either download the object including or not including its immediate children.
|
---|
21 | * The former case is called a "full download".
|
---|
22 | *
|
---|
23 | */
|
---|
24 | public class OsmServerObjectReader extends OsmServerReader {
|
---|
25 | /** the id of the object to download */
|
---|
26 | private PrimitiveId id;
|
---|
27 | /** true if a full download is required, i.e. a download including the immediate children */
|
---|
28 | private boolean full;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Creates a new server object reader for a given id and a primitive type.
|
---|
32 | *
|
---|
33 | * @param id the object id. > 0 required.
|
---|
34 | * @param type the type. Must not be null.
|
---|
35 | * @param full true, if a full download is requested (i.e. a download including
|
---|
36 | * immediate children); false, otherwise
|
---|
37 | * @throws IllegalArgumentException thrown if id <= 0
|
---|
38 | * @throws IllegalArgumentException thrown if type is null
|
---|
39 | */
|
---|
40 | public OsmServerObjectReader(long id, OsmPrimitiveType type, boolean full) throws IllegalArgumentException {
|
---|
41 | if (id <= 0)
|
---|
42 | throw new IllegalArgumentException(tr("Expected value > 0 for parameter ''{0}'', got {1}", "id", id));
|
---|
43 | if (type == null)
|
---|
44 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "type"));
|
---|
45 | this.id = new SimplePrimitiveId(id, type);
|
---|
46 | this.full = full;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Creates a new server object reader for an object with the given <code>id</code>
|
---|
51 | *
|
---|
52 | * @param id the object id. Must not be null. Unique id > 0 required.
|
---|
53 | * @param full true, if a full download is requested (i.e. a download including
|
---|
54 | * immediate children); false, otherwise
|
---|
55 | * @throws IllegalArgumentException thrown if id is null
|
---|
56 | * @throws IllegalArgumentException thrown if id.getUniqueId() <= 0
|
---|
57 | */
|
---|
58 | public OsmServerObjectReader(PrimitiveId id, boolean full) {
|
---|
59 | if (id == null)
|
---|
60 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "id"));
|
---|
61 | if (id.getUniqueId() <= 0)
|
---|
62 | throw new IllegalArgumentException(tr("Expected value > 0 for parameter ''{0}'', got {1}", "id.getUniqueId()", id.getUniqueId()));
|
---|
63 | this.id = id;
|
---|
64 | this.full = full;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Downloads and parses the data.
|
---|
69 | *
|
---|
70 | * @param progressMonitor the progress monitor. Set to {@see NullProgressMonitor#INSTANCE} if
|
---|
71 | * null
|
---|
72 | * @return the downloaded data
|
---|
73 | * @throws SAXException
|
---|
74 | * @throws IOException
|
---|
75 | */
|
---|
76 | @Override
|
---|
77 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
78 | if (progressMonitor == null) {
|
---|
79 | progressMonitor = NullProgressMonitor.INSTANCE;
|
---|
80 | }
|
---|
81 | progressMonitor.beginTask("", 1);
|
---|
82 | InputStream in = null;
|
---|
83 | try {
|
---|
84 | progressMonitor.indeterminateSubTask(tr("Downloading OSM data..."));
|
---|
85 | StringBuffer sb = new StringBuffer();
|
---|
86 | sb.append(id.getType().getAPIName());
|
---|
87 | sb.append("/");
|
---|
88 | sb.append(id.getUniqueId());
|
---|
89 | if (full && ! id.getType().equals(OsmPrimitiveType.NODE)) {
|
---|
90 | sb.append("/full");
|
---|
91 | }
|
---|
92 |
|
---|
93 | in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true));
|
---|
94 | if (in == null)
|
---|
95 | return null;
|
---|
96 | final DataSet data = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
|
---|
97 | return data;
|
---|
98 | } catch(OsmTransferException e) {
|
---|
99 | if (cancel) return null;
|
---|
100 | throw e;
|
---|
101 | } catch (Exception e) {
|
---|
102 | if (cancel) return null;
|
---|
103 | throw new OsmTransferException(e);
|
---|
104 | } finally {
|
---|
105 | progressMonitor.finishTask();
|
---|
106 | if (in!=null) {
|
---|
107 | try {
|
---|
108 | in.close();
|
---|
109 | } catch(Exception e) {/* ignore this exception */}
|
---|
110 | }
|
---|
111 | activeConnection = null;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|