1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.HashSet;
|
---|
6 | import java.util.Locale;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.Set;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
11 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
12 | import org.openstreetmap.josm.tools.TextTagParser;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Utility methods/constants that are useful for generic OSM tag handling.
|
---|
16 | */
|
---|
17 | public final class OsmUtils {
|
---|
18 |
|
---|
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"));
|
---|
25 |
|
---|
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";
|
---|
41 |
|
---|
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 |
|
---|
55 | private OsmUtils() {
|
---|
56 | // Hide default constructor for utils classes
|
---|
57 | }
|
---|
58 |
|
---|
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 | */
|
---|
66 | public static Boolean getOsmBoolean(String value) {
|
---|
67 | if (value == null) return null;
|
---|
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 | }
|
---|
73 |
|
---|
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 | */
|
---|
81 | public static String getNamedOsmBoolean(String value) {
|
---|
82 | Boolean res = getOsmBoolean(value);
|
---|
83 | return res == null ? value : (res ? trueval : falseval);
|
---|
84 | }
|
---|
85 |
|
---|
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 | */
|
---|
91 | public static boolean isReversed(String value) {
|
---|
92 | return REVERSE_VALUES.contains(value);
|
---|
93 | }
|
---|
94 |
|
---|
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 | */
|
---|
100 | public static boolean isTrue(String value) {
|
---|
101 | return TRUE_VALUES.contains(value);
|
---|
102 | }
|
---|
103 |
|
---|
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 | */
|
---|
109 | public static boolean isFalse(String value) {
|
---|
110 | return FALSE_VALUES.contains(value);
|
---|
111 | }
|
---|
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])
|
---|
125 | ? new Node(LatLon.ZERO)
|
---|
126 | : "w".equals(x[0]) || "way".equals(x[0]) || /*for MapCSS related usage*/ "area".equals(x[0])
|
---|
127 | ? new Way()
|
---|
128 | : "r".equals(x[0]) || "relation".equals(x[0])
|
---|
129 | ? new Relation()
|
---|
130 | : null;
|
---|
131 | if (p == null) {
|
---|
132 | throw new IllegalArgumentException("Expecting n/node/w/way/r/relation/area, but got '" + x[0] + '\'');
|
---|
133 | }
|
---|
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 | }
|
---|
138 | }
|
---|
139 | return p;
|
---|
140 | }
|
---|
141 | }
|
---|