001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.io; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.io.InputStream; 007import java.text.MessageFormat; 008 009import org.openstreetmap.josm.data.osm.DataSet; 010import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 011import org.openstreetmap.josm.data.osm.history.HistoryDataSet; 012import org.openstreetmap.josm.gui.progress.ProgressMonitor; 013import org.openstreetmap.josm.tools.CheckParameterUtil; 014 015/** 016 * Reads the history of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} from the OSM API server. 017 * 018 */ 019public class OsmServerHistoryReader extends OsmServerReader { 020 021 private OsmPrimitiveType primitiveType; 022 private long id; 023 024 /** 025 * constructor 026 * 027 * @param type the type of the primitive whose history is to be fetched from the server. 028 * Must not be null. 029 * @param id the id of the primitive 030 * 031 * @exception IllegalArgumentException thrown, if type is null 032 */ 033 public OsmServerHistoryReader(OsmPrimitiveType type, long id) throws IllegalArgumentException { 034 CheckParameterUtil.ensureParameterNotNull(type, "type"); 035 if (id < 0) 036 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' >= 0 expected. Got ''{1}''.", "id", id)); 037 this.primitiveType = type; 038 this.id = id; 039 } 040 041 /** 042 * don't use - not implemented! 043 * 044 */ 045 @Override 046 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException { 047 return null; 048 } 049 050 /** 051 * Fetches the history from the OSM API and parses it 052 * 053 * @return the data set with the parsed history data 054 * @throws OsmTransferException thrown, if an exception occurs 055 */ 056 public HistoryDataSet parseHistory(ProgressMonitor progressMonitor) throws OsmTransferException { 057 progressMonitor.beginTask(""); 058 try { 059 progressMonitor.indeterminateSubTask(tr("Contacting OSM Server...")); 060 StringBuilder sb = new StringBuilder(); 061 sb.append(primitiveType.getAPIName()) 062 .append("/").append(id).append("/history"); 063 064 try (InputStream in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true))) { 065 if (in == null) 066 return null; 067 progressMonitor.indeterminateSubTask(tr("Downloading history...")); 068 OsmHistoryReader reader = new OsmHistoryReader(in); 069 return reader.parse(progressMonitor.createSubTaskMonitor(1, true)); 070 } 071 } catch(OsmTransferException e) { 072 throw e; 073 } catch (Exception e) { 074 if (cancel) 075 return null; 076 throw new OsmTransferException(e); 077 } finally { 078 progressMonitor.finishTask(); 079 activeConnection = null; 080 } 081 } 082}