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

Last change on this file since 3733 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

  • Property svn:eol-style set to native
File size: 4.3 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;
17import 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 */
26public 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}
Note: See TracBrowser for help on using the repository browser.