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