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 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.text.MessageFormat;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.List;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
15 | import org.openstreetmap.josm.data.osm.ChangesetDataSet;
|
---|
16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
17 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
18 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Reads the history of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} from the OSM API server.
|
---|
23 | *
|
---|
24 | */
|
---|
25 | public class OsmServerChangesetReader extends OsmServerReader {
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * constructor
|
---|
29 | *
|
---|
30 | */
|
---|
31 | public OsmServerChangesetReader(){
|
---|
32 | setDoAuthenticate(false);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * don't use - not implemented!
|
---|
37 | *
|
---|
38 | */
|
---|
39 | @Override
|
---|
40 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
41 | return null;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Queries a list
|
---|
46 | * @param query the query specification. Must not be null.
|
---|
47 | * @param monitor a progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
48 | * @return the list of changesets read from the server
|
---|
49 | * @throws IllegalArgumentException thrown if query is null
|
---|
50 | * @throws OsmTransferException thrown if something goes wrong w
|
---|
51 | */
|
---|
52 | public List<Changeset> queryChangesets(ChangesetQuery query, ProgressMonitor monitor) throws OsmTransferException {
|
---|
53 | CheckParameterUtil.ensureParameterNotNull(query, "query");
|
---|
54 | if (monitor == null) {
|
---|
55 | monitor = NullProgressMonitor.INSTANCE;
|
---|
56 | }
|
---|
57 | try {
|
---|
58 | monitor.beginTask(tr("Reading changesets..."));
|
---|
59 | StringBuffer sb = new StringBuffer();
|
---|
60 | sb.append("changesets?").append(query.getQueryString());
|
---|
61 | InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
|
---|
62 | if (in == null)
|
---|
63 | return null;
|
---|
64 | monitor.indeterminateSubTask(tr("Downloading changesets ..."));
|
---|
65 | return OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
|
---|
66 | } catch(OsmTransferException e) {
|
---|
67 | throw e;
|
---|
68 | } catch(IllegalDataException e) {
|
---|
69 | throw new OsmTransferException(e);
|
---|
70 | } finally {
|
---|
71 | monitor.finishTask();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Reads the changeset with id <code>id</code> from the server
|
---|
77 | *
|
---|
78 | * @param id the changeset id. id > 0 required.
|
---|
79 | * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
80 | * @return the changeset read
|
---|
81 | * @throws OsmTransferException thrown if something goes wrong
|
---|
82 | * @throws IllegalArgumentException if id <= 0
|
---|
83 | */
|
---|
84 | public Changeset readChangeset(long id, ProgressMonitor monitor) throws OsmTransferException {
|
---|
85 | if (id <= 0)
|
---|
86 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "id", id));
|
---|
87 | if (monitor == null) {
|
---|
88 | monitor = NullProgressMonitor.INSTANCE;
|
---|
89 | }
|
---|
90 | try {
|
---|
91 | monitor.beginTask(tr("Reading changeset {0} ...",id));
|
---|
92 | StringBuffer sb = new StringBuffer();
|
---|
93 | sb.append("changeset/").append(id);
|
---|
94 | InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
|
---|
95 | if (in == null)
|
---|
96 | return null;
|
---|
97 | monitor.indeterminateSubTask(tr("Downloading changeset {0} ...", id));
|
---|
98 | List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
|
---|
99 | if (changesets == null || changesets.isEmpty())
|
---|
100 | return null;
|
---|
101 | return changesets.get(0);
|
---|
102 | } catch(OsmTransferException e) {
|
---|
103 | throw e;
|
---|
104 | } catch(IllegalDataException e) {
|
---|
105 | throw new OsmTransferException(e);
|
---|
106 | } finally {
|
---|
107 | monitor.finishTask();
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Reads the changeset with id <code>id</code> from the server
|
---|
113 | *
|
---|
114 | * @param ids the list of ids. Ignored if null. Only load changesets for ids > 0.
|
---|
115 | * @param monitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null
|
---|
116 | * @return the changeset read
|
---|
117 | * @throws OsmTransferException thrown if something goes wrong
|
---|
118 | * @throws IllegalArgumentException if id <= 0
|
---|
119 | */
|
---|
120 | public List<Changeset> readChangesets(Collection<Integer> ids, ProgressMonitor monitor) throws OsmTransferException {
|
---|
121 | if (ids == null)
|
---|
122 | return Collections.emptyList();
|
---|
123 | if (monitor == null) {
|
---|
124 | monitor = NullProgressMonitor.INSTANCE;
|
---|
125 | }
|
---|
126 | try {
|
---|
127 | monitor.beginTask(trn("Downloading {0} changeset ...", "Downloading {0} changesets ...",ids.size(),ids.size()));
|
---|
128 | monitor.setTicksCount(ids.size());
|
---|
129 | List<Changeset> ret = new ArrayList<Changeset>();
|
---|
130 | int i=0;
|
---|
131 | for (int id : ids) {
|
---|
132 | if (id <= 0) {
|
---|
133 | continue;
|
---|
134 | }
|
---|
135 | i++;
|
---|
136 | StringBuffer sb = new StringBuffer();
|
---|
137 | sb.append("changeset/").append(id);
|
---|
138 | InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
|
---|
139 | if (in == null)
|
---|
140 | return null;
|
---|
141 | monitor.indeterminateSubTask(tr("({0}/{1}) Downloading changeset {2} ...", i, ids.size(), id));
|
---|
142 | List<Changeset> changesets = OsmChangesetParser.parse(in, monitor.createSubTaskMonitor(1, true));
|
---|
143 | if (changesets == null || changesets.isEmpty()) {
|
---|
144 | continue;
|
---|
145 | }
|
---|
146 | ret.addAll(changesets);
|
---|
147 | monitor.worked(1);
|
---|
148 | }
|
---|
149 | return ret;
|
---|
150 | } catch(OsmTransferException e) {
|
---|
151 | throw e;
|
---|
152 | } catch(IllegalDataException e) {
|
---|
153 | throw new OsmTransferException(e);
|
---|
154 | } finally {
|
---|
155 | monitor.finishTask();
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Downloads the content of a changeset
|
---|
161 | *
|
---|
162 | * @param id the changeset id. >0 required.
|
---|
163 | * @param monitor the progress monitor. {@link NullProgressMonitor#INSTANCE} assumed if null.
|
---|
164 | * @return the changeset content
|
---|
165 | * @throws IllegalArgumentException thrown if id <= 0
|
---|
166 | * @throws OsmTransferException thrown if something went wrong
|
---|
167 | */
|
---|
168 | public ChangesetDataSet downloadChangeset(int id, ProgressMonitor monitor) throws IllegalArgumentException, OsmTransferException {
|
---|
169 | if (id <= 0)
|
---|
170 | throw new IllegalArgumentException(MessageFormat.format("Expected value of type integer > 0 for parameter ''{0}'', got {1}", "id", id));
|
---|
171 | if (monitor == null) {
|
---|
172 | monitor = NullProgressMonitor.INSTANCE;
|
---|
173 | }
|
---|
174 | try {
|
---|
175 | monitor.beginTask(tr("Downloading changeset content"));
|
---|
176 | StringBuffer sb = new StringBuffer();
|
---|
177 | sb.append("changeset/").append(id).append("/download");
|
---|
178 | InputStream in = getInputStream(sb.toString(), monitor.createSubTaskMonitor(1, true));
|
---|
179 | if (in == null)
|
---|
180 | return null;
|
---|
181 | monitor.setCustomText(tr("Downloading content for changeset {0} ...", id));
|
---|
182 | OsmChangesetContentParser parser = new OsmChangesetContentParser(in);
|
---|
183 | return parser.parse(monitor.createSubTaskMonitor(1, true));
|
---|
184 | } catch(OsmDataParsingException e) {
|
---|
185 | throw new OsmTransferException(e);
|
---|
186 | } finally {
|
---|
187 | monitor.finishTask();
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|