From b08dbc1dbc8410554e56a1fbb66d5e6e5af3dfe5 Mon Sep 17 00:00:00 2001 From: Anno van Vliet Date: Sat, 22 Nov 2014 23:16:45 +0100 Subject: [PATCH] Support for XEP-0122: Data Forms Validation. Data Forms Validation are a part of Data Fields and implemented as extensions, added to a Datafield. Data validation extensions are validated before adding to the message, using the consistency rules as described in the XEP. Fixes SMACK-621. Minor modifications done by Florian Schmaus --- documentation/extensions/index.md | 1 + .../smack/util/XmlStringBuilder.java | 4 +- .../jivesoftware/smackx/xdata/FormField.java | 22 +- .../xdata/provider/DataFormProvider.java | 9 + .../ValidationConsistencyException.java | 40 ++ .../XDataValidationManager.java | 37 ++ .../packet/ValidateElement.java | 436 ++++++++++++++++++ .../provider/DataValidationProvider.java | 95 ++++ .../org.jivesoftware.smackx/extensions.xml | 1 + .../smackx/xdata/packet/DataFormTest.java | 67 +-- .../xdatalayout/packet/DataLayoutTest.java | 37 +- .../DataValidationHelperTest.java | 120 +++++ .../provider/DataValidationTest.java | 140 ++++++ 13 files changed, 943 insertions(+), 66 deletions(-) create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/xdatavalidation/ValidationConsistencyException.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/xdatavalidation/XDataValidationManager.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/xdatavalidation/packet/ValidateElement.java create mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/xdatavalidation/provider/DataValidationProvider.java create mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/xdatavalidation/DataValidationHelperTest.java create mode 100644 smack-extensions/src/test/java/org/jivesoftware/smackx/xdatavalidation/provider/DataValidationTest.java diff --git a/documentation/extensions/index.md b/documentation/extensions/index.md index 9e29b5bae..005eddb60 100644 --- a/documentation/extensions/index.md +++ b/documentation/extensions/index.md @@ -48,6 +48,7 @@ Smack Extensions and currently supported XEPs by Smack (smack-extensions) | Stream Initation | [XEP-0095](http://xmpp.org/extensions/xep-0095.html) | Initiating a data stream between any two XMPP entities. | | [SI File Transfer](filetransfer.html) | [XEP-0096](http://xmpp.org/extensions/xep-0096.html) | Transfer files between two users over XMPP. | | [Entity Capabilities](caps.html) | [XEP-0115](http://xmpp.org/extensions/xep-0115.html) | Broadcasting and dynamic discovery of entity capabilities. | +| Data Forms Validation | [XEP-0122](http://xmpp.org/extensions/xep-0122.html) | Enables an application to specify additional validation guidelines . | | Stream Compression | [XEP-0138](http://xmpp.org/extensions/xep-0138.html) | Support for optional compression of the XMPP stream. | Data Forms Layout | [XEP-0141](http://xmpp.org/extensions/xep-0141.html) | Enables an application to specify form layouts. | | Personal Eventing Protocol | [XEP-0163](http://xmpp.org/extensions/xep-0163.html) | Using the XMPP publish-subscribe protocol to broadcast state change events associated with a XMPP account. | diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java index 708beb1ca..55c07e07a 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java @@ -206,14 +206,14 @@ public class XmlStringBuilder implements Appendable, CharSequence { } /** - * Add the given attribute if value => 0 + * Add the given attribute if value not null and value => 0. * * @param name * @param value * @return a reference to this object */ public XmlStringBuilder optLongAttribute(String name, Long value) { - if (value >= 0) { + if (value != null && value >= 0) { attribute(name, Long.toString(value)); } return this; diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/FormField.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/FormField.java index 6ba006cde..924a8a573 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/FormField.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/FormField.java @@ -17,12 +17,13 @@ package org.jivesoftware.smackx.xdata; -import org.jivesoftware.smack.util.XmlStringBuilder; - import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.jivesoftware.smack.util.XmlStringBuilder; +import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement; + /** * Represents a field of a form. The field could be used to represent a question to complete, * a completed question or a data returned from a search. The exact interpretation of the field @@ -124,6 +125,7 @@ public class FormField { private Type type; private final List