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 |
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
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.xml.sax.SAXException;
|
---|
14 |
|
---|
15 | import sun.reflect.generics.reflectiveObjects.NotImplementedException;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Reads the history of an {@see OsmPrimitive} from the OSM API server.
|
---|
19 | *
|
---|
20 | */
|
---|
21 | public class OsmServerHistoryReader extends OsmServerReader {
|
---|
22 |
|
---|
23 | private OsmPrimitiveType primitiveType;
|
---|
24 | private 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 | * @exception IllegalArgumentException thrown, if type is null
|
---|
34 | */
|
---|
35 | public OsmServerHistoryReader(OsmPrimitiveType type, long id) throws IllegalArgumentException {
|
---|
36 | if (type == null)
|
---|
37 | throw new IllegalArgumentException(tr("parameter ''{0}'' must not be null", "type"));
|
---|
38 | if (id < 0)
|
---|
39 | throw new IllegalArgumentException(tr("parameter ''{0}'' >= 0 expected, got ''{1}''", "id", id));
|
---|
40 | this.primitiveType = type;
|
---|
41 | this.id = id;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * don't use - not implemented!
|
---|
46 | *
|
---|
47 | * @exception NotImplementedException
|
---|
48 | */
|
---|
49 | @Override
|
---|
50 | public DataSet parseOsm() throws OsmTransferException {
|
---|
51 | throw new NotImplementedException();
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Fetches the history from the OSM API and parses it
|
---|
56 | *
|
---|
57 | * @return the data set with the parsed history data
|
---|
58 | * @throws OsmTransferException thrown, if an exception occurs
|
---|
59 | */
|
---|
60 | public HistoryDataSet parseHistory() throws OsmTransferException {
|
---|
61 | InputStream in = null;
|
---|
62 | try {
|
---|
63 | Main.pleaseWaitDlg.progress.setValue(0);
|
---|
64 | Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server..."));
|
---|
65 | StringBuffer sb = new StringBuffer();
|
---|
66 | sb.append(primitiveType.getAPIName())
|
---|
67 | .append("/").append(id).append("/history");
|
---|
68 |
|
---|
69 | in = getInputStream(sb.toString(), Main.pleaseWaitDlg);
|
---|
70 | if (in == null)
|
---|
71 | return null;
|
---|
72 | Main.pleaseWaitDlg.currentAction.setText(tr("Downloading history..."));
|
---|
73 | final OsmHistoryReader reader = new OsmHistoryReader(in);
|
---|
74 | HistoryDataSet data = reader.parse(Main.pleaseWaitDlg);
|
---|
75 | return data;
|
---|
76 | } catch (IOException e) {
|
---|
77 | if (cancel)
|
---|
78 | return null;
|
---|
79 | throw new OsmTransferException(e);
|
---|
80 | } catch (SAXException e) {
|
---|
81 | throw new OsmTransferException(e);
|
---|
82 | } catch(OsmTransferException e) {
|
---|
83 | throw e;
|
---|
84 | } catch (Exception e) {
|
---|
85 | if (cancel)
|
---|
86 | return null;
|
---|
87 | throw new OsmTransferException(e);
|
---|
88 | } finally {
|
---|
89 | if (in != null) {
|
---|
90 | try {
|
---|
91 | in.close();
|
---|
92 | } catch(Exception e) {}
|
---|
93 | activeConnection = null;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|