diff options
3 files changed, 39 insertions, 86 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; } diff --git a/test/com/isode/stroke/serializer/payloadserializers/FormSerializerTest.java b/test/com/isode/stroke/serializer/payloadserializers/FormSerializerTest.java index 7b16aa4..d7347cb 100644 --- a/test/com/isode/stroke/serializer/payloadserializers/FormSerializerTest.java +++ b/test/com/isode/stroke/serializer/payloadserializers/FormSerializerTest.java @@ -51,6 +51,11 @@ public class FormSerializerTest { field.addValue("someText"); form.addReportedField(field); + field = new FormField(FormField.Type.TEXT_MULTI_TYPE); + field.setLabel("text-multi-field"); + field.setTextMultiValue("This is some text\nthis is some more"); + form.addField(field); + FormItem item = new FormItem(); field = new FormField(FormField.Type.UNKNOWN_TYPE); field.setName("itemField"); @@ -69,8 +74,8 @@ public class FormSerializerTest { field.setLabel("The name of your bot"); form.addField(field); - field = new FormField(FormField.Type.TEXT_MULTI_TYPE, - "This is a bot.\nA quite good one actually"); + field = new FormField(FormField.Type.TEXT_MULTI_TYPE); + field.setTextMultiValue("This is a bot.\nA quite good one actually"); field.setName("description"); field.setLabel("Helpful description of your bot"); form.addField(field); @@ -129,7 +134,7 @@ public class FormSerializerTest { assertEquals( "<x type=\"form\" xmlns=\"jabber:x:data\">" + "<reported>" - + "<field label=\"description\" type=\"unknown\" var=\"field name\">" + + "<field label=\"description\" var=\"field name\">" + "<value>someText</value>" + "</field>" + "</reported>" @@ -138,17 +143,20 @@ public class FormSerializerTest { + "<value>itemValue</value>" + "</field>" + "</item>" + + "<field label=\"text-multi-field\" type=\"text-multi\">" + + "<value>This is some text</value><value>this is some more</value>" + + "</field>" + "<field type=\"hidden\" var=\"FORM_TYPE\">" + "<value>jabber:bot</value>" + "</field>" + "<field type=\"fixed\"><value>Section 1: Bot Info</value></field>" - + "<field label=\"The name of your bot\" type=\"text-single\" var=\"botname\"><value></value></field>" + + "<field label=\"The name of your bot\" type=\"text-single\" var=\"botname\"/>" + "<field label=\"Helpful description of your bot\" type=\"text-multi\" var=\"description\"><value>This is a bot.</value><value>A quite good one actually</value></field>" + "<field label=\"Public bot?\" type=\"boolean\" var=\"public\">" + "<required/>" + "<value>1</value>" + "</field>" - + "<field label=\"Password for special access\" type=\"text-private\" var=\"password\"><value></value></field>" + + "<field label=\"Password for special access\" type=\"text-private\" var=\"password\"/>" + "<field label=\"What features will the bot support?\" type=\"list-multi\" var=\"features\">" + "<value>news</value>" + "<value>search</value>" |