summaryrefslogtreecommitdiffstats
blob: 362408a8310b9efd6e3419dccfbc1d64c49b0846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright (c) 2012 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.FormField.HiddenFormField;

/**
 * XEP-0004 Data form. For the relevant Fields, the parsers and serialisers
 * protect the API user against the strange multi-value instead of newline thing
 * by transforming them.
 */
public class Form extends Payload {
    /**
     * Attribute "type"
     */
    public static final String FORM_ATTRIBUTE_TYPE = "type";

    /**
     * Element "title"
     */
    public static final String FORM_ELEMENT_TITLE = "title";

    /**
     * Element "instructions"
     */
    public static final String FORM_ELEMENT_INSTRUCTIONS = "instructions";

    /**
     * Element "field"
     */
    public static final String FORM_ELEMENT_FIELD = "field";

    /**
     * Type of form
     */
    public enum Type {
        /**
         * The form-processing entity is asking the form-submitting entity to
         * complete a form.
         */
        FORM_TYPE("form"),
        /**
         * The form-submitting entity is submitting data to the form-processing
         * entity.
         */
        SUBMIT_TYPE("submit"),
        /**
         * The form-submitting entity has cancelled submission of data to the
         * form-processing entity.
         */
        CANCEL_TYPE("cancel"),
        /**
         * The form-processing entity is returning data (e.g., search results)
         * to the form-submitting entity, or the data is a generic data set.
         */
        RESULT_TYPE("result");

        private String stringForm_;

        private Type(String stringForm) {
            stringForm_ = stringForm;
        }

        /**
         * Get type from its string form.
         *
         * @param stringForm String form of type, can be null
         *
         * @return Corresponding type if match found, otherwise
         *         {@link Type#FORM_TYPE}. Will never be null.
         */
        public static Type getType(String stringForm) {
            if (stringForm != null) {
                for (Type type : Type.values()) {
                    if (type.stringForm_.equals(stringForm)) {
                        return type;
                    }
                }
            }

            return FORM_TYPE;
        }

        /**
         * @return String form of type, will never be null
         */
        public String getStringForm() {
            return stringForm_;
        }
    };

    private List<FormField> fields_ = new ArrayList<FormField>();
    private String title_ = "";
    private String instructions_ = "";
    private Type type_;

    /**
     * Create a form of the given type.
     *
     * @param type Form type, must not be null
     */
    public Form(Type type) {
        setType(type);
    }

    /**
     * Create a form of {@link Type#FORM_TYPE}.
     */
    public Form() {
        this(Type.FORM_TYPE);
    }

    /**
     * Add to the list of fields for the form.
     *
     * @param field Field to add, must not be null. The instance of the form
     *            field is stored in the object, a copy is not made.
     */
    public void addField(FormField field) {
        if (field == null) {
            throw new NullPointerException("'field' must not be null");
        }

        fields_.add(field);
    }

    /**
     * @return List of fields for the form, will never be null. The instance of
     *         the list stored in the object is not returned, a copy is made.
     *         But the instances of the form fields stored in the list is the
     *         same as the list in the object, a copy is not made.
     */
    public List<FormField> getFields() {
        return new ArrayList<FormField>(fields_);
    }

    /**
     * Set title of the form.
     *
     * @param title title of the form, must not be null
     */
    public void setTitle(String title) {
        if (title == null) {
            throw new NullPointerException("'title' must not be null");
        }

        title_ = title;
    }

    /**
     * @return title of the form, will never be null
     */
    public String getTitle() {
        return title_;
    }

    /**
     * Set natural-language instructions for the form.
     *
     * @param instructions instructions for the form, must not be null
     */
    public void setInstructions(String instructions) {
        if (instructions == null) {
            throw new NullPointerException("'instructions' must not be null");
        }

        instructions_ = instructions;
    }

    /**
     * @return natural-language instructions for the form, will never be null
     */
    public String getInstructions() {
        return instructions_;
    }

    /**
     * Set type of the form.
     *
     * @param type Form type, must not be null
     */
    public void setType(Type type) {
        if (type == null) {
            throw new NullPointerException("'type' must not be null");
        }

        type_ = type;
    }

    /**
     * @return type of the form, will never be null
     */
    public Type getType() {
        return type_;
    }

    /**
     * @return Value of the "FORM_TYPE" hidden form field if it is present in
     *         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
        }

        return ((value != null) ? value : "");
    }

    /**
     * Get form field for the given name.
     *
     * @param name Name of form field to retrieve, must not be null
     *
     * @return Form field with the given name if it is present in the form, null
     *         otherwise. The instance of the form field stored in the object is
     *         returned, a copy is not made.
     */
    public FormField getField(String name) {
        if (name == null) {
            throw new NullPointerException("'name' must not be null");
        }

        for (FormField field : fields_) {
            if (field.getName().equals(name)) {
                return field;
            }
        }

        return null;
    }

    @Override
    public String toString() {
        return Form.class.getSimpleName() + "\ntitle: " + title_
                + "\ninstructions: " + instructions_ + "\ntype: " + type_;
    }
}