Пример #1
0
 /**
  * Returns a new Form to submit the completed values. The new Form will include all the fields of
  * the original form except for the fields of type FIXED. Only the HIDDEN fields will include the
  * same value of the original form. The other fields of the new form MUST be completed. If a field
  * remains with no answer when sending the completed form, then it won't be included as part of
  * the completed form.
  *
  * <p>The reason why the fields with variables are included in the new form is to provide a model
  * for binding with any UI. This means that the UIs will use the original form (of type "form") to
  * learn how to render the form, but the UIs will bind the fields to the form of type submit.
  *
  * @return a Form to submit the completed values.
  */
 public Form createAnswerForm() {
   if (!isFormType()) {
     throw new IllegalStateException("Only forms of type \"form\" could be answered");
   }
   // Create a new Form
   final Form form = new Form(TYPE_SUBMIT);
   for (final Iterator<FormField> fields = getFields(); fields.hasNext(); ) {
     final FormField field = fields.next();
     // Add to the new form any type of field that includes a variable.
     // Note: The fields of type FIXED are the only ones that don't
     // specify a variable
     if (field.getVariable() != null) {
       final FormField newField = new FormField(field.getVariable());
       newField.setType(field.getType());
       form.addField(newField);
       // Set the answer ONLY to the hidden fields
       if (FormField.TYPE_HIDDEN.equals(field.getType())) {
         // Since a hidden field could have many values we need to
         // collect them
         // in a list
         final List<String> values = new ArrayList<String>();
         for (final Iterator<String> it = field.getValues(); it.hasNext(); ) {
           values.add(it.next());
         }
         form.setAnswer(field.getVariable(), values);
       }
     }
   }
   return form;
 }
Пример #2
0
 /**
  * Sets a new String value to a given form's field. The field whose variable matches the requested
  * variable will be completed with the specified value. If no field could be found for the
  * specified variable then an exception will be raised.
  *
  * <p>If the value to set to the field is not a basic type (e.g. String, boolean, int, etc.) you
  * can use this message where the String value is the String representation of the object.
  *
  * @param variable the variable name that was completed.
  * @param value the String value that was answered.
  * @throws IllegalStateException if the form is not of type "submit".
  * @throws IllegalArgumentException if the form does not include the specified variable or if the
  *     answer type does not correspond with the field type..
  */
 public void setAnswer(String variable, String value) {
   final FormField field = getField(variable);
   if (field == null) {
     throw new IllegalArgumentException("Field not found for the specified variable name.");
   }
   if (!FormField.TYPE_TEXT_MULTI.equals(field.getType())
       && !FormField.TYPE_TEXT_PRIVATE.equals(field.getType())
       && !FormField.TYPE_TEXT_SINGLE.equals(field.getType())
       && !FormField.TYPE_JID_SINGLE.equals(field.getType())
       && !FormField.TYPE_HIDDEN.equals(field.getType())) {
     throw new IllegalArgumentException("This field is not of type String.");
   }
   setAnswer(field, value);
 }
Пример #3
0
 /**
  * Sets a new values to a given form's field. The field whose variable matches the requested
  * variable will be completed with the specified values. If no field could be found for the
  * specified variable then an exception will be raised.
  *
  * <p>The Objects contained in the List could be of any type. The String representation of them
  * (i.e. #toString) will be actually used when sending the answer to the server.
  *
  * @param variable the variable that was completed.
  * @param values the values that were answered.
  * @throws IllegalStateException if the form is not of type "submit".
  * @throws IllegalArgumentException if the form does not include the specified variable.
  */
 public void setAnswer(String variable, List<String> values) {
   if (!isSubmitType()) {
     throw new IllegalStateException(
         "Cannot set an answer if the form is not of type " + "\"submit\"");
   }
   final FormField field = getField(variable);
   if (field != null) {
     // Check that the field can accept a collection of values
     if (!FormField.TYPE_JID_MULTI.equals(field.getType())
         && !FormField.TYPE_LIST_MULTI.equals(field.getType())
         && !FormField.TYPE_LIST_SINGLE.equals(field.getType())
         && !FormField.TYPE_HIDDEN.equals(field.getType())) {
       throw new IllegalArgumentException("This field only accept list of values.");
     }
     // Clear the old values
     field.resetValues();
     // Set the new values. The string representation of each value will
     // be actually used.
     field.addValues(values);
   } else {
     throw new IllegalArgumentException("Couldn't find a field for the specified variable.");
   }
 }