1 11 12 package org.jivesoftware.messenger.forms.spi; 13 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import org.dom4j.DocumentHelper; 19 import org.dom4j.Element; 20 import org.dom4j.QName; 21 import org.jivesoftware.messenger.forms.FormField; 22 23 29 public class XFormFieldImpl implements FormField { 30 31 private String description; 32 private boolean required = false; 33 private String label; 34 private String variable; 35 private String type; 36 private List <Option> options = new ArrayList <Option>(); 37 private List <String > values = new ArrayList <String >(); 38 39 public XFormFieldImpl() { 40 super(); 41 } 42 43 public XFormFieldImpl(String variable) { 44 this.variable = variable; 45 } 46 47 public String getNamespace() { 48 return "jabber:x:data"; 50 } 51 52 public void setNamespace(String namespace) { 53 } 56 57 public String getName() { 58 return "x"; 60 } 61 62 public void setName(String name) { 63 } 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 if (values.size() > 0) { 87 Iterator <String > valuesItr = getValues(); 88 while (valuesItr.hasNext()) { 89 field.addElement("value").addText(valuesItr.next()); 90 } 91 } 92 if (options.size() > 0) { 94 Iterator <Option> optionsItr = getOptions(); 95 while (optionsItr.hasNext()) { 96 field.add((optionsItr.next()).asXMLElement()); 97 } 98 } 99 100 106 107 return field; 108 } 109 110 public void addValue(String 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 label, String value) { 126 synchronized (options) { 127 options.add(new Option(label, value)); 128 } 129 } 130 131 public void setType(String type) { 132 this.type = type; 133 } 134 135 public void setRequired(boolean required) { 136 this.required = required; 137 } 138 139 public void setLabel(String label) { 140 this.label = label; 141 } 142 143 public void setDescription(String description) { 144 this.description = description; 145 } 146 147 public boolean isRequired() { 148 return required; 149 } 150 151 public String getVariable() { 152 return variable; 153 } 154 155 public Iterator <String > getValues() { 156 synchronized (values) { 157 return Collections.unmodifiableList(new ArrayList <String >(values)).iterator(); 158 } 159 } 160 161 public String getType() { 162 return type; 163 } 164 165 171 private Iterator <Option> getOptions() { 172 synchronized (options) { 173 return Collections.unmodifiableList(new ArrayList <Option>(options)).iterator(); 174 } 175 } 176 177 public String getLabel() { 178 return label; 179 } 180 181 public String 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 valueElements = formElement.elementIterator("value"); 198 while (valueElements.hasNext()) { 199 addValue(((Element)valueElements.next()).getTextTrim()); 200 } 201 Iterator 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 toString() { 210 return "XFormFieldImpl " + Integer.toHexString(hashCode()) + " " + getVariable() + ">" + values 211 + " o: " + (options.isEmpty() ? "no options" : options.toString()); 212 } 213 214 219 private static class Option { 220 private String label; 221 private String value; 222 223 public Option(String label, String value) { 224 this.label = label; 225 this.value = value; 226 } 227 228 233 public String getLabel() { 234 return label; 235 } 236 237 242 public String 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 |