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.InputStream;
|
---|
7 | import java.text.MessageFormat;
|
---|
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.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
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 | * It can also download a specific version of the object (however, "full" download is not possible
|
---|
24 | * in that case).
|
---|
25 | *
|
---|
26 | */
|
---|
27 | public class OsmServerObjectReader extends OsmServerReader {
|
---|
28 | /** the id of the object to download */
|
---|
29 | private PrimitiveId id;
|
---|
30 | /** true if a full download is required, i.e. a download including the immediate children */
|
---|
31 | private boolean full;
|
---|
32 | /** the specific version number, if required (incompatible with full), or -1 else */
|
---|
33 | private int version;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Creates a new server object reader for a given id and a primitive type.
|
---|
37 | *
|
---|
38 | * @param id the object id. > 0 required.
|
---|
39 | * @param type the type. Must not be null.
|
---|
40 | * @param full true, if a full download is requested (i.e. a download including
|
---|
41 | * immediate children); false, otherwise
|
---|
42 | * @throws IllegalArgumentException if id <= 0
|
---|
43 | * @throws IllegalArgumentException if type is null
|
---|
44 | */
|
---|
45 | public OsmServerObjectReader(long id, OsmPrimitiveType type, boolean full) {
|
---|
46 | this(id, type, full, -1);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Creates a new server object reader for a given id and a primitive type.
|
---|
51 | *
|
---|
52 | * @param id the object id. > 0 required.
|
---|
53 | * @param type the type. Must not be null.
|
---|
54 | * @param version the specific version number, if required; -1, otherwise
|
---|
55 | * @throws IllegalArgumentException if id <= 0
|
---|
56 | * @throws IllegalArgumentException if type is null
|
---|
57 | */
|
---|
58 | public OsmServerObjectReader(long id, OsmPrimitiveType type, int version) {
|
---|
59 | this(id, type, false, version);
|
---|
60 | }
|
---|
61 |
|
---|
62 | protected OsmServerObjectReader(long id, OsmPrimitiveType type, boolean full, int version) {
|
---|
63 | if (id <= 0)
|
---|
64 | throw new IllegalArgumentException(MessageFormat.format("Expected value > 0 for parameter ''{0}'', got {1}", "id", id));
|
---|
65 | CheckParameterUtil.ensureParameterNotNull(type, "type");
|
---|
66 | this.id = new SimplePrimitiveId(id, type);
|
---|
67 | this.full = full;
|
---|
68 | this.version = version;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Creates a new server object reader for an object with the given <code>id</code>
|
---|
73 | *
|
---|
74 | * @param id the object id. Must not be null. Unique id > 0 required.
|
---|
75 | * @param full true, if a full download is requested (i.e. a download including
|
---|
76 | * immediate children); false, otherwise
|
---|
77 | * @throws IllegalArgumentException if id is null
|
---|
78 | * @throws IllegalArgumentException if id.getUniqueId() <= 0
|
---|
79 | */
|
---|
80 | public OsmServerObjectReader(PrimitiveId id, boolean full) {
|
---|
81 | this(id, full, -1);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Creates a new server object reader for an object with the given <code>id</code>
|
---|
86 | *
|
---|
87 | * @param id the object id. Must not be null. Unique id > 0 required.
|
---|
88 | * @param version the specific version number, if required; -1, otherwise
|
---|
89 | * @throws IllegalArgumentException if id is null
|
---|
90 | * @throws IllegalArgumentException if id.getUniqueId() <= 0
|
---|
91 | */
|
---|
92 | public OsmServerObjectReader(PrimitiveId id, int version) {
|
---|
93 | this(id, false, version);
|
---|
94 | }
|
---|
95 |
|
---|
96 | protected OsmServerObjectReader(PrimitiveId id, boolean full, int version) {
|
---|
97 | CheckParameterUtil.ensureValidPrimitiveId(id, "id");
|
---|
98 | this.id = id;
|
---|
99 | this.full = full;
|
---|
100 | this.version = version;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Downloads and parses the data.
|
---|
105 | *
|
---|
106 | * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
107 | * @return the downloaded data
|
---|
108 | * @throws OsmTransferException if any error occurs during dialog with OSM API
|
---|
109 | */
|
---|
110 | @Override
|
---|
111 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
112 | if (progressMonitor == null) {
|
---|
113 | progressMonitor = NullProgressMonitor.INSTANCE;
|
---|
114 | }
|
---|
115 | progressMonitor.beginTask("", 1);
|
---|
116 | try {
|
---|
117 | progressMonitor.indeterminateSubTask(tr("Downloading OSM data..."));
|
---|
118 | StringBuilder sb = new StringBuilder();
|
---|
119 | sb.append(id.getType().getAPIName())
|
---|
120 | .append('/')
|
---|
121 | .append(id.getUniqueId());
|
---|
122 | if (full && !id.getType().equals(OsmPrimitiveType.NODE)) {
|
---|
123 | sb.append("/full");
|
---|
124 | } else if (version > 0) {
|
---|
125 | sb.append('/').append(version);
|
---|
126 | }
|
---|
127 |
|
---|
128 | try (InputStream in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true))) {
|
---|
129 | if (in == null)
|
---|
130 | return null;
|
---|
131 | return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
|
---|
132 | }
|
---|
133 | } catch (OsmTransferException e) {
|
---|
134 | if (cancel) return null;
|
---|
135 | throw e;
|
---|
136 | } catch (Exception e) {
|
---|
137 | if (cancel) return null;
|
---|
138 | throw new OsmTransferException(e);
|
---|
139 | } finally {
|
---|
140 | progressMonitor.finishTask();
|
---|
141 | activeConnection = null;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | }
|
---|