1 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 46 public class XDataFormImpl { 47 48 private String type; 49 private String 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 type) { 60 this.type = type; 61 } 62 63 public void setTitle(String title) { 64 this.title = title; 65 } 66 67 public void setInstructions(List instructions) { 68 this.instructions = instructions; 69 } 70 71 public String getType() { 72 return type; 73 } 74 75 public String 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 variable) { 86 if (variable == null || variable.equals("")) { 87 throw new IllegalArgumentException ("Variable must not be null or blank."); 88 } 89 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 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 reportedItems.add(itemFields); 132 } 133 } 134 135 public String getNamespace() { 136 return "jabber:x:data"; 138 } 139 140 public void setNamespace(String namespace) { 141 } 144 145 public String getName() { 146 return "x"; 148 } 149 150 public void setName(String name) { 151 } 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 )instrItr.next()); 167 } 168 } 169 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 if (reportedItems.size() > 0) { 182 Iterator itemsItr = reportedItems.iterator(); 183 while (itemsItr.hasNext()) { 184 Element itemElement = x.addElement("item"); 186 List fields = (List)itemsItr.next(); 187 Iterator fieldsItr = fields.iterator(); 188 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 |