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

Last change on this file since 7392 was 7033, checked in by Don-vip, 10 years ago

see #8465 - global use of try-with-resources, according to

  • 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.InputStream;
7import java.text.MessageFormat;
8
9import org.openstreetmap.josm.data.osm.DataSet;
10import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
11import org.openstreetmap.josm.data.osm.PrimitiveId;
12import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
13import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
14import org.openstreetmap.josm.gui.progress.ProgressMonitor;
15import 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 */
27public 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 thrown if id <= 0
43 * @throws IllegalArgumentException thrown if type is null
44 */
45 public OsmServerObjectReader(long id, OsmPrimitiveType type, boolean full) throws IllegalArgumentException {
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 thrown if id <= 0
56 * @throws IllegalArgumentException thrown if type is null
57 */
58 public OsmServerObjectReader(long id, OsmPrimitiveType type, int version) throws IllegalArgumentException {
59 this(id, type, false, version);
60 }
61
62 protected OsmServerObjectReader(long id, OsmPrimitiveType type, boolean full, int version) throws IllegalArgumentException {
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 &gt; 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 thrown if id is null
78 * @throws IllegalArgumentException thrown if id.getUniqueId() &lt;= 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 &gt; 0 required.
88 * @param version the specific version number, if required; -1, otherwise
89 * @throws IllegalArgumentException thrown if id is null
90 * @throws IllegalArgumentException thrown if id.getUniqueId() &lt;= 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
107 * null
108 * @return the downloaded data
109 * @throws OsmTransferException
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 sb.append("/");
122 sb.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 (Exception 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.