1 20 21 package org.jivesoftware.smackx; 22 23 import java.util.*; 24 25 32 public class FormField { 33 public static final String TYPE_BOOLEAN = "boolean"; 34 public static final String TYPE_FIXED = "fixed"; 35 public static final String TYPE_HIDDEN = "hidden"; 36 public static final String TYPE_JID_MULTI = "jid-multi"; 37 public static final String TYPE_JID_SINGLE = "jid-single"; 38 public static final String TYPE_LIST_MULTI = "list-multi"; 39 public static final String TYPE_LIST_SINGLE = "list-single"; 40 public static final String TYPE_TEXT_MULTI = "text-multi"; 41 public static final String TYPE_TEXT_PRIVATE = "text-private"; 42 public static final String TYPE_TEXT_SINGLE = "text-single"; 43 44 private String description; 45 private boolean required = false; 46 private String label; 47 private String variable; 48 private String type; 49 private List options = new ArrayList(); 50 private List values = new ArrayList(); 51 52 58 public FormField(String variable) { 59 this.variable = variable; 60 } 61 62 67 public FormField() { 68 this.type = FormField.TYPE_FIXED; 69 } 70 71 80 public String getDescription() { 81 return description; 82 } 83 84 90 public String getLabel() { 91 return label; 92 } 93 94 100 public Iterator getOptions() { 101 synchronized (options) { 102 return Collections.unmodifiableList(new ArrayList(options)).iterator(); 103 } 104 } 105 106 111 public boolean isRequired() { 112 return required; 113 } 114 115 136 public String getType() { 137 return type; 138 } 139 140 147 public Iterator getValues() { 148 synchronized (values) { 149 return Collections.unmodifiableList(new ArrayList(values)).iterator(); 150 } 151 } 152 153 158 public String getVariable() { 159 return variable; 160 } 161 162 171 public void setDescription(String description) { 172 this.description = description; 173 } 174 175 181 public void setLabel(String label) { 182 this.label = label; 183 } 184 185 190 public void setRequired(boolean required) { 191 this.required = required; 192 } 193 194 215 public void setType(String type) { 216 this.type = type; 217 } 218 219 225 public void addValue(String value) { 226 synchronized (values) { 227 values.add(value); 228 } 229 } 230 231 237 public void addValues(List newValues) { 238 synchronized (values) { 239 values.addAll(newValues); 240 } 241 } 242 243 247 protected void resetValues() { 248 synchronized (values) { 249 values.removeAll(new ArrayList(values)); 250 } 251 } 252 253 259 public void addOption(Option option) { 260 synchronized (options) { 261 options.add(option); 262 } 263 } 264 265 public String toXML() { 266 StringBuffer buf = new StringBuffer (); 267 buf.append("<field"); 268 if (getLabel() != null) { 270 buf.append(" label=\"").append(getLabel()).append("\""); 271 } 272 if (getVariable() != null) { 273 buf.append(" var=\"").append(getVariable()).append("\""); 274 } 275 if (getType() != null) { 276 buf.append(" type=\"").append(getType()).append("\""); 277 } 278 buf.append(">"); 279 if (getDescription() != null) { 281 buf.append("<desc>").append(getDescription()).append("</desc>"); 282 } 283 if (isRequired()) { 284 buf.append("<required/>"); 285 } 286 for (Iterator i = getValues(); i.hasNext();) { 288 buf.append("<value>").append(i.next()).append("</value>"); 289 } 290 for (Iterator i = getOptions(); i.hasNext();) { 292 buf.append(((Option)i.next()).toXML()); 293 } 294 buf.append("</field>"); 295 return buf.toString(); 296 } 297 298 304 public static class Option { 305 private String label; 306 private String value; 307 308 public Option(String value) { 309 this.value = value; 310 } 311 312 public Option(String label, String value) { 313 this.label = label; 314 this.value = value; 315 } 316 317 322 public String getLabel() { 323 return label; 324 } 325 326 331 public String getValue() { 332 return value; 333 } 334 335 public String toXML() { 336 StringBuffer buf = new StringBuffer (); 337 buf.append("<option"); 338 if (getLabel() != null) { 340 buf.append(" label=\"").append(getLabel()).append("\""); 341 } 342 buf.append(">"); 343 buf.append("<value>").append(getValue()).append("</value>"); 345 346 buf.append("</option>"); 347 return buf.toString(); 348 } 349 } 350 } 351 | Popular Tags |