All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jivesoftware.smackx.xdata.Form Maven / Gradle / Ivy

Go to download

Smack extensions. Classes and methods that implement support for the various XMPP XEPs (Multi-User Chat, PubSub, …) and other XMPP extensions.

There is a newer version: 4.5.0-beta5
Show newest version
/**
 *
 * Copyright 2003-2007 Jive Software.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jivesoftware.smackx.xdata;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import org.jivesoftware.smack.packet.Stanza;

import org.jivesoftware.smackx.xdata.packet.DataForm;

/**
 * Represents a Form for gathering data. The form could be of the following types:
 * 
    *
  • form → Indicates a form to fill out.
  • *
  • submit → The form is filled out, and this is the data that is being returned from * the form.
  • *
  • cancel → The form was cancelled. Tell the asker that piece of information.
  • *
  • result → Data results being returned from a search, or some other query.
  • *
* * Depending of the form's type different operations are available. For example, it's only possible * to set answers if the form is of type "submit". * * @see XEP-0004 Data Forms * * @author Gaston Dombiak */ public class Form { private DataForm dataForm; /** * Returns a new ReportedData if the stanza is used for gathering data and includes an * extension that matches the elementName and namespace "x","jabber:x:data". * * @param packet the stanza used for gathering data. * @return the data form parsed from the stanza or null if there was not * a form in the packet. */ public static Form getFormFrom(Stanza packet) { // Check if the packet includes the DataForm extension DataForm dataForm = DataForm.from(packet); if (dataForm != null) { if (dataForm.getReportedData() == null) return new Form(dataForm); } // Otherwise return null return null; } /** * Creates a new Form that will wrap an existing DataForm. The wrapped DataForm must be * used for gathering data. * * @param dataForm the data form used for gathering data. */ public Form(DataForm dataForm) { this.dataForm = dataForm; } /** * Creates a new Form of a given type from scratch. * * @param type the form's type (e.g. form, submit, cancel, result). */ public Form(DataForm.Type type) { this.dataForm = new DataForm(type); } /** * Adds a new field to complete as part of the form. * * @param field the field to complete. */ public void addField(FormField field) { dataForm.addField(field); } /** * 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.

* * 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, CharSequence value) { FormField field = getField(variable); if (field == null) { throw new IllegalArgumentException("Field not found for the specified variable name."); } switch (field.getType()) { case text_multi: case text_private: case text_single: case jid_single: case hidden: break; default: throw new IllegalArgumentException("This field is not of type String."); } setAnswer(field, value); } /** * Sets a new int 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. * * @param variable the variable name that was completed. * @param value the int 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, int value) { FormField field = getField(variable); if (field == null) { throw new IllegalArgumentException("Field not found for the specified variable name."); } validateThatFieldIsText(field); setAnswer(field, value); } /** * Sets a new long 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. * * @param variable the variable name that was completed. * @param value the long 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, long value) { FormField field = getField(variable); if (field == null) { throw new IllegalArgumentException("Field not found for the specified variable name."); } validateThatFieldIsText(field); setAnswer(field, value); } /** * Sets a new float 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. * * @param variable the variable name that was completed. * @param value the float 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, float value) { FormField field = getField(variable); if (field == null) { throw new IllegalArgumentException("Field not found for the specified variable name."); } validateThatFieldIsText(field); setAnswer(field, value); } /** * Sets a new double 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. * * @param variable the variable name that was completed. * @param value the double 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, double value) { FormField field = getField(variable); if (field == null) { throw new IllegalArgumentException("Field not found for the specified variable name."); } validateThatFieldIsText(field); setAnswer(field, value); } private static void validateThatFieldIsText(FormField field) { switch (field.getType()) { case text_multi: case text_private: case text_single: break; default: throw new IllegalArgumentException("This field is not of type text (multi, private or single)."); } } /** * Sets a new boolean 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. * * @param variable the variable name that was completed. * @param value the boolean 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, boolean value) { FormField field = getField(variable); if (field == null) { throw new IllegalArgumentException("Field not found for the specified variable name."); } if (field.getType() != FormField.Type.bool) { throw new IllegalArgumentException("This field is not of type boolean."); } setAnswer(field, Boolean.toString(value)); } /** * Sets a new Object value to a given form's field. In fact, the object representation * (i.e. #toString) will be the actual value of the field.

* * If the value to set to the field is not a basic type (e.g. String, boolean, int, etc.) you * will need to use {@link #setAnswer(String, String)} where the String value is the * String representation of the object.

* * Before setting the new value to the field we will check if the form is of type submit. If * the form isn't of type submit means that it's not possible to complete the form and an * exception will be thrown. * * @param field the form field that was completed. * @param value the Object value that was answered. The object representation will be the * actual value. * @throws IllegalStateException if the form is not of type "submit". */ private void setAnswer(FormField field, Object value) { if (!isSubmitType()) { throw new IllegalStateException("Cannot set an answer if the form is not of type " + "\"submit\""); } FormField filledOutfield = field.buildAnswer().addValue(value.toString()).build(); dataForm.replaceField(filledOutfield); } /** * 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.

* * 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, Collection values) { if (!isSubmitType()) { throw new IllegalStateException("Cannot set an answer if the form is not of type " + "\"submit\""); } FormField field = getField(variable); if (field != null) { // Check that the field can accept a collection of values switch (field.getType()) { case jid_multi: case list_multi: case list_single: case text_multi: case hidden: break; default: throw new IllegalArgumentException("This field only accept list of values."); } FormField filledOutfield = field.buildAnswer().addValues(values).build(); dataForm.replaceField(filledOutfield); } else { throw new IllegalArgumentException("Couldn't find a field for the specified variable."); } } /** * Sets the default value as the value of a given form's field. The field whose variable matches * the requested variable will be completed with its default value. If no field could be found * for the specified variable then an exception will be raised. * * @param variable the variable to complete with its default value. * @throws IllegalStateException if the form is not of type "submit". * @throws IllegalArgumentException if the form does not include the specified variable. */ public void setDefaultAnswer(String variable) { if (!isSubmitType()) { throw new IllegalStateException("Cannot set an answer if the form is not of type " + "\"submit\""); } FormField field = getField(variable); if (field != null) { FormField.Builder filledOutFormFieldBuilder = field.buildAnswer(); // Set the default value for (CharSequence value : field.getValues()) { filledOutFormFieldBuilder.addValue(value); } dataForm.replaceField(filledOutFormFieldBuilder.build()); } else { throw new IllegalArgumentException("Couldn't find a field for the specified variable."); } } /** * Returns a List of the fields that are part of the form. * * @return a List of the fields that are part of the form. */ public List getFields() { return dataForm.getFields(); } /** * Returns the field of the form whose variable matches the specified variable. * The fields of type FIXED will never be returned since they do not specify a * variable. * * @param variable the variable to look for in the form fields. * @return the field of the form whose variable matches the specified variable. */ public FormField getField(String variable) { return dataForm.getField(variable); } /** * Check if a field with the given variable exists. * * @param variable the variable to check for. * @return true if a field with the variable exists, false otherwise. * @since 4.2 */ public boolean hasField(String variable) { return dataForm.hasField(variable); } /** * Returns the instructions that explain how to fill out the form and what the form is about. * * @return instructions that explain how to fill out the form. */ public String getInstructions() { StringBuilder sb = new StringBuilder(); // Join the list of instructions together separated by newlines for (Iterator it = dataForm.getInstructions().iterator(); it.hasNext();) { sb.append(it.next()); // If this is not the last instruction then append a newline if (it.hasNext()) { sb.append('\n'); } } return sb.toString(); } /** * Returns the description of the data. It is similar to the title on a web page or an X * window. You can put a title on either a form to fill out, or a set of data results. * * @return description of the data. */ public String getTitle() { return dataForm.getTitle(); } /** * Returns the meaning of the data within the context. The data could be part of a form * to fill out, a form submission or data results. * * @return the form's type. */ public DataForm.Type getType() { return dataForm.getType(); } /** * Sets instructions that explain how to fill out the form and what the form is about. * * @param instructions instructions that explain how to fill out the form. */ public void setInstructions(String instructions) { // Split the instructions into multiple instructions for each existent newline ArrayList instructionsList = new ArrayList<>(); StringTokenizer st = new StringTokenizer(instructions, "\n"); while (st.hasMoreTokens()) { instructionsList.add(st.nextToken()); } // Set the new list of instructions dataForm.setInstructions(instructionsList); } /** * Sets the description of the data. It is similar to the title on a web page or an X window. * You can put a title on either a form to fill out, or a set of data results. * * @param title description of the data. */ public void setTitle(String title) { dataForm.setTitle(title); } /** * Returns a DataForm that serves to send this Form to the server. If the form is of type * submit, it may contain fields with no value. These fields will be removed since they only * exist to assist the user while editing/completing the form in a UI. * * @return the wrapped DataForm. */ public DataForm getDataFormToSend() { if (isSubmitType()) { // Create a new DataForm that contains only the answered fields DataForm dataFormToSend = new DataForm(getType()); for (FormField field : getFields()) { if (!field.getValues().isEmpty()) { dataFormToSend.addField(field); } } return dataFormToSend; } return dataForm; } /** * Returns true if the form is a form to fill out. * * @return if the form is a form to fill out. */ private boolean isFormType() { return DataForm.Type.form == dataForm.getType(); } /** * Returns true if the form is a form to submit. * * @return if the form is a form to submit. */ private boolean isSubmitType() { return DataForm.Type.submit == dataForm.getType(); } /** * 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.

* * 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 Form form = new Form(DataForm.Type.submit); for (FormField field : getFields()) { // 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) { FormField.Builder newField = FormField.builder(field.getVariable()); newField.setType(field.getType()); form.addField(newField.build()); // Set the answer ONLY to the hidden fields if (field.getType() == FormField.Type.hidden) { // Since a hidden field could have many values we need to collect them // in a list List values = new ArrayList<>(); values.addAll(field.getValues()); form.setAnswer(field.getVariable(), values); } } } return form; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy