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