KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $RCSfile: XDataFormImpl.java,v $
3  * $Revision: 1.5 $
4  * $Date: 2005/01/25 18:41:48 $
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 org.jivesoftware.messenger.forms.FormField;
15
16 import java.util.*;
17 import org.dom4j.DocumentHelper;
18 import org.dom4j.Element;
19 import org.dom4j.QName;
20
21 /**
22  * A concrete DataForm capable of sending itself to a writer and recover its state from an XMPP
23  * stanza. XDataForms are packets of the form:
24  * <code><pre>
25  * &lt;x xmlns='jabber:x:data' type='{form-type}'&gt;
26  * &lt;title/&gt;
27  * &lt;instructions/&gt;
28  * &lt;field var='field-name'
29  * type='{field-type}'
30  * label='description'&gt;
31  * &lt;desc/&gt;
32  * &lt;required/&gt;
33  * &lt;value&gt;field-value&lt;/value&gt;
34  * &lt;option label='option-label'&gt;&lt;value&gt;option-value&lt;/value&gt;&lt;/option&gt;
35  * &lt;option label='option-label'&gt;&lt;value&gt;option-value&lt;/value&gt;&lt;/option&gt;
36  * &lt;/field&gt;
37  * &lt;/x&gt;
38  * </pre></code>
39  * <p/>
40  * An XDataFormImpl can contain zero or more XFormFieldImpl 'field' fragments.<p>
41  * <p/>
42  * To learn more follow this link: <a HREF="http://www.jabber.org/jeps/jep-0004.html">JEP-04</a>.
43  *
44  * @author gdombiak
45  */

