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.Collection;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.Locale;
|
---|
8 | import java.util.Map;
|
---|
9 | import java.util.Objects;
|
---|
10 | import java.util.Set;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
13 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
14 | import org.openstreetmap.josm.tools.TextTagParser;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Utility methods/constants that are useful for generic OSM tag handling.
|
---|
18 | */
|
---|
19 | public final class OsmUtils {
|
---|
20 |
|
---|
21 | private static final Set<String> TRUE_VALUES = new HashSet<>(Arrays
|
---|
22 | .asList("true", "yes", "1", "on"));
|
---|
23 | private static final Set<String> FALSE_VALUES = new HashSet<>(Arrays
|
---|
24 | .asList("false", "no", "0", "off"));
|
---|
25 | private static final Set<String> REVERSE_VALUES = new HashSet<>(Arrays
|
---|
26 | .asList("reverse", "-1"));
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * A value that should be used to indicate true
|
---|
30 | * @since 12186
|
---|
31 | */
|
---|
32 | public static final String TRUE_VALUE = "yes";
|
---|
33 | /**
|
---|
34 | * A value that should be used to indicate false
|
---|
35 | * @since 12186
|
---|
36 | */
|
---|
37 | public static final String FALSE_VALUE = "no";
|
---|
38 | /**
|
---|
39 | * A value that should be used to indicate that a property applies reversed on the way
|
---|
40 | * @since 12186
|
---|
41 | */
|
---|
42 | public static final String REVERSE_VALUE = "-1";
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Discouraged synonym for {@link #TRUE_VALUE}
|
---|
46 | */
|
---|
47 | public static final String trueval = TRUE_VALUE;
|
---|
48 | /**
|
---|
49 | * Discouraged synonym for {@link #FALSE_VALUE}
|
---|
50 | */
|
---|
51 | public static final String falseval = FALSE_VALUE;
|
---|
52 | /**
|
---|
53 | * Discouraged synonym for {@link #REVERSE_VALUE}
|
---|
54 | */
|
---|
55 | public static final String reverseval = REVERSE_VALUE;
|
---|
56 |
|
---|
57 | private OsmUtils() {
|
---|
58 | // Hide default constructor for utils classes
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Converts a string to a boolean value
|
---|
63 | * @param value The string to convert
|
---|
64 | * @return {@link Boolean#TRUE} if that string represents a true value,
|
---|
65 | * {@link Boolean#FALSE} if it represents a false value,
|
---|
66 | * <code>null</code> otherwise.
|
---|
67 | */
|
---|
68 | public static Boolean getOsmBoolean(String value) {
|
---|
69 | if (value == null) return null;
|
---|
70 | String lowerValue = value.toLowerCase(Locale.ENGLISH);
|
---|
71 | if (TRUE_VALUES.contains(lowerValue)) return Boolean.TRUE;
|
---|
72 | if (FALSE_VALUES.contains(lowerValue)) return Boolean.FALSE;
|
---|
73 | return null;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Normalizes the OSM boolean value
|
---|
78 | * @param value The tag value
|
---|
79 | * @return The best true/false value or the old value if the input cannot be converted.
|
---|
80 | * @see #TRUE_VALUE
|
---|
81 | * @see #FALSE_VALUE
|
---|
82 | */
|
---|
83 | public static String getNamedOsmBoolean(String value) {
|
---|
84 | Boolean res = getOsmBoolean(value);
|
---|
85 | return res == null ? value : (res ? trueval : falseval);
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Check if the value is a value indicating that a property applies reversed.
|
---|
90 | * @param value The value to check
|
---|
91 | * @return true if it is reversed.
|
---|
92 | */
|
---|
93 | public static boolean isReversed(String value) {
|
---|
94 | return REVERSE_VALUES.contains(value);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Check if a tag value represents a boolean true value
|
---|
99 | * @param value The value to check
|
---|
100 | * @return true if it is a true value.
|
---|
101 | */
|
---|
102 | public static boolean isTrue(String value) {
|
---|
103 | return TRUE_VALUES.contains(value);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Check if a tag value represents a boolean false value
|
---|
108 | * @param value The value to check
|
---|
109 | * @return true if it is a true value.
|
---|
110 | */
|
---|
111 | public static boolean isFalse(String value) {
|
---|
112 | return FALSE_VALUES.contains(value);
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Creates a new OSM primitive according to the given assertion. Originally written for unit tests,
|
---|
117 | * this can also be used in another places like validation of local MapCSS validator rules.
|
---|
118 | * @param assertion The assertion describing OSM primitive (ex: "way name=Foo railway=rail")
|
---|
119 | * @return a new OSM primitive according to the given assertion
|
---|
120 | * @throws IllegalArgumentException if assertion is null or if the primitive type cannot be deduced from it
|
---|
121 | * @since 7356
|
---|
122 | */
|
---|
123 | public static OsmPrimitive createPrimitive(String assertion) {
|
---|
124 | CheckParameterUtil.ensureParameterNotNull(assertion, "assertion");
|
---|
125 | final String[] x = assertion.split("\\s+", 2);
|
---|
126 | final OsmPrimitive p = "n".equals(x[0]) || "node".equals(x[0])
|
---|
127 | ? new Node(LatLon.ZERO)
|
---|
128 | : "w".equals(x[0]) || "way".equals(x[0]) || /*for MapCSS related usage*/ "area".equals(x[0])
|
---|
129 | ? new Way()
|
---|
130 | : "r".equals(x[0]) || "relation".equals(x[0])
|
---|
131 | ? new Relation()
|
---|
132 | : null;
|
---|
133 | if (p == null) {
|
---|
134 | throw new IllegalArgumentException("Expecting n/node/w/way/r/relation/area, but got '" + x[0] + '\'');
|
---|
135 | }
|
---|
136 | if (x.length > 1) {
|
---|
137 | for (final Map.Entry<String, String> i : TextTagParser.readTagsFromText(x[1]).entrySet()) {
|
---|
138 | p.put(i.getKey(), i.getValue());
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return p;
|
---|
142 | }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Returns the layer value of primitive (null for layer 0).
|
---|
146 | * @param w OSM primitive
|
---|
147 | * @return the value of "layer" key, or null if absent or set to 0 (default value)
|
---|
148 | * @since 12986
|
---|
149 | * @since 13637 (signature)
|
---|
150 | */
|
---|
151 | public static String getLayer(IPrimitive w) {
|
---|
152 | String layer1 = w.get("layer");
|
---|
153 | if ("0".equals(layer1)) {
|
---|
154 | layer1 = null; // 0 is default value for layer.
|
---|
155 | }
|
---|
156 | return layer1;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Determines if the given collection contains primitives, and that none of them belong to a locked layer.
|
---|
161 | * @param collection collection of OSM primitives
|
---|
162 | * @return {@code true} if the given collection is not empty and does not contain any primitive in a locked layer.
|
---|
163 | * @since 13611
|
---|
164 | */
|
---|
165 | public static boolean isOsmCollectionEditable(Collection<? extends OsmPrimitive> collection) {
|
---|
166 | return collection != null && !collection.isEmpty()
|
---|
167 | && collection.stream().map(OsmPrimitive::getDataSet).filter(Objects::nonNull).noneMatch(DataSet::isLocked);
|
---|
168 | }
|
---|
169 | }
|
---|