source: josm/trunk/src/org/openstreetmap/josm/io/OsmServerObjectReader.java@ 13455

Last change on this file since 13455 was 12713, checked in by bastiK, 7 years ago

see #15229 - remove dependencies of CheckParameterUtil on various data classes

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