diff options
author | Tim Robbings <tim.robbings@isode.com> | 2014-11-27 17:22:23 (GMT) |
---|---|---|
committer | Swift Review <review@swift.im> | 2014-12-14 15:02:38 (GMT) |
commit | e327b17aa54430284e67af3accf90f45430f74d6 (patch) | |
tree | 552ada79ce95c85e020d2c7437e29e772500814a /src | |
parent | 62faa5c8f718399e7e947be04ee04bc942251ea3 (diff) | |
download | stroke-e327b17aa54430284e67af3accf90f45430f74d6.zip stroke-e327b17aa54430284e67af3accf90f45430f74d6.tar.bz2 |
Changes to improve handling of unknown field types
Change to mirror Swiften code. This change removes some unnecessary code from
the FormSerializer class. It also includes changes to the FormField class to
improve the handling of 'unknown' form fields.
Test-information:
Tested using updatedJUnit tests, all tests complete successfully.
Change-Id: Ie28ed40be976704170525f7be20b8e08661536b6
Diffstat (limited to 'src')
-rw-r--r-- | src/com/isode/stroke/elements/FormField.java | 36 | ||||
-rw-r--r-- | src/com/isode/stroke/serializer/payloadserializers/FormSerializer.java | 71 |
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; } |