summaryrefslogtreecommitdiffstats
path: root/src/com
diff options
context:
space:
mode:
Diffstat (limited to 'src/com')
-rw-r--r--src/com/isode/stroke/elements/FormField.java36
-rw-r--r--src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java71
2 files changed, 26 insertions, 81 deletions
diff --git a/src/com/isode/stroke/elements/FormField.java b/src/com/isode/stroke/elements/FormField.java
index 2ec0e4b..84b6af1 100644
--- a/src/com/isode/stroke/elements/FormField.java
+++ b/src/com/isode/stroke/elements/FormField.java
@@ -47,7 +47,7 @@ public class FormField {
private Type type_;
public enum Type {
- UNKNOWN_TYPE("unknown"),
+ UNKNOWN_TYPE(""),
BOOLEAN_TYPE("boolean"),
FIXED_TYPE("fixed"),
HIDDEN_TYPE("hidden"),
@@ -76,6 +76,15 @@ public class FormField {
}
}
+ public FormField() {
+ this(Type.UNKNOWN_TYPE);
+ }
+
+ public FormField(Type type, String value) {
+ this(type);
+ addValue(value);
+ }
+
public FormField(Type type) {
type_ = type;
required_ = false;
@@ -84,12 +93,6 @@ public class FormField {
}
}
- public FormField(Type type, String value) {
- addValue(value);
- type_ = type;
- required_ = false;
- }
-
/**
* This class defines the option element that can be used in
* ListSingleFormField and ListMultiFormField. This class is
@@ -293,12 +296,13 @@ public class FormField {
* @return value boolean, will return false if FormField has no values
*/
public boolean getBoolValue() {
- if (type_ != Type.BOOLEAN_TYPE) {
+ if (type_ != Type.BOOLEAN_TYPE && type_ != Type.UNKNOWN_TYPE) {
throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
}
return values_.isEmpty() ? false : values_.get(0).equals("true") || values_.get(0).equals("1");
}
+
/**
* Sets the value of a FormField with type boolean to a boolean value.
* @param bool boolean
@@ -313,7 +317,7 @@ public class FormField {
* @return JID value, or empty JID is FormField has no values
*/
public JID getJIDSingleValue() {
- if (type_ != Type.JID_SINGLE_TYPE) {
+ if (type_ != Type.JID_SINGLE_TYPE && type_ != Type.UNKNOWN_TYPE) {
throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
}
return values_.isEmpty() ? new JID() : JID.fromString(values_.get(0));
@@ -325,7 +329,7 @@ public class FormField {
* @return JID value, or empty JID is FormField has no values
*/
public JID getJIDMultiValue(int index) {
- if (type_ != Type.JID_MULTI_TYPE) {
+ if (type_ != Type.JID_MULTI_TYPE && type_ != Type.UNKNOWN_TYPE) {
throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
}
return values_.isEmpty() ? new JID() : JID.fromString(values_.get(index));
@@ -336,7 +340,7 @@ public class FormField {
* @return value String, empty String if FormField has no values
*/
public String getTextPrivateValue() {
- if (type_ != Type.TEXT_PRIVATE_TYPE) {
+ if (type_ != Type.TEXT_PRIVATE_TYPE && type_ != Type.UNKNOWN_TYPE) {
throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
}
return values_.isEmpty() ? "" : values_.get(0);
@@ -347,7 +351,7 @@ public class FormField {
* @return value String, or empty String if invalid FormField type
*/
public String getFixedValue() {
- if (type_ != Type.FIXED_TYPE) {
+ if (type_ != Type.FIXED_TYPE && type_ != Type.UNKNOWN_TYPE) {
throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
}
return values_.isEmpty() ? "" : values_.get(0);
@@ -370,7 +374,7 @@ public class FormField {
* @return value String
*/
public String getTextMultiValue() {
- if (type_ != Type.TEXT_MULTI_TYPE) {
+ if (type_ != Type.TEXT_MULTI_TYPE && type_ != Type.UNKNOWN_TYPE) {
throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
}
StringBuilder val = new StringBuilder();
@@ -389,16 +393,16 @@ public class FormField {
* @param val String value to set, must not be null
*/
public void setTextMultiValue(String val) {
- if (type_ != Type.TEXT_MULTI_TYPE) {
+ if (type_ != Type.TEXT_MULTI_TYPE && type_ != Type.UNKNOWN_TYPE) {
throw new IllegalArgumentException(ILLEGAL_ARG_EX_STR + type_);
}
values_.clear();
- if (val.contains("\r\n")){
+ if (val.indexOf("\r\n") != -1) {
for (String s : val.split("\r\n")) {
values_.add(s);
}
}
- else if (val.contains("\n")){
+ else if (val.indexOf("\n") != -1){
for (String s : val.split("\n")) {
values_.add(s);
}
diff --git a/src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java
index 23160c6..79153af 100644
--- a/src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java
+++ b/src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java
@@ -93,72 +93,14 @@ public class FormSerializer extends GenericPayloadSerializer<Form> {
fieldElement.addNode(descriptionElement);
}
- String fieldType = field.getType().getDescription();
-
- if (!fieldType.isEmpty() && withTypeAttribute) {
- fieldElement.setAttribute("type", fieldType);
- }
-
- if (fieldType.equals("boolean")) {
- XMLElement valueElement = new XMLElement("value");
- if (field.getBoolValue() == true) {
- valueElement.addNode(XMLTextNode.create("1"));
- } else {
- valueElement.addNode(XMLTextNode.create("0"));
- }
- fieldElement.addNode(valueElement);
- }
-
- else if (fieldType.equals("jid-single")) {
- XMLElement valueElement = new XMLElement("value");
- valueElement.addNode(XMLTextNode.create(field.getJIDSingleValue().toString()));
- fieldElement.addNode(valueElement);
- }
-
- else if (fieldType.equals("jid-multi")) {
- for (String jid : field.getValues()){
- XMLElement valueElement = new XMLElement("value");
- valueElement.addNode(XMLTextNode.create(jid));
- fieldElement.addNode(valueElement);
- }
- }
-
- else if (fieldType.equals("text-private")) {
- XMLElement valueElement = new XMLElement("value");
- valueElement.addNode(XMLTextNode.create(field.getTextPrivateValue()));
- fieldElement.addNode(valueElement);
- }
-
- else if (fieldType.equals("fixed")) {
- XMLElement valueElement = new XMLElement("value");
- valueElement.addNode(XMLTextNode.create(field.getFixedValue()));
- fieldElement.addNode(valueElement);
- }
-
- else if (fieldType.equals("text-single") ||
- fieldType.equals("unknown")) {
- XMLElement valueElement = new XMLElement("value");
- valueElement.addNode(XMLTextNode.create(field.getTextSingleValue()));
- fieldElement.addNode(valueElement);
- }
-
- else if (fieldType.equals("text-multi")) {
- multiLineify(field.getTextMultiValue(),
- FormField.FORM_FIELD_ELEMENT_VALUE, fieldElement);
- }
-
- else if (fieldType.equals("list-multi") ||
- fieldType.equals("list-single")) {
- for (String s : field.getValues()) {
- XMLElement valueElement = new XMLElement("value");
- valueElement.addNode(XMLTextNode.create(s));
- fieldElement.addNode(valueElement);
- }
+ FormField.Type fieldType = field.getType();
+ if (fieldType != FormField.Type.UNKNOWN_TYPE && withTypeAttribute) {
+ fieldElement.setAttribute("type", fieldType.getDescription());
}
-
- else { // Unknown type
+
+ for (String s : field.getValues()) {
XMLElement valueElement = new XMLElement("value");
- valueElement.addNode(XMLTextNode.create(field.getValues().get(0)));
+ valueElement.addNode(XMLTextNode.create(s));
fieldElement.addNode(valueElement);
}
@@ -172,7 +114,6 @@ public class FormSerializer extends GenericPayloadSerializer<Form> {
optionElement.addNode(valueElement);
fieldElement.addNode(optionElement);
}
-
return fieldElement;
}