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