001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.osm; 003 004import java.util.Arrays; 005import java.util.HashSet; 006import java.util.Locale; 007import java.util.Map; 008import java.util.Set; 009 010import org.openstreetmap.josm.data.coor.LatLon; 011import org.openstreetmap.josm.tools.CheckParameterUtil; 012import org.openstreetmap.josm.tools.TextTagParser; 013 014public final class OsmUtils { 015 016 private static final Set<String> TRUE_VALUES = new HashSet<>(Arrays 017 .asList("true", "yes", "1", "on")); 018 private static final Set<String> FALSE_VALUES = new HashSet<>(Arrays 019 .asList("false", "no", "0", "off")); 020 private static final Set<String> REVERSE_VALUES = new HashSet<>(Arrays 021 .asList("reverse", "-1")); 022 023 public static final String trueval = "yes"; 024 public static final String falseval = "no"; 025 public static final String reverseval = "-1"; 026 027 private OsmUtils() { 028 // Hide default constructor for utils classes 029 } 030 031 public static Boolean getOsmBoolean(String value) { 032 if (value == null) return null; 033 String lowerValue = value.toLowerCase(Locale.ENGLISH); 034 if (TRUE_VALUES.contains(lowerValue)) return Boolean.TRUE; 035 if (FALSE_VALUES.contains(lowerValue)) return Boolean.FALSE; 036 return null; 037 } 038 039 public static String getNamedOsmBoolean(String value) { 040 Boolean res = getOsmBoolean(value); 041 return res == null ? value : (res ? trueval : falseval); 042 } 043 044 public static boolean isReversed(String value) { 045 return REVERSE_VALUES.contains(value); 046 } 047 048 public static boolean isTrue(String value) { 049 return TRUE_VALUES.contains(value); 050 } 051 052 public static boolean isFalse(String value) { 053 return FALSE_VALUES.contains(value); 054 } 055 056 /** 057 * Creates a new OSM primitive according to the given assertion. Originally written for unit tests, 058 * this can also be used in another places like validation of local MapCSS validator rules. 059 * @param assertion The assertion describing OSM primitive (ex: "way name=Foo railway=rail") 060 * @return a new OSM primitive according to the given assertion 061 * @throws IllegalArgumentException if assertion is null or if the primitive type cannot be deduced from it 062 * @since 7356 063 */ 064 public static OsmPrimitive createPrimitive(String assertion) { 065 CheckParameterUtil.ensureParameterNotNull(assertion, "assertion"); 066 final String[] x = assertion.split("\\s+", 2); 067 final OsmPrimitive p = "n".equals(x[0]) || "node".equals(x[0]) 068 ? new Node(LatLon.ZERO) 069 : "w".equals(x[0]) || "way".equals(x[0]) || /*for MapCSS related usage*/ "area".equals(x[0]) 070 ? new Way() 071 : "r".equals(x[0]) || "relation".equals(x[0]) 072 ? new Relation() 073 : null; 074 if (p == null) { 075 throw new IllegalArgumentException("Expecting n/node/w/way/r/relation/area, but got '" + x[0] + '\''); 076 } 077 for (final Map.Entry<String, String> i : TextTagParser.readTagsFromText(x[1]).entrySet()) { 078 p.put(i.getKey(), i.getValue()); 079 } 080 return p; 081 } 082}