source: josm/trunk/src/org/openstreetmap/josm/io/OsmServerHistoryReader.java@ 7221

Last change on this file since 7221 was 7033, checked in by Don-vip, 10 years ago

see #8465 - global use of try-with-resources, according to

  • Property svn:eol-style set to native
File size: 2.8 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.InputStream;
7import java.text.MessageFormat;
8
9import org.openstreetmap.josm.data.osm.DataSet;
10import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
11import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
12import org.openstreetmap.josm.gui.progress.ProgressMonitor;
13import org.openstreetmap.josm.tools.CheckParameterUtil;
14
15/**
16 * Reads the history of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} from the OSM API server.
17 *
18 */
19public 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 progressMonitor.beginTask("");
58 try {
59 progressMonitor.indeterminateSubTask(tr("Contacting OSM Server..."));
60 StringBuilder sb = new StringBuilder();
61 sb.append(primitiveType.getAPIName())
62 .append("/").append(id).append("/history");
63
64 try (InputStream in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true))) {
65 if (in == null)
66 return null;
67 progressMonitor.indeterminateSubTask(tr("Downloading history..."));
68 OsmHistoryReader reader = new OsmHistoryReader(in);
69 return reader.parse(progressMonitor.createSubTaskMonitor(1, true));
70 }
71 } catch(OsmTransferException e) {
72 throw e;
73 } catch (Exception e) {
74 if (cancel)
75 return null;
76 throw new OsmTransferException(e);
77 } finally {
78 progressMonitor.finishTask();
79 activeConnection = null;
80 }
81 }
82}
Note: See TracBrowser for help on using the repository browser.