[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[653] | 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[10212] | 6 | import java.io.IOException;
|
---|
[653] | 7 | import java.io.InputStream;
|
---|
[2852] | 8 | import java.text.MessageFormat;
|
---|
[653] | 9 |
|
---|
| 10 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[1670] | 11 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
[2563] | 12 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
| 13 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
| 14 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
[1811] | 15 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[2852] | 16 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[653] | 17 |
|
---|
[2563] | 18 | /**
|
---|
| 19 | * OsmServerObjectReader reads an individual object from the OSM server.
|
---|
[2711] | 20 | *
|
---|
[2563] | 21 | * It can either download the object including or not including its immediate children.
|
---|
| 22 | * The former case is called a "full download".
|
---|
[6070] | 23 | *
|
---|
[4734] | 24 | * It can also download a specific version of the object (however, "full" download is not possible
|
---|
| 25 | * in that case).
|
---|
[2711] | 26 | *
|
---|
[2563] | 27 | */
|
---|
[653] | 28 | public class OsmServerObjectReader extends OsmServerReader {
|
---|
[2563] | 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;
|
---|
[4734] | 33 | /** the specific version number, if required (incompatible with full), or -1 else */
|
---|
| 34 | private int version;
|
---|
[1071] | 35 |
|
---|
[2563] | 36 | /**
|
---|
| 37 | * Creates a new server object reader for a given id and a primitive type.
|
---|
[2711] | 38 | *
|
---|
[6830] | 39 | * @param id the object id. > 0 required.
|
---|
[2563] | 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
|
---|
[8291] | 43 | * @throws IllegalArgumentException if id <= 0
|
---|
| 44 | * @throws IllegalArgumentException if type is null
|
---|
[2563] | 45 | */
|
---|
[8291] | 46 | public OsmServerObjectReader(long id, OsmPrimitiveType type, boolean full) {
|
---|
[4734] | 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 | *
|
---|
[6830] | 53 | * @param id the object id. > 0 required.
|
---|
[4734] | 54 | * @param type the type. Must not be null.
|
---|
| 55 | * @param version the specific version number, if required; -1, otherwise
|
---|
[8291] | 56 | * @throws IllegalArgumentException if id <= 0
|
---|
| 57 | * @throws IllegalArgumentException if type is null
|
---|
[4734] | 58 | */
|
---|
[8291] | 59 | public OsmServerObjectReader(long id, OsmPrimitiveType type, int version) {
|
---|
[4734] | 60 | this(id, type, false, version);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[8291] | 63 | protected OsmServerObjectReader(long id, OsmPrimitiveType type, boolean full, int version) {
|
---|
[2563] | 64 | if (id <= 0)
|
---|
[2852] | 65 | throw new IllegalArgumentException(MessageFormat.format("Expected value > 0 for parameter ''{0}'', got {1}", "id", id));
|
---|
| 66 | CheckParameterUtil.ensureParameterNotNull(type, "type");
|
---|
[2563] | 67 | this.id = new SimplePrimitiveId(id, type);
|
---|
| 68 | this.full = full;
|
---|
[4734] | 69 | this.version = version;
|
---|
[2563] | 70 | }
|
---|
[1169] | 71 |
|
---|
[2563] | 72 | /**
|
---|
| 73 | * Creates a new server object reader for an object with the given <code>id</code>
|
---|
[2711] | 74 | *
|
---|
[6830] | 75 | * @param id the object id. Must not be null. Unique id > 0 required.
|
---|
[2563] | 76 | * @param full true, if a full download is requested (i.e. a download including
|
---|
| 77 | * immediate children); false, otherwise
|
---|
[8291] | 78 | * @throws IllegalArgumentException if id is null
|
---|
| 79 | * @throws IllegalArgumentException if id.getUniqueId() <= 0
|
---|
[2563] | 80 | */
|
---|
| 81 | public OsmServerObjectReader(PrimitiveId id, boolean full) {
|
---|
[4734] | 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 | *
|
---|
[6830] | 88 | * @param id the object id. Must not be null. Unique id > 0 required.
|
---|
[4734] | 89 | * @param version the specific version number, if required; -1, otherwise
|
---|
[8291] | 90 | * @throws IllegalArgumentException if id is null
|
---|
| 91 | * @throws IllegalArgumentException if id.getUniqueId() <= 0
|
---|
[4734] | 92 | */
|
---|
| 93 | public OsmServerObjectReader(PrimitiveId id, int version) {
|
---|
| 94 | this(id, false, version);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | protected OsmServerObjectReader(PrimitiveId id, boolean full, int version) {
|
---|
[2852] | 98 | CheckParameterUtil.ensureValidPrimitiveId(id, "id");
|
---|
[1146] | 99 | this.id = id;
|
---|
| 100 | this.full = full;
|
---|
[4734] | 101 | this.version = version;
|
---|
[1146] | 102 | }
|
---|
[2563] | 103 |
|
---|
[1071] | 104 | /**
|
---|
[2563] | 105 | * Downloads and parses the data.
|
---|
[2711] | 106 | *
|
---|
[7432] | 107 | * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
[2563] | 108 | * @return the downloaded data
|
---|
[8470] | 109 | * @throws OsmTransferException if any error occurs during dialog with OSM API
|
---|
[1071] | 110 | */
|
---|
[1670] | 111 | @Override
|
---|
[1811] | 112 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[2563] | 113 | if (progressMonitor == null) {
|
---|
| 114 | progressMonitor = NullProgressMonitor.INSTANCE;
|
---|
| 115 | }
|
---|
[1811] | 116 | progressMonitor.beginTask("", 1);
|
---|
[1071] | 117 | try {
|
---|
[2484] | 118 | progressMonitor.indeterminateSubTask(tr("Downloading OSM data..."));
|
---|
[6822] | 119 | StringBuilder sb = new StringBuilder();
|
---|
[8379] | 120 | sb.append(id.getType().getAPIName())
|
---|
[8390] | 121 | .append('/')
|
---|
[8379] | 122 | .append(id.getUniqueId());
|
---|
[8443] | 123 | if (full && !id.getType().equals(OsmPrimitiveType.NODE)) {
|
---|
[1071] | 124 | sb.append("/full");
|
---|
[4734] | 125 | } else if (version > 0) {
|
---|
[8390] | 126 | sb.append('/').append(version);
|
---|
[1670] | 127 | }
|
---|
[1071] | 128 |
|
---|
[7033] | 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 | }
|
---|
[8510] | 134 | } catch (OsmTransferException e) {
|
---|
[1827] | 135 | if (cancel) return null;
|
---|
[1071] | 136 | throw e;
|
---|
[10212] | 137 | } catch (IOException | IllegalDataException e) {
|
---|
[1827] | 138 | if (cancel) return null;
|
---|
[1670] | 139 | throw new OsmTransferException(e);
|
---|
[1811] | 140 | } finally {
|
---|
| 141 | progressMonitor.finishTask();
|
---|
[1827] | 142 | activeConnection = null;
|
---|
[653] | 143 | }
|
---|
[1071] | 144 | }
|
---|
[653] | 145 | }
|
---|