From 7ea7f9e2e9c8ff35e3bcc08d7f257386fac862d4 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 29 Nov 2018 08:55:10 +0100 Subject: [PATCH] Add ParserUtils.parseXmlBoolean(String) --- .../jivesoftware/smack/util/ParserUtils.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java index 5c516f51a..90f2b543c 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/ParserUtils.java @@ -129,6 +129,27 @@ public class ParserUtils { return Resourcepart.from(resourcepartString); } + /** + * Prase a string to a boolean value as per "xs:boolean". Valid input strings are "true", "1" for true, and "false", "0" for false. + * + * @param booleanString the input string. + * @return the boolean representation of the input string + * @throws IllegalArgumentException if the input string is not valid. + * @since 4.3.2 + */ + public static boolean parseXmlBoolean(String booleanString) { + switch (booleanString) { + case "true": + case "1": + return true; + case "false": + case "0": + return false; + default: + throw new IllegalArgumentException(booleanString + " is not a valid boolean string"); + } + } + /** * Get the boolean value of an argument. * @@ -141,7 +162,7 @@ public class ParserUtils { if (valueString == null) return null; valueString = valueString.toLowerCase(Locale.US); - return valueString.equals("true") || valueString.equals("1"); + return parseXmlBoolean(valueString); } public static boolean getBooleanAttribute(XmlPullParser parser, String name,