| 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.InputStream; |
|---|
| 7 | import java.text.MessageFormat; |
|---|
| 8 | |
|---|
| 9 | import org.openstreetmap.josm.data.osm.DataSet; |
|---|
| 10 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType; |
|---|
| 11 | import org.openstreetmap.josm.data.osm.history.HistoryDataSet; |
|---|
| 12 | import org.openstreetmap.josm.gui.progress.ProgressMonitor; |
|---|
| 13 | import org.openstreetmap.josm.tools.CheckParameterUtil; |
|---|
| 14 | |
|---|
| 15 | /** |
|---|
| 16 | * Reads the history of an {@see OsmPrimitive} from the OSM API server. |
|---|
| 17 | * |
|---|
| 18 | */ |
|---|
| 19 | public class OsmServerHistoryReader extends OsmServerReader { |
|---|
| 20 | |
|---|
| 21 | private OsmPrimitiveType primitiveType; |
|---|
| 22 | private long id; |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * constructor |
|---|
| 26 | * |
|---|
| 27 | * @param type the type of the primitive whose history is to be fetched from the server. |
|---|
| 28 | * Must not be null. |
|---|
| 29 | * @param id the id of the primitive |
|---|
| 30 | * |
|---|
| 31 | * @exception IllegalArgumentException thrown, if type is null |
|---|
| 32 | */ |
|---|
| 33 | public OsmServerHistoryReader(OsmPrimitiveType type, long id) throws IllegalArgumentException { |
|---|
| 34 | CheckParameterUtil.ensureParameterNotNull(type, "type"); |
|---|
| 35 | if (id < 0) |
|---|
| 36 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' >= 0 expected. Got ''{1}''.", "id", id)); |
|---|
| 37 | this.primitiveType = type; |
|---|
| 38 | this.id = id; |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * don't use - not implemented! |
|---|
| 43 | * |
|---|
| 44 | */ |
|---|
| 45 | @Override |
|---|
| 46 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException { |
|---|
| 47 | return null; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * Fetches the history from the OSM API and parses it |
|---|
| 52 | * |
|---|
| 53 | * @return the data set with the parsed history data |
|---|
| 54 | * @throws OsmTransferException thrown, if an exception occurs |
|---|
| 55 | */ |
|---|
| 56 | public HistoryDataSet parseHistory(ProgressMonitor progressMonitor) throws OsmTransferException { |
|---|
| 57 | InputStream in = null; |
|---|
| 58 | progressMonitor.beginTask(""); |
|---|
| 59 | try { |
|---|
| 60 | progressMonitor.indeterminateSubTask(tr("Contacting OSM Server...")); |
|---|
| 61 | StringBuffer sb = new StringBuffer(); |
|---|
| 62 | sb.append(primitiveType.getAPIName()) |
|---|
| 63 | .append("/").append(id).append("/history"); |
|---|
| 64 | |
|---|
| 65 | in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true)); |
|---|
| 66 | if (in == null) |
|---|
| 67 | return null; |
|---|
| 68 | progressMonitor.indeterminateSubTask(tr("Downloading history...")); |
|---|
| 69 | final OsmHistoryReader reader = new OsmHistoryReader(in); |
|---|
| 70 | HistoryDataSet data = reader.parse(progressMonitor.createSubTaskMonitor(1, true)); |
|---|
| 71 | return data; |
|---|
| 72 | } catch(OsmTransferException e) { |
|---|
| 73 | throw e; |
|---|
| 74 | } catch (Exception e) { |
|---|
| 75 | if (cancel) |
|---|
| 76 | return null; |
|---|
| 77 | throw new OsmTransferException(e); |
|---|
| 78 | } finally { |
|---|
| 79 | progressMonitor.finishTask(); |
|---|
| 80 | if (in != null) { |
|---|
| 81 | try { |
|---|
| 82 | in.close(); |
|---|
| 83 | } catch(Exception e) {} |
|---|
| 84 | activeConnection = null; |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | } |
|---|