mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-12-14 06:51:08 +01:00
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 <flo@geekplace.eu>
This commit is contained in:
parent
019b9dc5d4
commit
b08dbc1dbc
13 changed files with 943 additions and 66 deletions
|
|
@ -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<Option> options = new ArrayList<Option>();
|
||||
private final List<String> values = new ArrayList<String>();
|
||||
private ValidateElement validateElement;
|
||||
|
||||
/**
|
||||
* Creates a new FormField with the variable name that uniquely identifies the field
|
||||
|
|
@ -219,6 +221,13 @@ public class FormField {
|
|||
return variable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the validateElement
|
||||
*/
|
||||
public ValidateElement getValidateElement() {
|
||||
return validateElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a description that provides extra clarification about the question. This information
|
||||
* could be presented to the user either in tool-tip, help button, or as a section of text
|
||||
|
|
@ -251,6 +260,14 @@ public class FormField {
|
|||
this.required = required;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param validateElement the validateElement to set
|
||||
*/
|
||||
public void setValidateElement(ValidateElement validateElement) {
|
||||
validateElement.checkConsistency(this);
|
||||
this.validateElement = validateElement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an indicative of the format for the data to answer.
|
||||
*
|
||||
|
|
@ -325,6 +342,7 @@ public class FormField {
|
|||
for (Option option : getOptions()) {
|
||||
buf.append(option.toXML());
|
||||
}
|
||||
buf.optElement(validateElement);
|
||||
buf.closeElement(ELEMENT);
|
||||
return buf;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,8 @@ import org.jivesoftware.smackx.xdata.FormField;
|
|||
import org.jivesoftware.smackx.xdata.packet.DataForm;
|
||||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout;
|
||||
import org.jivesoftware.smackx.xdatalayout.provider.DataLayoutProvider;
|
||||
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement;
|
||||
import org.jivesoftware.smackx.xdatavalidation.provider.DataValidationProvider;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
|
|
@ -102,6 +104,7 @@ public class DataFormProvider extends PacketExtensionProvider<DataForm> {
|
|||
switch (eventType) {
|
||||
case XmlPullParser.START_TAG:
|
||||
String name = parser.getName();
|
||||
String namespace = parser.getNamespace();
|
||||
switch (name) {
|
||||
case "desc":
|
||||
formField.setDescription(parser.nextText());
|
||||
|
|
@ -115,6 +118,12 @@ public class DataFormProvider extends PacketExtensionProvider<DataForm> {
|
|||
case "option":
|
||||
formField.addOption(parseOption(parser));
|
||||
break;
|
||||
// See XEP-122 Data Forms Validation
|
||||
case ValidateElement.ELEMENT:
|
||||
if (namespace.equals(ValidateElement.NAMESPACE)) {
|
||||
formField.setValidateElement(DataValidationProvider.parse(parser));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case XmlPullParser.END_TAG:
|
||||
if (parser.getDepth() == initialDepth) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue