From 47cad5fd7883b8c7273ea11073643aa585b485c6 Mon Sep 17 00:00:00 2001 From: Tim Robbings Date: Wed, 22 Oct 2014 10:06:10 +0100 Subject: Stroke FormField refactoring Changes to catch up with Swiften changes to FormField in commit 00284e5, also adds and elements, added to Swiften in commit 83afa3d. Changes include refactoring of the FormField class, changes to Form parser and serializer classes and updates to JUnit tests. Test-information: Tested using updated JUnit tests, all tests complete successfully. Change-Id: Ic91ad4a11a335fb3d2b2a2c4a1865f836e2af70b Reviewer: Alex Clayton Reviewer: Gurmeen Bindra diff --git a/src/com/isode/stroke/elements/Form.java b/src/com/isode/stroke/elements/Form.java index 362408a..8068f28 100644 --- a/src/com/isode/stroke/elements/Form.java +++ b/src/com/isode/stroke/elements/Form.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012 Isode Limited, London, England. + * Copyright (c) 2012-2014 Isode Limited, London, England. * All rights reserved. */ /* @@ -12,7 +12,6 @@ package com.isode.stroke.elements; import java.util.ArrayList; import java.util.List; -import com.isode.stroke.elements.FormField.HiddenFormField; /** * XEP-0004 Data form. For the relevant Fields, the parsers and serialisers @@ -34,6 +33,16 @@ public class Form extends Payload { * Element "instructions" */ public static final String FORM_ELEMENT_INSTRUCTIONS = "instructions"; + + /** + * Element "reported" + */ + public static final String FORM_ELEMENT_REPORTED = "reported"; + + /** + * Element "item" + */ + public static final String FORM_ELEMENT_ITEM = "item"; /** * Element "field" @@ -97,13 +106,15 @@ public class Form extends Payload { public String getStringForm() { return stringForm_; } - }; + } private List fields_ = new ArrayList(); - private String title_ = ""; + private List reportedFields_ = new ArrayList(); + private List items_ = new ArrayList(); private String instructions_ = ""; + private String title_ = ""; private Type type_; - + /** * Create a form of the given type. * @@ -130,7 +141,6 @@ public class Form extends Payload { if (field == null) { throw new NullPointerException("'field' must not be null"); } - fields_.add(field); } @@ -143,6 +153,52 @@ public class Form extends Payload { public List getFields() { return new ArrayList(fields_); } + + /** + * Add a reported element to this Form. + * @param reportedField should not be null + */ + public void addReportedField(FormField reportedField) { + if (reportedField == null) { + throw new NullPointerException("'reportedField' should not be null"); + } + reportedFields_.add(reportedField); + } + + /** + * Return the list of reported fields for this Form. + * @return reportedFields_, never null + */ + public List getReportedFields() { + return reportedFields_; + } + + /** + * Add a list of item elements to the Form. + * @param item List, should not be null + */ + public void addItem(FormItem item) { + if (item == null) { + throw new NullPointerException("'item' should not be null"); + } + items_.add(item); + } + + /** + * Get the list of FormItem elements for the form. + * @return itemsCopy ArrayList>, list of items for the Form, + * a copy is made + */ + public List getItems() { + return new ArrayList(items_); + } + + /** + * Remove all reported fields from this Form. + */ + public void clearReportedFields() { + reportedFields_.clear(); + } /** * Set title of the form. @@ -209,19 +265,11 @@ public class Form extends Payload { * the form, an empty string otherwise, will never be null */ public String getFormType() { - String value = null; - FormField field = getField("FORM_TYPE"); - try { - HiddenFormField f = (HiddenFormField) field; - if (f != null) { - value = f.getValue(); - } - } catch (ClassCastException e) { - // value remains null + if (field != null && field.getType() == FormField.Type.HIDDEN_TYPE) { + return field.getValues().isEmpty() ? "" : field.getValues().get(0); } - - return ((value != null) ? value : ""); + return ""; } /** 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