[702] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.data.osm;
|
---|
| 3 |
|
---|
| 4 | import java.util.Arrays;
|
---|
[8390] | 5 | import java.util.HashSet;
|
---|
[702] | 6 | import java.util.Locale;
|
---|
[7356] | 7 | import java.util.Map;
|
---|
[8390] | 8 | import java.util.Set;
|
---|
[702] | 9 |
|
---|
[7798] | 10 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[7356] | 11 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
| 12 | import org.openstreetmap.josm.tools.TextTagParser;
|
---|
| 13 |
|
---|
[12187] | 14 | /**
|
---|
| 15 | * Utility methods/constants that are useful for generic OSM tag handling.
|
---|
| 16 | */
|
---|
[6362] | 17 | public final class OsmUtils {
|
---|
[6792] | 18 |
|
---|
[8390] | 19 | private static final Set<String> TRUE_VALUES = new HashSet<>(Arrays
|
---|
| 20 | .asList("true", "yes", "1", "on"));
|
---|
| 21 | private static final Set<String> FALSE_VALUES = new HashSet<>(Arrays
|
---|
| 22 | .asList("false", "no", "0", "off"));
|
---|
| 23 | private static final Set<String> REVERSE_VALUES = new HashSet<>(Arrays
|
---|
| 24 | .asList("reverse", "-1"));
|
---|
[702] | 25 |
|
---|
[12187] | 26 | /**
|
---|
| 27 | * A value that should be used to indicate true
|
---|
| 28 | * @since 12186
|
---|
| 29 | */
|
---|
| 30 | public static final String TRUE_VALUE = "yes";
|
---|
| 31 | /**
|
---|
| 32 | * A value that should be used to indicate false
|
---|
| 33 | * @since 12186
|
---|
| 34 | */
|
---|
| 35 | public static final String FALSE_VALUE = "no";
|
---|
| 36 | /**
|
---|
| 37 | * A value that should be used to indicate that a property applies reversed on the way
|
---|
| 38 | * @since 12186
|
---|
| 39 | */
|
---|
| 40 | public static final String REVERSE_VALUE = "-1";
|
---|
[702] | 41 |
|
---|
[12187] | 42 | /**
|
---|
| 43 | * Discouraged synonym for {@link #TRUE_VALUE}
|
---|
| 44 | */
|
---|
| 45 | public static final String trueval = TRUE_VALUE;
|
---|
| 46 | /**
|
---|
| 47 | * Discouraged synonym for {@link #FALSE_VALUE}
|
---|
| 48 | */
|
---|
| 49 | public static final String falseval = FALSE_VALUE;
|
---|
| 50 | /**
|
---|
| 51 | * Discouraged synonym for {@link #REVERSE_VALUE}
|
---|
| 52 | */
|
---|
| 53 | public static final String reverseval = REVERSE_VALUE;
|
---|
| 54 |
|
---|
[8390] | 55 | private OsmUtils() {
|
---|
| 56 | // Hide default constructor for utils classes
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[12187] | 59 | /**
|
---|
| 60 | * Converts a string to a boolean value
|
---|
| 61 | * @param value The string to convert
|
---|
| 62 | * @return {@link Boolean#TRUE} if that string represents a true value,
|
---|
| 63 | * {@link Boolean#FALSE} if it represents a false value,
|
---|
| 64 | * <code>null</code> otherwise.
|
---|
| 65 | */
|
---|
[1169] | 66 | public static Boolean getOsmBoolean(String value) {
|
---|
[8510] | 67 | if (value == null) return null;
|
---|
[1169] | 68 | String lowerValue = value.toLowerCase(Locale.ENGLISH);
|
---|
| 69 | if (TRUE_VALUES.contains(lowerValue)) return Boolean.TRUE;
|
---|
| 70 | if (FALSE_VALUES.contains(lowerValue)) return Boolean.FALSE;
|
---|
| 71 | return null;
|
---|
| 72 | }
|
---|
[2573] | 73 |
|
---|
[12187] | 74 | /**
|
---|
| 75 | * Normalizes the OSM boolean value
|
---|
| 76 | * @param value The tag value
|
---|
| 77 | * @return The best true/false value or the old value if the input cannot be converted.
|
---|
| 78 | * @see #TRUE_VALUE
|
---|
| 79 | * @see #FALSE_VALUE
|
---|
| 80 | */
|
---|
[1169] | 81 | public static String getNamedOsmBoolean(String value) {
|
---|
| 82 | Boolean res = getOsmBoolean(value);
|
---|
| 83 | return res == null ? value : (res ? trueval : falseval);
|
---|
| 84 | }
|
---|
[2573] | 85 |
|
---|
[12187] | 86 | /**
|
---|
| 87 | * Check if the value is a value indicating that a property applies reversed.
|
---|
| 88 | * @param value The value to check
|
---|
| 89 | * @return true if it is reversed.
|
---|
| 90 | */
|
---|
[2573] | 91 | public static boolean isReversed(String value) {
|
---|
| 92 | return REVERSE_VALUES.contains(value);
|
---|
| 93 | }
|
---|
[2683] | 94 |
|
---|
[12187] | 95 | /**
|
---|
| 96 | * Check if a tag value represents a boolean true value
|
---|
| 97 | * @param value The value to check
|
---|
| 98 | * @return true if it is a true value.
|
---|
| 99 | */
|
---|
[2683] | 100 | public static boolean isTrue(String value) {
|
---|
| 101 | return TRUE_VALUES.contains(value);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[12187] | 104 | /**
|
---|
| 105 | * Check if a tag value represents a boolean false value
|
---|
| 106 | * @param value The value to check
|
---|
| 107 | * @return true if it is a true value.
|
---|
| 108 | */
|
---|
[2683] | 109 | public static boolean isFalse(String value) {
|
---|
| 110 | return FALSE_VALUES.contains(value);
|
---|
| 111 | }
|
---|
[7356] | 112 |
|
---|
| 113 | /**
|
---|
| 114 | * Creates a new OSM primitive according to the given assertion. Originally written for unit tests,
|
---|
| 115 | * this can also be used in another places like validation of local MapCSS validator rules.
|
---|
| 116 | * @param assertion The assertion describing OSM primitive (ex: "way name=Foo railway=rail")
|
---|
| 117 | * @return a new OSM primitive according to the given assertion
|
---|
| 118 | * @throws IllegalArgumentException if assertion is null or if the primitive type cannot be deduced from it
|
---|
| 119 | * @since 7356
|
---|
| 120 | */
|
---|
| 121 | public static OsmPrimitive createPrimitive(String assertion) {
|
---|
| 122 | CheckParameterUtil.ensureParameterNotNull(assertion, "assertion");
|
---|
| 123 | final String[] x = assertion.split("\\s+", 2);
|
---|
| 124 | final OsmPrimitive p = "n".equals(x[0]) || "node".equals(x[0])
|
---|
[7798] | 125 | ? new Node(LatLon.ZERO)
|
---|
[8257] | 126 | : "w".equals(x[0]) || "way".equals(x[0]) || /*for MapCSS related usage*/ "area".equals(x[0])
|
---|
[7356] | 127 | ? new Way()
|
---|
| 128 | : "r".equals(x[0]) || "relation".equals(x[0])
|
---|
| 129 | ? new Relation()
|
---|
| 130 | : null;
|
---|
| 131 | if (p == null) {
|
---|
[8846] | 132 | throw new IllegalArgumentException("Expecting n/node/w/way/r/relation/area, but got '" + x[0] + '\'');
|
---|
[7356] | 133 | }
|
---|
[10674] | 134 | if (x.length > 1) {
|
---|
| 135 | for (final Map.Entry<String, String> i : TextTagParser.readTagsFromText(x[1]).entrySet()) {
|
---|
| 136 | p.put(i.getKey(), i.getValue());
|
---|
| 137 | }
|
---|
[7356] | 138 | }
|
---|
| 139 | return p;
|
---|
| 140 | }
|
---|
[702] | 141 | }
|
---|