KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > forms > spi > XFormFieldImpl


1 /**
2  * $RCSfile: XFormFieldImpl.java,v $
3  * $Revision: 1.9 $
4  * $Date: 2005/02/03 00:55:43 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.forms.spi;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import org.dom4j.DocumentHelper;
19 import org.dom4j.Element;
20 import org.dom4j.QName;
21 import org.jivesoftware.messenger.forms.FormField;
22
23 /**
24  * A concrete FormField capable of sending itself to a writer and recover its state from an XMPP
25  * stanza.
26  *
27  * @author gdombiak
28  */

29 public class XFormFieldImpl implements FormField {
30
31     private String JavaDoc description;
32     private boolean required = false;
33     private String JavaDoc label;
34     private String JavaDoc variable;
35     private String JavaDoc type;
36     private List JavaDoc<Option> options = new ArrayList JavaDoc<Option>();
37     private List JavaDoc<String JavaDoc> values = new ArrayList JavaDoc<String JavaDoc>();
38
39     public XFormFieldImpl() {
40         super();
41     }
42
43     public XFormFieldImpl(String JavaDoc variable) {
44         this.variable = variable;
45     }
46
47     public String JavaDoc getNamespace() {
48         // Is someone sending this message?
49
return "jabber:x:data";
50     }
51
52     public void setNamespace(String JavaDoc namespace) {
53         // Is someone sending this message?
54
// Do nothing
55
}
56
57     public String JavaDoc getName() {
58         // Is someone sending this message?
59
return "x";
60     }
61
62     public void setName(String JavaDoc name) {
63         // Is someone sending this message?
64
// Do nothing
65
}
66
67     public Element asXMLElement() {
68         Element field = DocumentHelper.createElement(QName.get("field", "jabber:x:data"));
69         if (getLabel() != null) {
70             field.addAttribute("label", getLabel());
71         }
72         if (getVariable() != null) {
73             field.addAttribute("var", getVariable());
74         }
75         if (getType() != null) {
76             field.addAttribute("type", getType());
77         }
78
79         if (getDescription() != null) {
80             field.addElement("desc").addText(getDescription());
81         }
82         if (isRequired()) {
83             field.addElement("required");
84         }
85         // Loop through all the values and append them to the stream writer
86
if (values.size() > 0) {
87             Iterator JavaDoc<String JavaDoc> valuesItr = getValues();
88             while (valuesItr.hasNext()) {
89                 field.addElement("value").addText(valuesItr.next());
90             }
91         }
92         // Loop through all the options and append them to the stream writer
93
if (options.size() > 0) {
94             Iterator JavaDoc<Option> optionsItr = getOptions();
95             while (optionsItr.hasNext()) {
96                 field.add((optionsItr.next()).asXMLElement());
97             }
98         }
99
100         // Loop through all the values and append them to the stream writer
101
/*Iterator frags = fragments.iterator();
102         while (frags.hasNext()){
103             XMPPFragment frag = (XMPPFragment) frags.next();
104             frag.send(xmlSerializer,version);
105         }*/

106
107         return field;
108     }
109
110     public void addValue(String JavaDoc value) {
111         if (value == null) {
112             value = "";
113         }
114         synchronized (values) {
115             values.add(value);
116         }
117     }
118
119     public void clearValues() {
120         synchronized (values) {
121             values.clear();
122         }
123     }
124
125     public void addOption(String JavaDoc label, String JavaDoc value) {
126         synchronized (options) {
127             options.add(new Option(label, value));
128         }
129     }
130
131     public void setType(String JavaDoc type) {
132         this.type = type;
133     }
134
135     public void setRequired(boolean required) {
136         this.required = required;
137     }
138
139     public void setLabel(String JavaDoc label) {
140         this.label = label;
141     }
142
143     public void setDescription(String JavaDoc description) {
144         this.description = description;
145     }
146
147     public boolean isRequired() {
148         return required;
149     }
150
151     public String JavaDoc getVariable() {
152         return variable;
153     }
154
155     public Iterator JavaDoc<String JavaDoc> getValues() {
156         synchronized (values) {
157             return Collections.unmodifiableList(new ArrayList JavaDoc<String JavaDoc>(values)).iterator();
158         }
159     }
160
161     public String JavaDoc getType() {
162         return type;
163     }
164
165     /**
166      * Returns an Iterator for the available options that the user has in order to answer
167      * the question.
168      *
169      * @return Iterator for the available options.
170      */

171     private Iterator JavaDoc<Option> getOptions() {
172         synchronized (options) {
173             return Collections.unmodifiableList(new ArrayList JavaDoc<Option>(options)).iterator();
174         }
175     }
176
177     public String JavaDoc getLabel() {
178         return label;
179     }
180
181     public String JavaDoc getDescription() {
182         return description;
183     }
184
185     public void parse(Element formElement) {
186         variable = formElement.attributeValue("var");
187         setLabel(formElement.attributeValue("label"));
188         setType(formElement.attributeValue("type"));
189
190         Element descElement = formElement.element("desc");
191         if (descElement != null) {
192             setDescription(descElement.getTextTrim());
193         }
194         if (formElement.element("required") != null) {
195             setRequired(true);
196         }
197         Iterator JavaDoc valueElements = formElement.elementIterator("value");
198         while (valueElements.hasNext()) {
199             addValue(((Element)valueElements.next()).getTextTrim());
200         }
201         Iterator JavaDoc optionElements = formElement.elementIterator("option");
202         Element optionElement;
203         while (optionElements.hasNext()) {
204             optionElement = (Element)optionElements.next();
205             addOption(optionElement.attributeValue("label"), optionElement.elementTextTrim("value"));
206         }
207     }
208
209     public String JavaDoc toString() {
210         return "XFormFieldImpl " + Integer.toHexString(hashCode()) + " " + getVariable() + ">" + values
211                 + " o: " + (options.isEmpty() ? "no options" : options.toString());
212     }
213
214     /**
215      * Represents the available option of a given FormField.
216      *
217      * @author Gaston Dombiak
218      */

219     private static class Option {
220         private String JavaDoc label;
221         private String JavaDoc value;
222
223         public Option(String JavaDoc label, String JavaDoc value) {
224             this.label = label;
225             this.value = value;
226         }
227
228         /**
229          * Returns the label that represents the option.
230          *
231          * @return the label that represents the option.
232          */

233         public String JavaDoc getLabel() {
234             return label;
235         }
236
237         /**
238          * Returns the value of the option.
239          *
240          * @return the value of the option.
241          */

242         public String JavaDoc getValue() {
243             return value;
244         }
245
246         public Element asXMLElement() {
247             Element option = DocumentHelper.createElement(QName.get("option", "jabber:x:data"));
248             if (getLabel() != null) {
249                 option.addAttribute("label", getLabel());
250             }
251             if (getValue() != null) {
252                 option.addElement("value").addText(getValue());
253             }
254             return option;
255         }
256     }
257 }
Popular Tags