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