From 229653af3017e32141dcbe0a53abe6573b0c2df9 Mon Sep 17 00:00:00 2001 From: Georg Lukas Date: Tue, 27 Nov 2018 18:27:10 +0100 Subject: [PATCH 1/6] ParserUtils: fix boolean parser How could this even happen? Signed-off-by: Georg Lukas --- .../src/main/java/org/jivesoftware/smack/util/ParserUtils.java | 2 +- 1 file changed, 1 insertion(+), 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 28440bdbd..5c516f51a 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 @@ -141,7 +141,7 @@ public class ParserUtils { if (valueString == null) return null; valueString = valueString.toLowerCase(Locale.US); - return valueString.equals("true") || valueString.equals("0"); + return valueString.equals("true") || valueString.equals("1"); } public static boolean getBooleanAttribute(XmlPullParser parser, String name, From 8b88f9cb20b86f302c91684d8cd73823599b703d Mon Sep 17 00:00:00 2001 From: Georg Lukas Date: Tue, 27 Nov 2018 18:33:39 +0100 Subject: [PATCH 2/6] Bookmarks: use proper boolean parser for `autojoin` Some clients (read: Gajim) store boolean values as `0` and `1` instead of `false` and `true`, which is legal for the XML boolean type. Signed-off-by: Georg Lukas --- .../java/org/jivesoftware/smackx/bookmarks/Bookmarks.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bookmarks/Bookmarks.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bookmarks/Bookmarks.java index 322d0c8bb..f89e40f49 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bookmarks/Bookmarks.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bookmarks/Bookmarks.java @@ -269,12 +269,12 @@ public class Bookmarks implements PrivateData { private static BookmarkedConference getConferenceStorage(XmlPullParser parser) throws XmlPullParserException, IOException { String name = parser.getAttributeValue("", "name"); - String autojoin = parser.getAttributeValue("", "autojoin"); + boolean autojoin = ParserUtils.getBooleanAttribute(parser, "autojoin", false); EntityBareJid jid = ParserUtils.getBareJidAttribute(parser); BookmarkedConference conf = new BookmarkedConference(jid); conf.setName(name); - conf.setAutoJoin(Boolean.valueOf(autojoin)); + conf.setAutoJoin(autojoin); // Check for nickname boolean done = false; From b8bd10b0562810b8abc14a92c8aa8cb01abc9dc7 Mon Sep 17 00:00:00 2001 From: Georg Lukas Date: Tue, 27 Nov 2018 18:33:39 +0100 Subject: [PATCH 3/6] RoomInfo: use proper boolean parser for `muc#roominfo_subjectmod` XML allows both false/true and 0/1 syntax for booleans. Signed-off-by: Georg Lukas --- .../src/main/java/org/jivesoftware/smackx/muc/RoomInfo.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/RoomInfo.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/RoomInfo.java index b31f5bb4f..421f957f4 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/RoomInfo.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/RoomInfo.java @@ -207,7 +207,8 @@ public class RoomInfo { FormField subjectmodField = form.getField("muc#roominfo_subjectmod"); if (subjectmodField != null && !subjectmodField.getValues().isEmpty()) { - subjectmod = Boolean.valueOf(subjectmodField.getFirstValue()); + String firstValue = subjectmodField.getFirstValue(); + subjectmod = ("true".equals(firstValue) || "1".equals(firstValue)); } FormField urlField = form.getField("muc#roominfo_logs"); From 7ea7f9e2e9c8ff35e3bcc08d7f257386fac862d4 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 29 Nov 2018 08:55:10 +0100 Subject: [PATCH 4/6] 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, From dbfc123e5e05d9c8f80dd907f02faa1e54ae116a Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Thu, 29 Nov 2018 22:29:21 +0100 Subject: [PATCH 5/6] Use ParserUtils.parseXmlBoolean() where appropriate --- .../smackx/hoxt/provider/HttpOverXmppReqProvider.java | 8 +++++--- .../smackx/iot/control/element/SetBoolData.java | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/hoxt/provider/HttpOverXmppReqProvider.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/hoxt/provider/HttpOverXmppReqProvider.java index 0a69ab4bd..6edf5ca0c 100644 --- a/smack-experimental/src/main/java/org/jivesoftware/smackx/hoxt/provider/HttpOverXmppReqProvider.java +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/hoxt/provider/HttpOverXmppReqProvider.java @@ -16,6 +16,8 @@ */ package org.jivesoftware.smackx.hoxt.provider; +import org.jivesoftware.smack.util.ParserUtils; + import org.jivesoftware.smackx.hoxt.packet.HttpMethod; import org.jivesoftware.smackx.hoxt.packet.HttpOverXmppReq; @@ -47,13 +49,13 @@ public class HttpOverXmppReqProvider extends AbstractHttpOverXmppProvider Date: Thu, 29 Nov 2018 22:38:11 +0100 Subject: [PATCH 6/6] Add checkstyle rule for "Boolean.valueOf()" usages --- config/checkstyle.xml | 5 +++++ .../jivesoftware/smack/provider/IntrospectionProvider.java | 2 ++ .../provider/JivePropertiesExtensionProvider.java | 2 ++ .../smackx/workgroup/settings/WorkgroupProperties.java | 2 ++ 4 files changed, 11 insertions(+) diff --git a/config/checkstyle.xml b/config/checkstyle.xml index a05c12726..041e31ffb 100644 --- a/config/checkstyle.xml +++ b/config/checkstyle.xml @@ -89,6 +89,11 @@ + + + + + diff --git a/smack-core/src/main/java/org/jivesoftware/smack/provider/IntrospectionProvider.java b/smack-core/src/main/java/org/jivesoftware/smack/provider/IntrospectionProvider.java index 981b437a5..35dde99b4 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/provider/IntrospectionProvider.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/provider/IntrospectionProvider.java @@ -123,7 +123,9 @@ public class IntrospectionProvider{ case "java.lang.String": return value; case "boolean": + // CHECKSTYLE:OFF return Boolean.valueOf(value); + // CHECKSTYLE:ON case "int": return Integer.valueOf(value); case "long": diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/jiveproperties/provider/JivePropertiesExtensionProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/jiveproperties/provider/JivePropertiesExtensionProvider.java index b5719be65..178e4ad85 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/jiveproperties/provider/JivePropertiesExtensionProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/jiveproperties/provider/JivePropertiesExtensionProvider.java @@ -92,7 +92,9 @@ public class JivePropertiesExtensionProvider extends ExtensionElementProvider