46 public class XDataFormImpl {
47
48     private String JavaDoc type;
49     private String JavaDoc title;
50     private List instructions = new ArrayList();
51     private List fields = new ArrayList();
52     private List reportedFields = new ArrayList();
53     private List reportedItems = new ArrayList();
54
55     public XDataFormImpl() {
56         super();
57     }
58
59     public XDataFormImpl(String JavaDoc type) {
60         this.type = type;
61     }
62
63     public void setTitle(String JavaDoc title) {
64         this.title = title;
65     }
66
67     public void setInstructions(List instructions) {
68         this.instructions = instructions;
69     }
70
71     public String JavaDoc getType() {
72         return type;
73     }
74
75     public String JavaDoc getTitle() {
76         return title;
77     }
78
79     public Iterator getInstructions() {
80         synchronized (instructions) {
81             return Collections.unmodifiableList(new ArrayList(instructions)).iterator();
82         }
83     }
84
85     public FormField getField(String JavaDoc variable) {
86         if (variable == null || variable.equals("")) {
87             throw new IllegalArgumentException JavaDoc("Variable must not be null or blank.");
88         }
89         // Look for the field whose variable matches the requested variable
90
FormField field;
91         for (Iterator it = getFields(); it.hasNext();) {
92             field = (FormField)it.next();
93             if (variable.equals(field.getVariable())) {
94                 return field;
95             }
96         }
97         return null;
98     }
99
100     public Iterator getFields() {
101         synchronized (fields) {
102             return Collections.unmodifiableList(new ArrayList(fields)).iterator();
103         }
104     }
105
106     public int getFieldsSize() {
107         return fields.size();
108     }
109
110     public void addInstruction(String JavaDoc instruction) {
111         synchronized (instructions) {
112             instructions.add(instruction);
113         }
114     }
115
116     public void addField(FormField field) {
117         synchronized (fields) {
118             fields.add(field);
119         }
120     }
121
122     public void addReportedField(FormField field) {
123         synchronized (reportedFields) {
124             reportedFields.add(field);
125         }
126     }
127
128     public void addItemFields(ArrayList itemFields) {
129         synchronized (reportedItems) {
130             // We are nesting a List (of fields) inside of the List of items
131
reportedItems.add(itemFields);
132         }
133     }
134
135     public String JavaDoc getNamespace() {
136         // Is someone sending this message?
137
return "jabber:x:data";
138     }
139
140     public void setNamespace(String JavaDoc namespace) {
141         // Is someone sending this message?
142
// Do nothing
143
}
144
145     public String JavaDoc getName() {
146         // Is someone sending this message?
147
return "x";
148     }
149
150     public void setName(String JavaDoc name) {
151         // Is someone sending this message?
152
// Do nothing
153
}
154
155     public Element asXMLElement() {
156         Element x = DocumentHelper.createElement(QName.get("x", "jabber:x:data"));
157         if (getType() != null) {
158             x.addAttribute("type", getType());
159         }
160         if (getTitle() != null) {
161             x.addElement("title").addText(getTitle());
162         }
163         if (instructions.size() > 0) {
164             Iterator instrItr = getInstructions();
165             while (instrItr.hasNext()) {
166                 x.addElement("instructions").addText((String JavaDoc)instrItr.next());
167             }
168         }
169         // Append the list of fields returned from a search
170
if (reportedFields.size() > 0) {
171             Element reportedElement = x.addElement("reported");
172             Iterator fieldsItr = reportedFields.iterator();
173             while (fieldsItr.hasNext()) {
174                 XFormFieldImpl field = (XFormFieldImpl)fieldsItr.next();
175                 reportedElement.add(field.asXMLElement());
176             }
177         }
178
179         // Append the list of items returned from a search
180
// Note: each item contains a List of XFormFieldImpls
181
if (reportedItems.size() > 0) {
182             Iterator itemsItr = reportedItems.iterator();
183             while (itemsItr.hasNext()) {
184                 // Add a new item element for this list of fields
185
Element itemElement = x.addElement("item");
186                 List fields = (List)itemsItr.next();
187                 Iterator fieldsItr = fields.iterator();
188                 // Iterate on the fields and add them to the new item
189
while (fieldsItr.hasNext()) {
190                     XFormFieldImpl field = (XFormFieldImpl)fieldsItr.next();
191                     itemElement.add(field.asXMLElement());
192                 }
193             }
194         }
195
196         if (fields.size() > 0) {
197             Iterator fieldsItr = getFields();
198             while (fieldsItr.hasNext()) {
199                 XFormFieldImpl field = (XFormFieldImpl)fieldsItr.next();
200                 x.add(field.asXMLElement());
201             }
202         }
203
204         return x;
205     }
206
207     public void parse(Element formElement) {
208         type = formElement.attributeValue("type");
209         Element titleElement = formElement.element("title");
210         if (titleElement != null) {
211             setTitle(titleElement.getTextTrim());
212         }
213         Iterator instructionElements = formElement.elementIterator("instructions");
214         while (instructionElements.hasNext()) {
215             addInstruction(((Element)instructionElements.next()).getTextTrim());
216         }
217         Iterator fieldElements = formElement.elementIterator("field");
218         while (fieldElements.hasNext()) {
219             XFormFieldImpl field = new XFormFieldImpl();
220             field.parse((Element)fieldElements.next());
221             addField(field);
222         }
223
224         Element reportedElement = formElement.element("reported");
225         if (reportedElement != null) {
226             Iterator reportedFieldElements = reportedElement.elementIterator("field");
227             while (reportedFieldElements.hasNext()) {
228                 XFormFieldImpl field = new XFormFieldImpl();
229                 field.parse((Element)reportedFieldElements.next());
230                 addReportedField(field);
231             }
232         }
233
234         Iterator itemElements = formElement.elementIterator("item");
235         while (itemElements.hasNext()) {
236             Element itemElement = (Element)itemElements.next();
237             Iterator itemFieldElements = itemElement.elementIterator("field");
238             ArrayList itemFields = new ArrayList();
239             while (itemFieldElements.hasNext()) {
240                 XFormFieldImpl field = new XFormFieldImpl();
241                 field.parse((Element)itemFieldElements.next());
242                 itemFields.add(field);
243             }
244             addItemFields(itemFields);
245         }
246     }
247 }
Popular Tags