summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/isode/stroke/elements/FormField.java')
-rw-r--r--src/com/isode/stroke/elements/FormField.java704
1 files changed, 219 insertions, 485 deletions
diff --git a/src/com/isode/stroke/elements/FormField.java b/src/com/isode/stroke/elements/FormField.java
index aedc8fe..2ec0e4b 100644
--- a/src/com/isode/stroke/elements/FormField.java
+++ b/src/com/isode/stroke/elements/FormField.java
@@ -1,18 +1,16 @@
/*
- * Copyright (c) 2012 Isode Limited, London, England.
+ * Copyright (c) 2012-2014 Isode Limited, London, England.
* All rights reserved.
*/
/*
* Copyright (c) 2010 Remko Tronçon
* All rights reserved.
*/
-
package com.isode.stroke.elements;
import java.util.ArrayList;
import java.util.List;
-import com.isode.stroke.elements.Form.Type;
import com.isode.stroke.jid.JID;
/**
@@ -28,120 +26,88 @@ import com.isode.stroke.jid.JID;
* optionally providing a textual explanation.
*/
public class FormField {
- /**
- * Attribute "var"
- */
+
public static final String FORM_FIELD_ATTRIBUTE_VAR = "var";
-
- /**
- * Attribute "label"
- */
public static final String FORM_FIELD_ATTRIBUTE_LABEL = "label";
-
- /**
- * Element "required"
- */
public static final String FORM_FIELD_ELEMENT_REQUIRED = "required";
-
- /**
- * Element "desc"
- */
public static final String FORM_FIELD_ELEMENT_DESC = "desc";
-
- /**
- * Element "value"
- */
public static final String FORM_FIELD_ELEMENT_VALUE = "value";
-
- /**
- * Attribute "type"
- */
public static final String FORM_FIELD_ATTRIBUTE_TYPE = "type";
-
- /**
- * Element "option"
- */
public static final String FORM_FIELD_ELEMENT_OPTION = "option";
-
- /**
- * Attribute option "label"
- */
public static final String FORM_FIELD_ATTRIBUTE_OPTION_LABEL = "label";
-
- /**
- * Element option "value"
- */
public static final String FORM_FIELD_ELEMENT_OPTION_VALUE = "value";
-
- /**
- * Type "boolean"
- */
- public static final String FORM_FIELD_TYPE_BOOLEAN = "boolean";
-
- /**
- * Type "fixed"
- */
- public static final String FORM_FIELD_TYPE_FIXED = "fixed";
-
- /**
- * Type "hidden"
- */
- public static final String FORM_FIELD_TYPE_HIDDEN = "hidden";
-
- /**
- * Type "list-single"
- */
- public static final String FORM_FIELD_TYPE_LIST_SINGLE = "list-single";
-
- /**
- * Type "text-private"
- */
- public static final String FORM_FIELD_TYPE_TEXT_PRIVATE = "text-private";
-
- /**
- * Type "text-single"
- */
- public static final String FORM_FIELD_TYPE_TEXT_SINGLE = "text-single";
-
- /**
- * Type "jid-multi"
- */
- public static final String FORM_FIELD_TYPE_JID_MULTI = "jid-multi";
-
- /**
- * Type "jid-single"
- */
- public static final String FORM_FIELD_TYPE_JID_SINGLE = "jid-single";
-
- /**
- * Type "list-multi"
- */
- public static final String FORM_FIELD_TYPE_LIST_MULTI = "list-multi";
-
- /**
- * Type "text-multi"
- */
- public static final String FORM_FIELD_TYPE_TEXT_MULTI = "text-multi";
-
+ private static final String ILLEGAL_ARG_EX_STR = "This API is not valid for getting a value of type ";
+
+ private boolean required_;
+ private List<Option> options_ = new ArrayList<Option>();
+ private List<String> values_ = new ArrayList<String>();
+ private String description_ = "";
+ private String label_ = "";
+ private String name_ = "";
+ private Type type_;
+
+ public enum Type {
+ UNKNOWN_TYPE("unknown"),
+ BOOLEAN_TYPE("boolean"),
+ FIXED_TYPE("fixed"),
+ HIDDEN_TYPE("hidden"),
+ LIST_SINGLE_TYPE("list-single"),
+ LIST_MULTI_TYPE("list-multi"),
+ TEXT_PRIVATE_TYPE("text-private"),
+ TEXT_MULTI_TYPE("text-multi"),
+ TEXT_SINGLE_TYPE("text-single"),
+ JID_MULTI_TYPE("jid-multi"),
+ JID_SINGLE_TYPE("jid-single");
+ private String description_;
+ private Type(String description) {
+ description_ = description;
+ }
+ public String getDescription() {
+ return description_;
+ }
+
+ public static Type getTypeFromString(String string) {
+ for (Type type : Type.values()) {
+ if (type.getDescription().equals(string)) {
+ return type;
+ }
+ }
+ return Type.UNKNOWN_TYPE;
+ }
+ }
+
+ public FormField(Type type) {
+ type_ = type;
+ required_ = false;
+ if (type == Type.BOOLEAN_TYPE) {
+ setBoolValue(false);
+ }
+ }
+
+ public FormField(Type type, String value) {
+ addValue(value);
+ type_ = type;
+ required_ = false;
+ }
+
/**
* This class defines the option element that can be used in
- * {@link ListSingleFormField} and {@link ListMultiFormField}. This class is
+ * ListSingleFormField and ListMultiFormField. This class is
* immutable.
*/
public static class Option {
/**
* Human-readable name for the option, will not be null
*/
- public final String label;
+ public final String label_;
/**
* Option value, will not be null
*/
- public final String value;
+ public final String value_;
/**
* Create an option element.
- *
* @param label Human-readable name for the option, must not be null
* @param value Option value, must not be null
*/
@@ -152,46 +118,31 @@ public class FormField {
if (value == null) {
throw new NullPointerException("'value' must not be null");
}
-
- this.label = label;
- this.value = value;
+ label_ = label;
+ value_ = value;
}
}
- private String name = "";
- private String label = "";
- private String description = "";
- private boolean required;
- private List<Option> options = new ArrayList<Option>();
- private List<String> rawValues = new ArrayList<String>();
-
- protected FormField() {
- required = false;
- }
-
/**
* Set the unique identifier for the field in the form.
- *
* @param name unique identifier for the field in the form, must not be null
*/
public void setName(String name) {
if (name == null) {
throw new NullPointerException("'name' must not be null");
}
-
- this.name = name;
+ name_ = name;
}
/**
* @return unique identifier for the field, will never be null
*/
public String getName() {
- return name;
+ return name_;
}
/**
* Set the human-readable name for the field.
- *
* @param label human-readable name for the field, must not be null
*/
public void setLabel(String label) {
@@ -199,19 +150,18 @@ public class FormField {
throw new NullPointerException("'label' must not be null");
}
- this.label = label;
+ label_ = label;
}
/**
* @return human-readable name for the field, will never be null
*/
public String getLabel() {
- return label;
+ return label_;
}
/**
* Set the natural-language description for the field.
- *
* @param description natural-language description for the field, must not
* be null
*/
@@ -220,464 +170,248 @@ public class FormField {
throw new NullPointerException("'description' must not be null");
}
- this.description = description;
+ description_ = description;
}
/**
* @return natural-language description for the field, will never be null
*/
public String getDescription() {
- return description;
+ return description_;
}
/**
* Set if the field is required for the form to be considered valid.
- *
* @param required true if the field is required for the form to be
* considered valid
*/
public void setRequired(boolean required) {
- this.required = required;
+ required_ = required;
}
/**
* @return true if the field is required for the form to be considered valid
*/
public boolean getRequired() {
- return required;
+ return required_;
}
/**
- * Add to the list of options for this form field.
- *
+ * Add to the list of options_ for this FormField.
* @param option Option to add, must not be null
*/
public void addOption(Option option) {
if (option == null) {
throw new NullPointerException("'option' must not be null");
}
-
- options.add(option);
+ options_.add(option);
}
/**
- * @return List of options for this form, will never be null. The instance
+ * @return List of options_ for this FormField, will never be null. The instance
* of the list stored in the object is not returned, a copy is made.
*/
public List<Option> getOptions() {
- return new ArrayList<Option>(options);
+ return new ArrayList<Option>(options_);
+ }
+
+ /**
+ * Clears the list of options_ for the FormField.
+ */
+ public void clearOptions() {
+ options_.clear();
}
/**
- * Add to values for this field. The values can be the defaults suggested in
- * a form of {@link Type#FORM_TYPE} or results provided in a form of
- * {@link Type#RESULT_TYPE} or values submitted in a form of
+ * Add to values_ for this field. The values_ can be the defaults suggested in
+ * a form of {@link Form.Type#FORM_TYPE} or results provided in a form of
+ * {@link Type#RESULT_TYPE} or values_ submitted in a form of
* {@link Type#SUBMIT_TYPE}.
- *
* @param value Value to add, must not be null
*/
- public void addRawValue(String value) {
+ public void addValue(String value) {
if (value == null) {
throw new NullPointerException("'value' must not be null");
}
-
- rawValues.add(value);
+ values_.add(value);
}
-
+
+ /**
+ * Creates a single value for the FormField. This resets the values_ for this
+ * field to a single argument.
+ * @param value String, should not be null
+ */
+ public void setValue(String value) {
+ if (value == null ) {
+ throw new NullPointerException("'value' should not be null");
+ }
+ values_.clear();
+ values_.add(value);
+ }
+
/**
- * @return List of values for this field, will never be null. The instance
- * of the list stored in the object is not returned, a copy is made.
+ * Resets the list of values for this field to the specified argument. The
+ * instance of the list stored in the object is not returned, a copy is made.
+ * @param values List<String> of values
*/
- public List<String> getRawValues() {
- return new ArrayList<String>(rawValues);
+ public void setValues(List<String> values) {
+ if (values == null) {
+ throw new NullPointerException("'values' must not be null");
+ }
+ values_ = new ArrayList<String>(values);
}
/**
- * Template for creating a form field.
- *
- * @param <T> Type of form field.
+ * @return List of values_ for this field, will never be null. The instance
+ * of the list stored in the object is not returned, a copy is made.
*/
- public static class GenericFormField<T> extends FormField {
- private T value;
-
- /**
- * @return Values for this field. The values can be the defaults
- * suggested in a form of {@link Type#FORM_TYPE} or results
- * provided in a form of {@link Type#RESULT_TYPE} or values
- * submitted in a form of {@link Type#SUBMIT_TYPE}. Will never
- * be null.
- */
- public final T getValue() {
- return value;
- }
-
- /**
- * Set values for this field. The values can be the defaults suggested
- * in a form of {@link Type#FORM_TYPE} or results provided in a form of
- * {@link Type#RESULT_TYPE} or values submitted in a form of
- * {@link Type#SUBMIT_TYPE}.
- *
- * @param value Value to set, must not be null
- */
- public void setValue(final T value) {
- if (value == null) {
- throw new NullPointerException("'value' must not be null");
- }
-
- this.value = value;
- }
-
- protected GenericFormField(final T value) {
- setValue(value);
- }
+ public List<String> getValues() {
+ if (values_ == null) {
+ values_ = new ArrayList<String>();
+ }
+ return new ArrayList<String>(values_);
}
/**
- * This field enables an entity to gather or provide an either-or choice
- * between two options. The default value is "false".
+ * Returns the type of the FormField.
+ * @return type_ Type, never null
*/
- public static class BooleanFormField extends GenericFormField<Boolean> {
- private BooleanFormField(Boolean value) {
- super(value);
- }
-
- /**
- * Create an object with given value.
- *
- * @param value Value for this field, must not be null
- *
- * @return new object, will never be null
- */
- public static BooleanFormField create(final Boolean value) {
- if (value == null) {
- throw new NullPointerException("'value' must not be null");
- }
-
- return new BooleanFormField(value);
- }
-
- /**
- * Create an object with value FALSE.
- *
- * @return new object, will never be null
- */
- public static BooleanFormField create() {
- return new BooleanFormField(Boolean.FALSE);
- }
+ public Type getType() {
+ return type_;
}
-
+
/**
- * This field is intended for data description (e.g., human-readable text
- * such as "section" headers) rather than data gathering or provision.
+ * Sets the type of the FormField.
+ * @param type Type, never null
*/
- public static class FixedFormField extends GenericFormField<String> {
- private FixedFormField(String value) {
- super(value);
- }
-
- /**
- * Create an object with given value.
- *
- * @param value Value for this field, must not be null
- *
- * @return new object, will never be null
- */
- public static FixedFormField create(final String value) {
- if (value == null) {
- throw new NullPointerException("'value' must not be null");
- }
-
- return new FixedFormField(value);
- }
-
- /**
- * Create an object with value as an empty string.
- *
- * @return new object, will never be null
- */
- public static FixedFormField create() {
- return new FixedFormField("");
- }
+ public void setType(Type type) {
+ type_ = type;
}
-
+
/**
- * This field is not shown to the form-submitting entity, but instead is
- * returned with the form.
+ * Returns the value of a FormField with the type boolean.
+ * @return value boolean, will return false if FormField has no values
*/
- public static class HiddenFormField extends GenericFormField<String> {
- private HiddenFormField(String value) {
- super(value);
- }
-
- /**
- * Create an object with given value.
- *
- * @param value Value for this field, must not be null
- *
- * @return new object, will never be null
- */
- public static HiddenFormField create(final String value) {
- if (value == null) {
- throw new NullPointerException("'value' must not be null");
- }
-
- return new HiddenFormField(value);
- }
-
- /**
- * Create an object with value as an empty string.
- *
- * @return new object, will never be null
- */
- public static HiddenFormField create() {
- return new HiddenFormField("");
- }
+ public boolean getBoolValue() {
+ if (type_ != Type.BOOLEAN_TYPE) {
+ throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
+ }
+ return values_.isEmpty() ? false : values_.get(0).equals("true") || values_.get(0).equals("1");
}
-
+
/**
- * This field enables an entity to gather or provide one option from among
- * many.
+ * Sets the value of a FormField with type boolean to a boolean value.
+ * @param bool boolean
*/
- public static class ListSingleFormField extends GenericFormField<String> {
- private ListSingleFormField(String value) {
- super(value);
- }
-
- /**
- * Create an object with given value.
- *
- * @param value Value for this field, must not be null
- *
- * @return new object, will never be null
- */
- public static ListSingleFormField create(final String value) {
- if (value == null) {
- throw new NullPointerException("'value' must not be null");
- }
-
- return new ListSingleFormField(value);
- }
-
- /**
- * Create an object with value as an empty string.
- *
- * @return new object, will never be null
- */
- public static ListSingleFormField create() {
- return new ListSingleFormField("");
- }
+ public void setBoolValue(boolean bool) {
+ values_.clear();
+ values_.add(bool ? "1" : "0");
}
-
+
/**
- * This field enables an entity to gather or provide multiple lines of text
- * (i.e. containing newlines).
+ * Returns a JID single value.
+ * @return JID value, or empty JID is FormField has no values
*/
- public static class TextMultiFormField extends GenericFormField<String> {
- private TextMultiFormField(String value) {
- super(value);
- }
-
- /**
- * Create an object with given value.
- *
- * @param value Value for this field, must not be null
- *
- * @return new object, will never be null
- */
- public static TextMultiFormField create(final String value) {
- if (value == null) {
- throw new NullPointerException("'value' must not be null");
- }
-
- return new TextMultiFormField(value);
- }
-
- /**
- * Create an object with value as an empty string.
- *
- * @return new object, will never be null
- */
- public static TextMultiFormField create() {
- return new TextMultiFormField("");
- }
+ public JID getJIDSingleValue() {
+ if (type_ != Type.JID_SINGLE_TYPE) {
+ throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
+ }
+ return values_.isEmpty() ? new JID() : JID.fromString(values_.get(0));
}
/**
- * This field enables an entity to gather or provide a single line or word
- * of text, which shall be obscured in an interface (e.g., with multiple
- * instances of the asterisk character).
+ * Gets the value at a specified index of a JID-multi FormField.
+ * @param index index of the JID value
+ * @return JID value, or empty JID is FormField has no values
*/
- public static class TextPrivateFormField extends GenericFormField<String> {
- private TextPrivateFormField(String value) {
- super(value);
- }
-
- /**
- * Create an object with given value.
- *
- * @param value Value for this field, must not be null
- *
- * @return new object, will never be null
- */
- public static TextPrivateFormField create(final String value) {
- if (value == null) {
- throw new NullPointerException("'value' must not be null");
- }
-
- return new TextPrivateFormField(value);
- }
-
- /**
- * Create an object with value as an empty string.
- *
- * @return new object, will never be null
- */
- public static TextPrivateFormField create() {
- return new TextPrivateFormField("");
- }
+ public JID getJIDMultiValue(int index) {
+ if (type_ != Type.JID_MULTI_TYPE) {
+ throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
+ }
+ return values_.isEmpty() ? new JID() : JID.fromString(values_.get(index));
}
/**
- * This field enables an entity to gather or provide a single line or word
- * of text, which may be shown in an interface.
+ * Gets the value of a FormField with the type text-private.
+ * @return value String, empty String if FormField has no values
*/
- public static class TextSingleFormField extends GenericFormField<String> {
- private TextSingleFormField(String value) {
- super(value);
- }
-
- /**
- * Create an object with given value.
- *
- * @param value Value for this field, must not be null
- *
- * @return new object, will never be null
- */
- public static TextSingleFormField create(final String value) {
- if (value == null) {
- throw new NullPointerException("'value' must not be null");
- }
-
- return new TextSingleFormField(value);
- }
-
- /**
- * Create an object with value as an empty string.
- *
- * @return new object, will never be null
- */
- public static TextSingleFormField create() {
- return new TextSingleFormField("");
- }
+ public String getTextPrivateValue() {
+ if (type_ != Type.TEXT_PRIVATE_TYPE) {
+ throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
+ }
+ return values_.isEmpty() ? "" : values_.get(0);
}
/**
- * This field enables an entity to gather or provide a single Jabber ID.
+ * Gets the value of a FormField with the type fixed.
+ * @return value String, or empty String if invalid FormField type
*/
- public static class JIDSingleFormField extends GenericFormField<JID> {
- private JIDSingleFormField(JID value) {
- super(value);
- }
-
- /**
- * Create an object with given value.
- *
- * @param value Value for this field, must not be null
- *
- * @return new object, will never be null
- */
- public static JIDSingleFormField create(final JID value) {
- if (value == null) {
- throw new NullPointerException("'value' must not be null");
- }
-
- return new JIDSingleFormField(value);
- }
-
- /**
- * Create an object with value as an invalid JID.
- *
- * @return new object, will never be null
- */
- public static JIDSingleFormField create() {
- return new JIDSingleFormField(new JID());
- }
+ public String getFixedValue() {
+ if (type_ != Type.FIXED_TYPE) {
+ throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
+ }
+ return values_.isEmpty() ? "" : values_.get(0);
}
-
+
/**
- * This field enables an entity to gather or provide multiple Jabber IDs.
+ * Gets the value of a FormField with the type text-single.
+ * If unknown type, extract a string value.
+ * @return value String, or empty String if invalid FormField type
*/
- public static class JIDMultiFormField extends GenericFormField<List<JID>> {
- private JIDMultiFormField(List<JID> value) {
- super(value);
- }
-
- /**
- * Create an object with given value.
- *
- * @param value Value for this field, must not be null. A copy of the
- * given list will be kept by this object.
- *
- * @return new object, will never be null
- */
- public static JIDMultiFormField create(final List<JID> value) {
- if (value == null) {
- throw new NullPointerException("'value' must not be null");
- }
-
- return new JIDMultiFormField(new ArrayList<JID>(value));
- }
-
- /**
- * Create an object with an empty list.
- *
- * @return new object, will never be null
- */
- public static JIDMultiFormField create() {
- return new JIDMultiFormField(new ArrayList<JID>());
- }
+ public String getTextSingleValue() {
+ if (type_ != Type.TEXT_SINGLE_TYPE && type_ != Type.UNKNOWN_TYPE) {
+ throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
+ }
+ return values_.isEmpty() ? "" : values_.get(0);
+ }
+
+ /**
+ * Gets the value of a FormField with the type text-multi.
+ * @return value String
+ */
+ public String getTextMultiValue() {
+ if (type_ != Type.TEXT_MULTI_TYPE) {
+ throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
+ }
+ StringBuilder val = new StringBuilder();
+ for (int i=0; i<values_.size(); i++) {
+ String s = values_.get(i);
+ if (i != 0) {
+ val.append("\n");
+ }
+ val.append(s);
+ }
+ return val.toString();
}
/**
- * This field enables an entity to gather or provide one or more options
- * from among many. The order of items MAY be significant.
- */
- public static class ListMultiFormField extends
- GenericFormField<List<String>> {
- private ListMultiFormField(List<String> value) {
- super(value);
- }
-
- /**
- * Create an object with given value.
- *
- * @param value Value for this field, must not be null. A copy of the
- * given list will be kept by this object.
- *
- * @return new object, will never be null
- */
- public static ListMultiFormField create(final List<String> value) {
- if (value == null) {
- throw new NullPointerException("'value' must not be null");
- }
-
- return new ListMultiFormField(new ArrayList<String>(value));
- }
-
- /**
- * Create an object with an empty list.
- *
- * @return new object, will never be null
- */
- public static ListMultiFormField create() {
- return new ListMultiFormField(new ArrayList<String>());
- }
+ * Sets the value of a FormField with the type text-multi.
+ * @param val String value to set, must not be null
+ */
+ public void setTextMultiValue(String val) {
+ if (type_ != Type.TEXT_MULTI_TYPE) {
+ throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
+ }
+ values_.clear();
+ if (val.contains("\r\n")){
+ for (String s : val.split("\r\n")) {
+ values_.add(s);
+ }
+ }
+ else if (val.contains("\n")){
+ for (String s : val.split("\n")) {
+ values_.add(s);
+ }
+ }
+ else {
+ values_.add(val);
+ }
}
@Override
public String toString() {
- return FormField.class.getSimpleName() + "\nname: " + name
- + "\nlabel: " + label + "\ndescription: " + description
- + "\nrequired: " + required;
+ return FormField.class.getSimpleName() + "\nname: " + name_
+ + "\nlabel: " + label_ + "\ndescription: " + description_
+ + "\nrequired: " + required_;
}
}