[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
[2599] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[2512] | 6 | import java.util.HashMap;
|
---|
| 7 |
|
---|
| 8 | /**
|
---|
| 9 | * Represents the server capabilities
|
---|
| 10 | *
|
---|
| 11 | */
|
---|
| 12 | public class Capabilities {
|
---|
| 13 | private HashMap<String, HashMap<String,String>> capabilities;
|
---|
| 14 |
|
---|
| 15 | public Capabilities() {
|
---|
| 16 | capabilities = new HashMap<String, HashMap<String,String>>();
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | public boolean isDefined(String element, String attribute) {
|
---|
| 20 | if (! capabilities.containsKey(element)) return false;
|
---|
| 21 | HashMap<String, String> e = capabilities.get(element);
|
---|
| 22 | if (e == null) return false;
|
---|
| 23 | return (e.get(attribute) != null);
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | public String get(String element, String attribute ) {
|
---|
| 27 | if (! capabilities.containsKey(element)) return null;
|
---|
| 28 | HashMap<String, String> e = capabilities.get(element);
|
---|
| 29 | if (e == null) return null;
|
---|
| 30 | return e.get(attribute);
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * replies the value of configuration item in the capabilities as
|
---|
| 35 | * double value
|
---|
| 36 | *
|
---|
| 37 | * @param element the name of the element
|
---|
| 38 | * @param attribute the name of the attribute
|
---|
| 39 | * @return the value; null, if the respective configuration item doesn't exist
|
---|
| 40 | * @throws NumberFormatException if the value is not a valid double
|
---|
| 41 | */
|
---|
| 42 | public Double getDouble(String element, String attribute) throws NumberFormatException {
|
---|
| 43 | String s = get(element, attribute);
|
---|
| 44 | if (s == null) return null;
|
---|
| 45 | return Double.parseDouble(s);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public Long getLong(String element, String attribute) {
|
---|
| 49 | String s = get(element, attribute);
|
---|
| 50 | if (s == null) return null;
|
---|
| 51 | return Long.parseLong(s);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public void put(String element, String attribute, String value) {
|
---|
| 55 | if (capabilities == null) {
|
---|
| 56 | capabilities = new HashMap<String, HashMap<String,String>>();
|
---|
| 57 | }
|
---|
| 58 | if (! capabilities.containsKey(element)) {
|
---|
| 59 | HashMap<String,String> h = new HashMap<String, String>();
|
---|
| 60 | capabilities.put(element, h);
|
---|
| 61 | }
|
---|
| 62 | HashMap<String, String> e = capabilities.get(element);
|
---|
| 63 | e.put(attribute, value);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public void clear() {
|
---|
| 67 | capabilities = new HashMap<String, HashMap<String,String>>();
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public boolean supportsVersion(String version) {
|
---|
| 71 | return get("version", "minimum").compareTo(version) <= 0
|
---|
| 72 | && get("version", "maximum").compareTo(version) >= 0;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[2599] | 75 | /**
|
---|
| 76 | * Replies the max number of objects in a changeset. -1 if either the capabilities
|
---|
| 77 | * don't include this parameter or if the parameter value is illegal (not a number,
|
---|
| 78 | * a negative number)
|
---|
[2711] | 79 | *
|
---|
[2599] | 80 | * @return the max number of objects in a changeset
|
---|
| 81 | */
|
---|
| 82 | public int getMaxChangsetSize() {
|
---|
| 83 | String v = get("changesets", "maximum_elements");
|
---|
| 84 | if (v == null) return -1;
|
---|
| 85 | try {
|
---|
| 86 | int n = Integer.parseInt(v);
|
---|
| 87 | if (n <= 0) {
|
---|
| 88 | System.err.println(tr("Warning: illegal value of attribute '{0}'' of element ''{1}'' in server capabilities. Got ''{2}", "changesets", "maximum_elements", n ));
|
---|
| 89 | return -1;
|
---|
| 90 | }
|
---|
| 91 | return n;
|
---|
| 92 | } catch(NumberFormatException e) {
|
---|
| 93 | System.err.println(tr("Warning: illegal value of attribute '{0}'' of element ''{1}'' in server capabilities. Got ''{2}", "changesets", "maximum_elements", v ));
|
---|
| 94 | return -1;
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
[2512] | 97 | }
|
---|