source: josm/trunk/src/org/openstreetmap/josm/io/ChangesetQuery.java@ 8345

Last change on this file since 8345 was 8304, checked in by Don-vip, 9 years ago

simplify URL encoding/decoding

  • Property svn:eol-style set to native
File size: 19.7 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.text.DateFormat;
7import java.text.MessageFormat;
8import java.text.ParseException;
9import java.util.Arrays;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.Date;
13import java.util.HashMap;
14import java.util.HashSet;
15import java.util.Map;
16import java.util.Map.Entry;
17
18import org.openstreetmap.josm.data.Bounds;
19import org.openstreetmap.josm.data.coor.LatLon;
20import org.openstreetmap.josm.tools.CheckParameterUtil;
21import org.openstreetmap.josm.tools.Utils;
22import org.openstreetmap.josm.tools.date.DateUtils;
23
24public class ChangesetQuery {
25
26 /**
27 * Replies a changeset query object from the query part of a OSM API URL for querying changesets.
28 *
29 * @param query the query part
30 * @return the query object
31 * @throws ChangesetQueryUrlException if query doesn't consist of valid query parameters
32 */
33 public static ChangesetQuery buildFromUrlQuery(String query) throws ChangesetQueryUrlException{
34 return new ChangesetQueryUrlParser().parse(query);
35 }
36
37 /** the user id this query is restricted to. null, if no restriction to a user id applies */
38 private Integer uid = null;
39 /** the user name this query is restricted to. null, if no restriction to a user name applies */
40 private String userName = null;
41 /** the bounding box this query is restricted to. null, if no restriction to a bounding box applies */
42 private Bounds bounds = null;
43
44 private Date closedAfter = null;
45 private Date createdBefore = null;
46 /** indicates whether only open changesets are queried. null, if no restrictions regarding open changesets apply */
47 private Boolean open = null;
48 /** indicates whether only closed changesets are queried. null, if no restrictions regarding open changesets apply */
49 private Boolean closed = null;
50 /** a collection of changeset ids to query for */
51 private Collection<Long> changesetIds = null;
52
53 /**
54 * Constructs a new {@code ChangesetQuery}.
55 */
56 public ChangesetQuery() {
57
58 }
59
60 /**
61 * Restricts the query to changesets owned by the user with id <code>uid</code>.
62 *
63 * @param uid the uid of the user. &gt; 0 expected.
64 * @return the query object with the applied restriction
65 * @throws IllegalArgumentException if uid &lt;= 0
66 * @see #forUser(String)
67 */
68 public ChangesetQuery forUser(int uid) {
69 if (uid <= 0)
70 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "uid", uid));
71 this.uid = uid;
72 this.userName = null;
73 return this;
74 }
75
76 /**
77 * Restricts the query to changesets owned by the user with user name <code>username</code>.
78 *
79 * Caveat: for historical reasons the username might not be unique! It is recommended to use
80 * {@link #forUser(int)} to restrict the query to a specific user.
81 *
82 * @param username the username. Must not be null.
83 * @return the query object with the applied restriction
84 * @throws IllegalArgumentException if username is null.
85 * @see #forUser(int)
86 */
87 public ChangesetQuery forUser(String username) {
88 CheckParameterUtil.ensureParameterNotNull(username, "username");
89 this.userName = username;
90 this.uid = null;
91 return this;
92 }
93
94 /**
95 * Replies true if this query is restricted to user whom we only know the user name for.
96 *
97 * @return true if this query is restricted to user whom we only know the user name for
98 */
99 public boolean isRestrictedToPartiallyIdentifiedUser() {
100 return userName != null;
101 }
102
103 /**
104 * Replies the user name which this query is restricted to. null, if this query isn't
105 * restricted to a user name, i.e. if {@link #isRestrictedToPartiallyIdentifiedUser()} is false.
106 *
107 * @return the user name which this query is restricted to
108 */
109 public String getUserName() {
110 return userName;
111 }
112
113 /**
114 * Replies true if this query is restricted to user whom know the user id for.
115 *
116 * @return true if this query is restricted to user whom know the user id for
117 */
118 public boolean isRestrictedToFullyIdentifiedUser() {
119 return uid > 0;
120 }
121
122 /**
123 * Replies a query which is restricted to a bounding box.
124 *
125 * @param minLon min longitude of the bounding box. Valid longitude value expected.
126 * @param minLat min latitude of the bounding box. Valid latitude value expected.
127 * @param maxLon max longitude of the bounding box. Valid longitude value expected.
128 * @param maxLat max latitude of the bounding box. Valid latitude value expected.
129 *
130 * @return the restricted changeset query
131 * @throws IllegalArgumentException if either of the parameters isn't a valid longitude or
132 * latitude value
133 */
134 public ChangesetQuery inBbox(double minLon, double minLat, double maxLon, double maxLat) {
135 if (!LatLon.isValidLon(minLon))
136 throw new IllegalArgumentException(tr("Illegal longitude value for parameter ''{0}'', got {1}", "minLon", minLon));
137 if (!LatLon.isValidLon(maxLon))
138 throw new IllegalArgumentException(tr("Illegal longitude value for parameter ''{0}'', got {1}", "maxLon", maxLon));
139 if (!LatLon.isValidLat(minLat))
140 throw new IllegalArgumentException(tr("Illegal latitude value for parameter ''{0}'', got {1}", "minLat", minLat));
141 if (!LatLon.isValidLat(maxLat))
142 throw new IllegalArgumentException(tr("Illegal longitude value for parameter ''{0}'', got {1}", "maxLat", maxLat));
143
144 return inBbox(new LatLon(minLon, minLat), new LatLon(maxLon, maxLat));
145 }
146
147 /**
148 * Replies a query which is restricted to a bounding box.
149 *
150 * @param min the min lat/lon coordinates of the bounding box. Must not be null.
151 * @param max the max lat/lon coordiantes of the bounding box. Must not be null.
152 *
153 * @return the restricted changeset query
154 * @throws IllegalArgumentException if min is null
155 * @throws IllegalArgumentException if max is null
156 */
157 public ChangesetQuery inBbox(LatLon min, LatLon max) {
158 CheckParameterUtil.ensureParameterNotNull(min, "min");
159 CheckParameterUtil.ensureParameterNotNull(max, "max");
160 this.bounds = new Bounds(min,max);
161 return this;
162 }
163
164 /**
165 * Replies a query which is restricted to a bounding box given by <code>bbox</code>.
166 *
167 * @param bbox the bounding box. Must not be null.
168 * @return the changeset query
169 * @throws IllegalArgumentException if bbox is null.
170 */
171 public ChangesetQuery inBbox(Bounds bbox) {
172 CheckParameterUtil.ensureParameterNotNull(bbox, "bbox");
173 this.bounds = bbox;
174 return this;
175 }
176
177 /**
178 * Restricts the result to changesets which have been closed after the date given by <code>d</code>.
179 * <code>d</code> d is a date relative to the current time zone.
180 *
181 * @param d the date . Must not be null.
182 * @return the restricted changeset query
183 * @throws IllegalArgumentException if d is null
184 */
185 public ChangesetQuery closedAfter(Date d) {
186 CheckParameterUtil.ensureParameterNotNull(d, "d");
187 this.closedAfter = d;
188 return this;
189 }
190
191 /**
192 * Restricts the result to changesets which have been closed after <code>closedAfter</code> and which
193 * habe been created before <code>createdBefore</code>. Both dates are expressed relative to the current
194 * time zone.
195 *
196 * @param closedAfter only reply changesets closed after this date. Must not be null.
197 * @param createdBefore only reply changesets created before this date. Must not be null.
198 * @return the restricted changeset query
199 * @throws IllegalArgumentException if closedAfter is null
200 * @throws IllegalArgumentException if createdBefore is null
201 */
202 public ChangesetQuery closedAfterAndCreatedBefore(Date closedAfter, Date createdBefore ) {
203 CheckParameterUtil.ensureParameterNotNull(closedAfter, "closedAfter");
204 CheckParameterUtil.ensureParameterNotNull(createdBefore, "createdBefore");
205 this.closedAfter = closedAfter;
206 this.createdBefore = createdBefore;
207 return this;
208 }
209
210 /**
211 * Restricts the result to changesets which are or aren't open, depending on the value of
212 * <code>isOpen</code>
213 *
214 * @param isOpen whether changesets should or should not be open
215 * @return the restricted changeset query
216 */
217 public ChangesetQuery beingOpen(boolean isOpen) {
218 this.open = isOpen;
219 return this;
220 }
221
222 /**
223 * Restricts the result to changesets which are or aren't closed, depending on the value of
224 * <code>isClosed</code>
225 *
226 * @param isClosed whether changesets should or should not be open
227 * @return the restricted changeset query
228 */
229 public ChangesetQuery beingClosed(boolean isClosed) {
230 this.closed = isClosed;
231 return this;
232 }
233
234 /**
235 * Restricts the query to the given changeset ids (which are added to previously added ones).
236 *
237 * @param changesetIds the changeset ids
238 * @return the query object with the applied restriction
239 * @throws IllegalArgumentException if changesetIds is null.
240 */
241 public ChangesetQuery forChangesetIds(Collection<Long> changesetIds) {
242 CheckParameterUtil.ensureParameterNotNull(changesetIds, "changesetIds");
243 this.changesetIds = changesetIds;
244 return this;
245 }
246
247 /**
248 * Replies the query string to be used in a query URL for the OSM API.
249 *
250 * @return the query string
251 */
252 public String getQueryString() {
253 StringBuilder sb = new StringBuilder();
254 if (uid != null) {
255 sb.append("user").append("=").append(uid);
256 } else if (userName != null) {
257 sb.append("display_name").append("=").append(Utils.encodeUrl(userName));
258 }
259 if (bounds != null) {
260 if (sb.length() > 0) {
261 sb.append("&");
262 }
263 sb.append("bbox=").append(bounds.encodeAsString(","));
264 }
265 if (closedAfter != null && createdBefore != null) {
266 if (sb.length() > 0) {
267 sb.append("&");
268 }
269 DateFormat df = DateUtils.newIsoDateTimeFormat();
270 sb.append("time").append("=").append(df.format(closedAfter));
271 sb.append(",").append(df.format(createdBefore));
272 } else if (closedAfter != null) {
273 if (sb.length() > 0) {
274 sb.append("&");
275 }
276 DateFormat df = DateUtils.newIsoDateTimeFormat();
277 sb.append("time").append("=").append(df.format(closedAfter));
278 }
279
280 if (open != null) {
281 if (sb.length() > 0) {
282 sb.append("&");
283 }
284 sb.append("open=").append(Boolean.toString(open));
285 } else if (closed != null) {
286 if (sb.length() > 0) {
287 sb.append("&");
288 }
289 sb.append("closed=").append(Boolean.toString(closed));
290 } else if (changesetIds != null) {
291 // since 2013-12-05, see https://github.com/openstreetmap/openstreetmap-website/commit/1d1f194d598e54a5d6fb4f38fb569d4138af0dc8
292 if (sb.length() > 0) {
293 sb.append("&");
294 }
295 sb.append("changesets=").append(Utils.join(",", changesetIds));
296 }
297 return sb.toString();
298 }
299
300 @Override
301 public String toString() {
302 return getQueryString();
303 }
304
305 public static class ChangesetQueryUrlException extends Exception {
306
307 /**
308 * Constructs a new {@code ChangesetQueryUrlException} with the specified detail message.
309 *
310 * @param message the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method.
311 */
312 public ChangesetQueryUrlException(String message) {
313 super(message);
314 }
315
316 /**
317 * Constructs a new {@code ChangesetQueryUrlException} with the specified cause and a detail message of
318 * <tt>(cause==null ? null : cause.toString())</tt> (which typically contains the class and detail message of <tt>cause</tt>).
319 *
320 * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method).
321 * (A <tt>null</tt> value is permitted, and indicates that the cause is nonexistent or unknown.)
322 */
323 public ChangesetQueryUrlException(Throwable cause) {
324 super(cause);
325 }
326 }
327
328 public static class ChangesetQueryUrlParser {
329 protected int parseUid(String value) throws ChangesetQueryUrlException {
330 if (value == null || value.trim().isEmpty())
331 throw new ChangesetQueryUrlException(tr("Unexpected value for ''{0}'' in changeset query url, got {1}", "uid", value));
332 int id;
333 try {
334 id = Integer.parseInt(value);
335 if (id <= 0)
336 throw new ChangesetQueryUrlException(tr("Unexpected value for ''{0}'' in changeset query url, got {1}", "uid", value));
337 } catch(NumberFormatException e) {
338 throw new ChangesetQueryUrlException(tr("Unexpected value for ''{0}'' in changeset query url, got {1}", "uid", value));
339 }
340 return id;
341 }
342
343 protected boolean parseBoolean(String value, String parameter) throws ChangesetQueryUrlException {
344 if (value == null || value.trim().isEmpty())
345 throw new ChangesetQueryUrlException(tr("Unexpected value for ''{0}'' in changeset query url, got {1}", parameter, value));
346 switch (value) {
347 case "true":
348 return true;
349 case "false":
350 return false;
351 default:
352 throw new ChangesetQueryUrlException(tr("Unexpected value for ''{0}'' in changeset query url, got {1}", parameter, value));
353 }
354 }
355
356 protected Date parseDate(String value, String parameter) throws ChangesetQueryUrlException {
357 if (value == null || value.trim().isEmpty())
358 throw new ChangesetQueryUrlException(tr("Unexpected value for ''{0}'' in changeset query url, got {1}", parameter, value));
359 DateFormat formatter = DateUtils.newIsoDateTimeFormat();
360 try {
361 return formatter.parse(value);
362 } catch(ParseException e) {
363 throw new ChangesetQueryUrlException(tr("Unexpected value for ''{0}'' in changeset query url, got {1}", parameter, value));
364 }
365 }
366
367 protected Date[] parseTime(String value) throws ChangesetQueryUrlException {
368 String[] dates = value.split(",");
369 if (dates == null || dates.length == 0 || dates.length > 2)
370 throw new ChangesetQueryUrlException(tr("Unexpected value for ''{0}'' in changeset query url, got {1}", "time", value));
371 if (dates.length == 1)
372 return new Date[]{parseDate(dates[0], "time")};
373 else if (dates.length == 2)
374 return new Date[]{parseDate(dates[0], "time"), parseDate(dates[1], "time")};
375 return null;
376 }
377
378 protected Collection<Long> parseLongs(String value) {
379 return value == null || value.isEmpty()
380 ? Collections.<Long>emptySet() :
381 new HashSet<>(Utils.transform(Arrays.asList(value.split(",")), new Utils.Function<String, Long>() {
382 @Override
383 public Long apply(String x) {
384 return Long.valueOf(x);
385 }
386 }));
387 }
388
389 protected ChangesetQuery createFromMap(Map<String, String> queryParams) throws ChangesetQueryUrlException {
390 ChangesetQuery csQuery = new ChangesetQuery();
391
392 for (Entry<String, String> entry: queryParams.entrySet()) {
393 String k = entry.getKey();
394 switch(k) {
395 case "uid":
396 if (queryParams.containsKey("display_name"))
397 throw new ChangesetQueryUrlException(tr("Cannot create a changeset query including both the query parameters ''uid'' and ''display_name''"));
398 csQuery.forUser(parseUid(queryParams.get("uid")));
399 break;
400 case "display_name":
401 if (queryParams.containsKey("uid"))
402 throw new ChangesetQueryUrlException(tr("Cannot create a changeset query including both the query parameters ''uid'' and ''display_name''"));
403 csQuery.forUser(queryParams.get("display_name"));
404 break;
405 case "open":
406 csQuery.beingOpen(parseBoolean(entry.getValue(), "open"));
407 break;
408 case "closed":
409 csQuery.beingClosed(parseBoolean(entry.getValue(), "closed"));
410 break;
411 case "time":
412 Date[] dates = parseTime(entry.getValue());
413 switch(dates.length) {
414 case 1:
415 csQuery.closedAfter(dates[0]);
416 break;
417 case 2:
418 csQuery.closedAfterAndCreatedBefore(dates[0], dates[1]);
419 break;
420 }
421 break;
422 case "bbox":
423 try {
424 csQuery.inBbox(new Bounds(entry.getValue(), ","));
425 } catch (IllegalArgumentException e) {
426 throw new ChangesetQueryUrlException(e);
427 }
428 break;
429 case "changesets":
430 try {
431 csQuery.forChangesetIds(parseLongs(entry.getValue()));
432 } catch (NumberFormatException e) {
433 throw new ChangesetQueryUrlException(e);
434 }
435 break;
436 default:
437 throw new ChangesetQueryUrlException(tr("Unsupported parameter ''{0}'' in changeset query string", k));
438 }
439 }
440 return csQuery;
441 }
442
443 protected Map<String,String> createMapFromQueryString(String query) {
444 Map<String,String> queryParams = new HashMap<>();
445 String[] keyValuePairs = query.split("&");
446 for (String keyValuePair: keyValuePairs) {
447 String[] kv = keyValuePair.split("=");
448 queryParams.put(kv[0], kv.length > 1 ? kv[1] : "");
449 }
450 return queryParams;
451 }
452
453 /**
454 * Parses the changeset query given as URL query parameters and replies a {@link ChangesetQuery}.
455 *
456 * <code>query</code> is the query part of a API url for querying changesets,
457 * see <a href="http://wiki.openstreetmap.org/wiki/API_v0.6#Query:_GET_.2Fapi.2F0.6.2Fchangesets">OSM API</a>.
458 *
459 * Example for an query string:<br>
460 * <pre>
461 * uid=1234&amp;open=true
462 * </pre>
463 *
464 * @param query the query string. If null, an empty query (identical to a query for all changesets) is
465 * assumed
466 * @return the changeset query
467 * @throws ChangesetQueryUrlException if the query string doesn't represent a legal query for changesets
468 */
469 public ChangesetQuery parse(String query) throws ChangesetQueryUrlException {
470 if (query == null)
471 return new ChangesetQuery();
472 query = query.trim();
473 if (query.isEmpty())
474 return new ChangesetQuery();
475 Map<String,String> queryParams = createMapFromQueryString(query);
476 return createFromMap(queryParams);
477 }
478 }
479}
Note: See TracBrowser for help on using the repository browser.