1 20 21 package org.jivesoftware.smackx; 22 23 import java.util.*; 24 25 import org.jivesoftware.smack.packet.*; 26 import org.jivesoftware.smackx.packet.DataForm; 27 28 43 public class Form { 44 45 public static final String TYPE_FORM = "form"; 46 public static final String TYPE_SUBMIT = "submit"; 47 public static final String TYPE_CANCEL = "cancel"; 48 public static final String TYPE_RESULT = "result"; 49 50 private DataForm dataForm; 51 52 58 public static Form getFormFrom(Packet packet) { 59 PacketExtension packetExtension = packet.getExtension("x","jabber:x:data"); 61 if (packetExtension != null) { 62 DataForm dataForm = (DataForm) packetExtension; 64 if (dataForm.getReportedData() == null) 65 return new Form(dataForm); 66 } 67 return null; 69 } 70 71 77 private Form(DataForm dataForm) { 78 this.dataForm = dataForm; 79 } 80 81 95 public Form(String type) { 96 this.dataForm = new DataForm(type); 97 } 98 99 104 public void addField(FormField field) { 105 dataForm.addField(field); 106 } 107 108 122 public void setAnswer(String variable, String value) { 123 FormField field = getField(variable); 124 if (field == null) { 125 throw new IllegalArgumentException ("Field not found for the specified variable name."); 126 } 127 if (!FormField.TYPE_TEXT_MULTI.equals(field.getType()) 128 && !FormField.TYPE_TEXT_PRIVATE.equals(field.getType()) 129 && !FormField.TYPE_TEXT_SINGLE.equals(field.getType()) 130 && !FormField.TYPE_JID_SINGLE.equals(field.getType()) 131 && !FormField.TYPE_HIDDEN.equals(field.getType())) { 132 throw new IllegalArgumentException ("This field is not of type String."); 133 } 134 setAnswer(field, value); 135 } 136 137 148 public void setAnswer(String variable, int value) { 149 FormField field = getField(variable); 150 if (field == null) { 151 throw new IllegalArgumentException ("Field not found for the specified variable name."); 152 } 153 if (!FormField.TYPE_TEXT_MULTI.equals(field.getType()) 154 && !FormField.TYPE_TEXT_PRIVATE.equals(field.getType()) 155 && !FormField.TYPE_TEXT_SINGLE.equals(field.getType())) { 156 throw new IllegalArgumentException ("This field is not of type int."); 157 } 158 setAnswer(field, new Integer (value)); 159 } 160 161 172 public void setAnswer(String variable, long value) { 173 FormField field = getField(variable); 174 if (field == null) { 175 throw new IllegalArgumentException ("Field not found for the specified variable name."); 176 } 177 if (!FormField.TYPE_TEXT_MULTI.equals(field.getType()) 178 && !FormField.TYPE_TEXT_PRIVATE.equals(field.getType()) 179 && !FormField.TYPE_TEXT_SINGLE.equals(field.getType())) { 180 throw new IllegalArgumentException ("This field is not of type long."); 181 } 182 setAnswer(field, new Long (value)); 183 } 184 185 196 public void setAnswer(String variable, float value) { 197 FormField field = getField(variable); 198 if (field == null) { 199 throw new IllegalArgumentException ("Field not found for the specified variable name."); 200 } 201 if (!FormField.TYPE_TEXT_MULTI.equals(field.getType()) 202 && !FormField.TYPE_TEXT_PRIVATE.equals(field.getType()) 203 && !FormField.TYPE_TEXT_SINGLE.equals(field.getType())) { 204 throw new IllegalArgumentException ("This field is not of type float."); 205 } 206 setAnswer(field, new Float (value)); 207 } 208 209 220 public void setAnswer(String variable, double value) { 221 FormField field = getField(variable); 222 if (field == null) { 223 throw new IllegalArgumentException ("Field not found for the specified variable name."); 224 } 225 if (!FormField.TYPE_TEXT_MULTI.equals(field.getType()) 226 && !FormField.TYPE_TEXT_PRIVATE.equals(field.getType()) 227 && !FormField.TYPE_TEXT_SINGLE.equals(field.getType())) { 228 throw new IllegalArgumentException ("This field is not of type double."); 229 } 230 setAnswer(field, new Double (value)); 231 } 232 233 244 public void setAnswer(String variable, boolean value) { 245 FormField field = getField(variable); 246 if (field == null) { 247 throw new IllegalArgumentException ("Field not found for the specified variable name."); 248 } 249 if (!FormField.TYPE_BOOLEAN.equals(field.getType())) { 250 throw new IllegalArgumentException ("This field is not of type boolean."); 251 } 252 setAnswer(field, (value ? "1" : "0")); 253 } 254 255 272 private void setAnswer(FormField field, Object value) { 273 if (!isSubmitType()) { 274 throw new IllegalStateException ("Cannot set an answer if the form is not of type " + 275 "\"submit\""); 276 } 277 field.resetValues(); 278 field.addValue(value.toString()); 279 } 280 281 294 public void setAnswer(String variable, List values) { 295 if (!isSubmitType()) { 296 throw new IllegalStateException ("Cannot set an answer if the form is not of type " + 297 "\"submit\""); 298 } 299 FormField field = getField(variable); 300 if (field != null) { 301 if (!FormField.TYPE_JID_MULTI.equals(field.getType()) 303 && !FormField.TYPE_LIST_MULTI.equals(field.getType()) 304 && !FormField.TYPE_LIST_SINGLE.equals(field.getType()) 305 && !FormField.TYPE_HIDDEN.equals(field.getType())) { 306 throw new IllegalArgumentException ("This field only accept list of values."); 307 } 308 field.resetValues(); 310 field.addValues(values); 312 } 313 else { 314 throw new IllegalArgumentException ("Couldn't find a field for the specified variable."); 315 } 316 } 317 318 327 public void setDefaultAnswer(String variable) { 328 if (!isSubmitType()) { 329 throw new IllegalStateException ("Cannot set an answer if the form is not of type " + 330 "\"submit\""); 331 } 332 FormField field = getField(variable); 333 if (field != null) { 334 field.resetValues(); 336 for (Iterator it = field.getValues(); it.hasNext();) { 338 field.addValue((String ) it.next()); 339 } 340 } 341 else { 342 throw new IllegalArgumentException ("Couldn't find a field for the specified variable."); 343 } 344 } 345 346 351 public Iterator getFields() { 352 return dataForm.getFields(); 353 } 354 355 363 public FormField getField(String variable) { 364 if (variable == null || variable.equals("")) { 365 throw new IllegalArgumentException ("Variable must not be null or blank."); 366 } 367 FormField field; 369 for (Iterator it=getFields();it.hasNext();) { 370 field = (FormField)it.next(); 371 if (variable.equals(field.getVariable())) { 372 return field; 373 } 374 } 375 return null; 376 } 377 378 383 public String getInstructions() { 384 StringBuffer sb = new StringBuffer (); 385 for (Iterator it = dataForm.getInstructions(); it.hasNext();) { 387 sb.append((String ) it.next()); 388 if (it.hasNext()) { 390 sb.append("\n"); 391 } 392 } 393 return sb.toString(); 394 } 395 396 397 403 public String getTitle() { 404 return dataForm.getTitle(); 405 } 406 407 408 423 public String getType() { 424 return dataForm.getType(); 425 } 426 427 428 433 public void setInstructions(String instructions) { 434 ArrayList instructionsList = new ArrayList(); 436 StringTokenizer st = new StringTokenizer(instructions, "\n"); 437 while (st.hasMoreTokens()) { 438 instructionsList.add(st.nextToken()); 439 } 440 dataForm.setInstructions(instructionsList); 442 443 } 444 445 446 452 public void setTitle(String title) { 453 dataForm.setTitle(title); 454 } 455 456 463 public DataForm getDataFormToSend() { 464 if (isSubmitType()) { 465 DataForm dataFormToSend = new DataForm(getType()); 467 for(Iterator it=getFields();it.hasNext();) { 468 FormField field = (FormField)it.next(); 469 if (field.getValues().hasNext()) { 470 dataFormToSend.addField(field); 471 } 472 } 473 return dataFormToSend; 474 } 475 return dataForm; 476 } 477 478 483 private boolean isFormType() { 484 return TYPE_FORM.equals(dataForm.getType()); 485 } 486 487 492 private boolean isSubmitType() { 493 return TYPE_SUBMIT.equals(dataForm.getType()); 494 } 495 496 510 public Form createAnswerForm() { 511 if (!isFormType()) { 512 throw new IllegalStateException ("Only forms of type \"form\" could be answered"); 513 } 514 Form form = new Form(TYPE_SUBMIT); 516 for (Iterator fields=getFields(); fields.hasNext();) { 517 FormField field = (FormField)fields.next(); 518 if (field.getVariable() != null) { 521 FormField newField = new FormField(field.getVariable()); 522 newField.setType(field.getType()); 523 form.addField(newField); 524 if (FormField.TYPE_HIDDEN.equals(field.getType())) { 526 List values = new ArrayList(); 529 for (Iterator it=field.getValues();it.hasNext();) { 530 values.add((String )it.next()); 531 } 532 form.setAnswer(field.getVariable(), values); 533 } 534 } 535 } 536 return form; 537 } 538 539 } 540 | Popular Tags |