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