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