1 20 21 package org.xmpp.forms; 22 23 import org.dom4j.Element; 24 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 36 public class FormField { 37 38 private Element element; 39 40 FormField(Element element) { 41 this.element = element; 42 } 43 44 50 public void addValue(Object value) { 51 element.addElement("value").setText(DataForm.encode(value)); 52 } 53 54 57 public void clearValues() { 58 for (Iterator it = element.elementIterator("value"); it.hasNext();) { 59 it.next(); 60 it.remove(); 61 } 62 } 63 64 71 public void addOption(String label, String value) { 72 Element option = element.addElement("option"); 73 option.addAttribute("label", label); 74 option.addElement("value").setText(value); 75 } 76 77 83 public List <Option> getOptions() { 84 List <Option> answer = new ArrayList <Option>(); 85 for (Iterator it = element.elementIterator("option"); it.hasNext();) { 86 answer.add(new Option((Element) it.next())); 87 } 88 return answer; 89 } 90 91 112 public void setType(Type type) { 113 element.addAttribute("type", type==null?null:type.toXMPP()); 114 } 115 116 122 public void setVariable(String var) { 123 element.addAttribute("var", var); 124 } 125 126 132 public void setLabel(String label) { 133 element.addAttribute("label", label); 134 } 135 136 141 public void setRequired(boolean required) { 142 if (element.element("required") != null) { 144 element.remove(element.element("required")); 145 } 146 if (required) { 147 element.addElement("required"); 148 } 149 } 150 151 160 public void setDescription(String description) { 161 if (element.element("desc") != null) { 163 element.remove(element.element("desc")); 164 } 165 element.addElement("desc").setText(description); 166 } 167 168 173 public boolean isRequired() { 174 return element.element("required") != null; 175 } 176 177 182 public String getVariable() { 183 return element.attributeValue("var"); 184 } 185 186 193 public List <String > getValues() { 194 List <String > answer = new ArrayList <String >(); 195 for (Iterator it = element.elementIterator("value"); it.hasNext();) { 196 answer.add(((Element) it.next()).getTextTrim()); 197 } 198 return answer; 199 } 200 201 222 public Type getType() { 223 String type = element.attributeValue("type"); 224 if (type != null) { 225 Type.fromXMPP(type); 226 } 227 return null; 228 } 229 230 236 public String getLabel() { 237 return element.attributeValue("label"); 238 } 239 240 249 public String getDescription() { 250 return element.elementTextTrim("desc"); 251 } 252 253 258 public static class Option { 259 private Element element; 260 261 private Option(Element element) { 262 this.element = element; 263 } 264 265 270 public String getLabel() { 271 return element.attributeValue("label"); 272 } 273 274 279 public String getValue() { 280 return element.elementTextTrim("value"); 281 } 282 } 283 284 294 public enum Type { 295 300 boolean_type("boolean"), 301 302 308 fixed("fixed"), 309 310 314 hidden("hidden"), 315 316 319 jid_multi("jid-multi"), 320 321 324 jid_single("jid-single"), 325 326 330 list_multi("list-multi"), 331 332 335 list_single("list-single"), 336 337 340 text_multi("text-multi"), 341 342 346 text_private("text-private"), 347 348 353 text_single("text-single"); 354 355 361 public static Type fromXMPP(String type) { 362 if (type == null) { 363 throw new NullPointerException (); 364 } 365 type = type.toLowerCase(); 366 if (boolean_type.toXMPP().equals(type)) { 367 return boolean_type; 368 } 369 else if (fixed.toXMPP().equals(type)) { 370 return fixed; 371 } 372 else if (hidden.toXMPP().equals(type)) { 373 return hidden; 374 } 375 else if (jid_multi.toXMPP().equals(type)) { 376 return jid_multi; 377 } 378 else if (jid_single.toXMPP().equals(type)) { 379 return jid_single; 380 } 381 else if (list_multi.toXMPP().equals(type)) { 382 return list_multi; 383 } 384 else if (list_single.toXMPP().equals(type)) { 385 return list_single; 386 } 387 else if (text_multi.toXMPP().equals(type)) { 388 return text_multi; 389 } 390 else if (text_private.toXMPP().equals(type)) { 391 return text_private; 392 } 393 else if (text_single.toXMPP().equals(type)) { 394 return text_single; 395 } 396 else { 397 throw new IllegalArgumentException ("Type invalid:" + type); 398 } 399 } 400 401 private String value; 402 403 private Type(String value) { 404 this.value = value; 405 } 406 407 412 public String toXMPP() { 413 return value; 414 } 415 416 } 417 } 418 | Popular Tags |