();
@@ -142,6 +148,7 @@ public class FormField {
* name.
*/
public FormField() {
+ this(null);
this.type = Type.fixed;
}
@@ -214,7 +221,12 @@ public class FormField {
/**
* Returns the variable name that the question is filling out.
- *
+ *
+ * According to XEP-4 § 3.2 the variable name (the 'var' attribute)
+ * "uniquely identifies the field in the context of the form" (if the field is not of type 'fixed', in which case
+ * the field "MAY possess a 'var' attribute")
+ *
+ *
* @return the variable name of the question.
*/
public String getVariable() {
@@ -270,11 +282,19 @@ public class FormField {
/**
* Sets an indicative of the format for the data to answer.
+ *
+ * This method will throw an IllegalArgumentException if type is 'fixed'. To create FormFields of type 'fixed' use
+ * {@link #FormField()} instead.
+ *
*
* @param type an indicative of the format for the data to answer.
* @see Type
+ * @throws IllegalArgumentException if type is 'fixed'.
*/
public void setType(Type type) {
+ if (type == Type.fixed) {
+ throw new IllegalArgumentException("Can not set type to fixed, use FormField constructor without arguments instead.");
+ }
this.type = type;
}
diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java
index effdc03bf..ecba14505 100644
--- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java
+++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java
@@ -125,6 +125,24 @@ public class DataForm implements PacketExtension {
}
}
+ /**
+ * Return the form field with the given variable name or null.
+ *
+ * @param variableName
+ * @return the form field or null.
+ * @since 4.1
+ */
+ public FormField getField(String variableName) {
+ synchronized (fields) {
+ for (FormField field : fields) {
+ if (variableName.equals(field.getVariable())) {
+ return field;
+ }
+ }
+ }
+ return null;
+ }
+
public String getElementName() {
return ELEMENT;
}
@@ -169,6 +187,11 @@ public class DataForm implements PacketExtension {
* @param field the field to add to the form.
*/
public void addField(FormField field) {
+ String fieldVariableName = field.getVariable();
+ if (fieldVariableName != null && getField(fieldVariableName) != null) {
+ throw new IllegalArgumentException("This data form already contains a form field with the variable name '"
+ + fieldVariableName + "'");
+ }
synchronized (fields) {
fields.add(field);
}
@@ -206,6 +229,20 @@ public class DataForm implements PacketExtension {
return Collections.unmodifiableList(extensionElements);
}
+ /**
+ * Returns the hidden FORM_TYPE field or null if this data form has none.
+ *
+ * @return the hidden FORM_TYPE field or null.
+ * @since 4.1
+ */
+ public FormField getHiddenFormTypeField() {
+ FormField field = getField(FormField.FORM_TYPE);
+ if (field != null && field.getType() == FormField.Type.hidden) {
+ return field;
+ }
+ return null;
+ }
+
/**
* Returns true if this DataForm has at least one FORM_TYPE field which is
* hidden. This method is used for sanity checks.
@@ -213,12 +250,7 @@ public class DataForm implements PacketExtension {
* @return true if there is at least one field which is hidden.
*/
public boolean hasHiddenFormTypeField() {
- for (FormField f : fields) {
- if (f.getVariable().equals("FORM_TYPE") && f.getType() == FormField.Type.hidden) {
- return true;
- }
- }
- return false;
+ return getHiddenFormTypeField() != null;
}
@Override