mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-12-12 22:11:07 +01:00
Prefix subprojects with 'smack-'
instead of using the old baseName=smack appendix=project.name approach, we are now going convention over configuration and renaming the subprojects directories to the proper name. Having a prefix is actually very helpful, because the resulting libraries will be named like the subproject. And a core-4.0.0-rc1.jar is not as explicit about what it actually *is* as a smack-core-4.0.0-rc1.jar. SMACK-265
This commit is contained in:
parent
b6fb1f3743
commit
91fd15ad86
758 changed files with 42 additions and 42 deletions
|
|
@ -0,0 +1,543 @@
|
|||
/**
|
||||
*
|
||||
* 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.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.PacketExtension;
|
||||
import org.jivesoftware.smackx.xdata.packet.DataForm;
|
||||
|
||||
/**
|
||||
* Represents a Form for gathering data. The form could be of the following types:
|
||||
* <ul>
|
||||
* <li>form -> Indicates a form to fill out.</li>
|
||||
* <li>submit -> The form is filled out, and this is the data that is being returned from
|
||||
* the form.</li>
|
||||
* <li>cancel -> The form was cancelled. Tell the asker that piece of information.</li>
|
||||
* <li>result -> Data results being returned from a search, or some other query.</li>
|
||||
* </ul>
|
||||
*
|
||||
* 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 <a href="http://xmpp.org/extensions/xep-0004.html">XEP-0004 Data Forms</a>
|
||||
*
|
||||
* @author Gaston Dombiak
|
||||
*/
|
||||
public class Form {
|
||||
|
||||
public static final String TYPE_FORM = "form";
|
||||
public static final String TYPE_SUBMIT = "submit";
|
||||
public static final String TYPE_CANCEL = "cancel";
|
||||
public static final String TYPE_RESULT = "result";
|
||||
|
||||
public static final String NAMESPACE = "jabber:x:data";
|
||||
public static final String ELEMENT = "x";
|
||||
|
||||
private DataForm dataForm;
|
||||
|
||||
/**
|
||||
* Returns a new ReportedData if the packet is used for gathering data and includes an
|
||||
* extension that matches the elementName and namespace "x","jabber:x:data".
|
||||
*
|
||||
* @param packet the packet used for gathering data.
|
||||
* @return the data form parsed from the packet or <tt>null</tt> if there was not
|
||||
* a form in the packet.
|
||||
*/
|
||||
public static Form getFormFrom(Packet packet) {
|
||||
// Check if the packet includes the DataForm extension
|
||||
PacketExtension packetExtension = packet.getExtension("x","jabber:x:data");
|
||||
if (packetExtension != null) {
|
||||
// Check if the existing DataForm is not a result of a search
|
||||
DataForm dataForm = (DataForm) packetExtension;
|
||||
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.<p>
|
||||
*
|
||||
* Possible form types are:
|
||||
* <ul>
|
||||
* <li>form -> Indicates a form to fill out.</li>
|
||||
* <li>submit -> The form is filled out, and this is the data that is being returned from
|
||||
* the form.</li>
|
||||
* <li>cancel -> The form was cancelled. Tell the asker that piece of information.</li>
|
||||
* <li>result -> Data results being returned from a search, or some other query.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param type the form's type (e.g. form, submit,cancel,result).
|
||||
*/
|
||||
public Form(String 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.<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) {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.");
|
||||
}
|
||||
if (!FormField.TYPE_TEXT_MULTI.equals(field.getType())
|
||||
&& !FormField.TYPE_TEXT_PRIVATE.equals(field.getType())
|
||||
&& !FormField.TYPE_TEXT_SINGLE.equals(field.getType())) {
|
||||
throw new IllegalArgumentException("This field is not of type int.");
|
||||
}
|
||||
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.");
|
||||
}
|
||||
if (!FormField.TYPE_TEXT_MULTI.equals(field.getType())
|
||||
&& !FormField.TYPE_TEXT_PRIVATE.equals(field.getType())
|
||||
&& !FormField.TYPE_TEXT_SINGLE.equals(field.getType())) {
|
||||
throw new IllegalArgumentException("This field is not of type long.");
|
||||
}
|
||||
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.");
|
||||
}
|
||||
if (!FormField.TYPE_TEXT_MULTI.equals(field.getType())
|
||||
&& !FormField.TYPE_TEXT_PRIVATE.equals(field.getType())
|
||||
&& !FormField.TYPE_TEXT_SINGLE.equals(field.getType())) {
|
||||
throw new IllegalArgumentException("This field is not of type float.");
|
||||
}
|
||||
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.");
|
||||
}
|
||||
if (!FormField.TYPE_TEXT_MULTI.equals(field.getType())
|
||||
&& !FormField.TYPE_TEXT_PRIVATE.equals(field.getType())
|
||||
&& !FormField.TYPE_TEXT_SINGLE.equals(field.getType())) {
|
||||
throw new IllegalArgumentException("This field is not of type double.");
|
||||
}
|
||||
setAnswer(field, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 (!FormField.TYPE_BOOLEAN.equals(field.getType())) {
|
||||
throw new IllegalArgumentException("This field is not of type boolean.");
|
||||
}
|
||||
setAnswer(field, (value ? "1" : "0"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.<p>
|
||||
*
|
||||
* 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.<p>
|
||||
*
|
||||
* 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\"");
|
||||
}
|
||||
field.resetValues();
|
||||
field.addValue(value.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 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\"");
|
||||
}
|
||||
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_TEXT_MULTI.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.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
// Clear the old values
|
||||
field.resetValues();
|
||||
// Set the default value
|
||||
for (String value : field.getValues()) {
|
||||
field.addValue(value);
|
||||
}
|
||||
}
|
||||
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<FormField> 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) {
|
||||
if (variable == null || variable.equals("")) {
|
||||
throw new IllegalArgumentException("Variable must not be null or blank.");
|
||||
}
|
||||
// Look for the field whose variable matches the requested variable
|
||||
for (FormField field : getFields()) {
|
||||
if (variable.equals(field.getVariable())) {
|
||||
return field;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<String> 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.<p>
|
||||
*
|
||||
* Possible form types are:
|
||||
* <ul>
|
||||
* <li>form -> Indicates a form to fill out.</li>
|
||||
* <li>submit -> The form is filled out, and this is the data that is being returned from
|
||||
* the form.</li>
|
||||
* <li>cancel -> The form was cancelled. Tell the asker that piece of information.</li>
|
||||
* <li>result -> Data results being returned from a search, or some other query.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return the form's type.
|
||||
*/
|
||||
public String 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<String> instructionsList = new ArrayList<String>();
|
||||
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 TYPE_FORM.equals(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 TYPE_SUBMIT.equals(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.<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
|
||||
Form form = new Form(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 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
|
||||
List<String> values = new ArrayList<String>();
|
||||
for (String value : field.getValues()) {
|
||||
values.add(value);
|
||||
}
|
||||
form.setAnswer(field.getVariable(), values);
|
||||
}
|
||||
}
|
||||
}
|
||||
return form;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,404 @@
|
|||
/**
|
||||
*
|
||||
* 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 org.jivesoftware.smack.util.StringUtils;
|
||||
import org.jivesoftware.smack.util.XmlStringBuilder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents a field of a form. The field could be used to represent a question to complete,
|
||||
* a completed question or a data returned from a search. The exact interpretation of the field
|
||||
* depends on the context where the field is used.
|
||||
*
|
||||
* @author Gaston Dombiak
|
||||
*/
|
||||
public class FormField {
|
||||
|
||||
public static final String TYPE_BOOLEAN = "boolean";
|
||||
public static final String TYPE_FIXED = "fixed";
|
||||
public static final String TYPE_HIDDEN = "hidden";
|
||||
public static final String TYPE_JID_MULTI = "jid-multi";
|
||||
public static final String TYPE_JID_SINGLE = "jid-single";
|
||||
public static final String TYPE_LIST_MULTI = "list-multi";
|
||||
public static final String TYPE_LIST_SINGLE = "list-single";
|
||||
public static final String TYPE_TEXT_MULTI = "text-multi";
|
||||
public static final String TYPE_TEXT_PRIVATE = "text-private";
|
||||
public static final String TYPE_TEXT_SINGLE = "text-single";
|
||||
|
||||
private String description;
|
||||
private boolean required = false;
|
||||
private String label;
|
||||
private String variable;
|
||||
private String type;
|
||||
private final List<Option> options = new ArrayList<Option>();
|
||||
private final List<String> values = new ArrayList<String>();
|
||||
|
||||
/**
|
||||
* Creates a new FormField with the variable name that uniquely identifies the field
|
||||
* in the context of the form.
|
||||
*
|
||||
* @param variable the variable name of the question.
|
||||
*/
|
||||
public FormField(String variable) {
|
||||
this.variable = variable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new FormField of type FIXED. The fields of type FIXED do not define a variable
|
||||
* name.
|
||||
*/
|
||||
public FormField() {
|
||||
this.type = FormField.TYPE_FIXED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a description that provides extra clarification about the question. This information
|
||||
* could be presented to the user either in tool-tip, help button, or as a section of text
|
||||
* before the question.<p>
|
||||
* <p/>
|
||||
* If the question is of type FIXED then the description should remain empty.
|
||||
*
|
||||
* @return description that provides extra clarification about the question.
|
||||
*/
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the label of the question which should give enough information to the user to
|
||||
* fill out the form.
|
||||
*
|
||||
* @return label of the question.
|
||||
*/
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a List of the available options that the user has in order to answer
|
||||
* the question.
|
||||
*
|
||||
* @return List of the available options.
|
||||
*/
|
||||
public List<Option> getOptions() {
|
||||
synchronized (options) {
|
||||
return Collections.unmodifiableList(new ArrayList<Option>(options));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the question must be answered in order to complete the questionnaire.
|
||||
*
|
||||
* @return true if the question must be answered in order to complete the questionnaire.
|
||||
*/
|
||||
public boolean isRequired() {
|
||||
return required;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an indicative of the format for the data to answer. Valid formats are:
|
||||
* <p/>
|
||||
* <ul>
|
||||
* <li>text-single -> single line or word of text
|
||||
* <li>text-private -> instead of showing the user what they typed, you show ***** to
|
||||
* protect it
|
||||
* <li>text-multi -> multiple lines of text entry
|
||||
* <li>list-single -> given a list of choices, pick one
|
||||
* <li>list-multi -> given a list of choices, pick one or more
|
||||
* <li>boolean -> 0 or 1, true or false, yes or no. Default value is 0
|
||||
* <li>fixed -> fixed for putting in text to show sections, or just advertise your web
|
||||
* site in the middle of the form
|
||||
* <li>hidden -> is not given to the user at all, but returned with the questionnaire
|
||||
* <li>jid-single -> Jabber ID - choosing a JID from your roster, and entering one based
|
||||
* on the rules for a JID.
|
||||
* <li>jid-multi -> multiple entries for JIDs
|
||||
* </ul>
|
||||
*
|
||||
* @return format for the data to answer.
|
||||
*/
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a List of the default values of the question if the question is part
|
||||
* of a form to fill out. Otherwise, returns a List of the answered values of
|
||||
* the question.
|
||||
*
|
||||
* @return a List of the default values or answered values of the question.
|
||||
*/
|
||||
public List<String> getValues() {
|
||||
synchronized (values) {
|
||||
return Collections.unmodifiableList(new ArrayList<String>(values));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the variable name that the question is filling out.
|
||||
*
|
||||
* @return the variable name of the question.
|
||||
*/
|
||||
public String getVariable() {
|
||||
return variable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a description that provides extra clarification about the question. This information
|
||||
* could be presented to the user either in tool-tip, help button, or as a section of text
|
||||
* before the question.<p>
|
||||
* <p/>
|
||||
* If the question is of type FIXED then the description should remain empty.
|
||||
*
|
||||
* @param description provides extra clarification about the question.
|
||||
*/
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the label of the question which should give enough information to the user to
|
||||
* fill out the form.
|
||||
*
|
||||
* @param label the label of the question.
|
||||
*/
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets if the question must be answered in order to complete the questionnaire.
|
||||
*
|
||||
* @param required if the question must be answered in order to complete the questionnaire.
|
||||
*/
|
||||
public void setRequired(boolean required) {
|
||||
this.required = required;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an indicative of the format for the data to answer. Valid formats are:
|
||||
* <p/>
|
||||
* <ul>
|
||||
* <li>text-single -> single line or word of text
|
||||
* <li>text-private -> instead of showing the user what they typed, you show ***** to
|
||||
* protect it
|
||||
* <li>text-multi -> multiple lines of text entry
|
||||
* <li>list-single -> given a list of choices, pick one
|
||||
* <li>list-multi -> given a list of choices, pick one or more
|
||||
* <li>boolean -> 0 or 1, true or false, yes or no. Default value is 0
|
||||
* <li>fixed -> fixed for putting in text to show sections, or just advertise your web
|
||||
* site in the middle of the form
|
||||
* <li>hidden -> is not given to the user at all, but returned with the questionnaire
|
||||
* <li>jid-single -> Jabber ID - choosing a JID from your roster, and entering one based
|
||||
* on the rules for a JID.
|
||||
* <li>jid-multi -> multiple entries for JIDs
|
||||
* </ul>
|
||||
*
|
||||
* @param type an indicative of the format for the data to answer.
|
||||
*/
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a default value to the question if the question is part of a form to fill out.
|
||||
* Otherwise, adds an answered value to the question.
|
||||
*
|
||||
* @param value a default value or an answered value of the question.
|
||||
*/
|
||||
public void addValue(String value) {
|
||||
synchronized (values) {
|
||||
values.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a default values to the question if the question is part of a form to fill out.
|
||||
* Otherwise, adds an answered values to the question.
|
||||
*
|
||||
* @param newValues default values or an answered values of the question.
|
||||
*/
|
||||
public void addValues(List<String> newValues) {
|
||||
synchronized (values) {
|
||||
values.addAll(newValues);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all the values of the field.
|
||||
*/
|
||||
protected void resetValues() {
|
||||
synchronized (values) {
|
||||
values.removeAll(new ArrayList<String>(values));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adss an available options to the question that the user has in order to answer
|
||||
* the question.
|
||||
*
|
||||
* @param option a new available option for the question.
|
||||
*/
|
||||
public void addOption(Option option) {
|
||||
synchronized (options) {
|
||||
options.add(option);
|
||||
}
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
XmlStringBuilder buf = new XmlStringBuilder();
|
||||
buf.append("<field");
|
||||
// Add attributes
|
||||
if (getLabel() != null) {
|
||||
buf.append(" label=\"").append(getLabel()).append("\"");
|
||||
}
|
||||
buf.attribute("var", getVariable());
|
||||
if (getType() != null) {
|
||||
buf.append(" type=\"").append(getType()).append("\"");
|
||||
}
|
||||
buf.append(">");
|
||||
// Add elements
|
||||
if (getDescription() != null) {
|
||||
buf.append("<desc>").append(getDescription()).append("</desc>");
|
||||
}
|
||||
if (isRequired()) {
|
||||
buf.append("<required/>");
|
||||
}
|
||||
// Loop through all the values and append them to the string buffer
|
||||
for (String value : getValues()) {
|
||||
buf.element("value", value);
|
||||
}
|
||||
// Loop through all the values and append them to the string buffer
|
||||
for (Option option : getOptions()) {
|
||||
buf.append(option.toXML());
|
||||
}
|
||||
buf.append("</field>");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (obj == this)
|
||||
return true;
|
||||
if (!(obj instanceof FormField))
|
||||
return false;
|
||||
|
||||
FormField other = (FormField) obj;
|
||||
|
||||
return toXML().equals(other.toXML());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return toXML().hashCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the available option of a given FormField.
|
||||
*
|
||||
* @author Gaston Dombiak
|
||||
*/
|
||||
public static class Option {
|
||||
|
||||
private String label;
|
||||
private String value;
|
||||
|
||||
public Option(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public Option(String label, String value) {
|
||||
this.label = label;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the label that represents the option.
|
||||
*
|
||||
* @return the label that represents the option.
|
||||
*/
|
||||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value of the option.
|
||||
*
|
||||
* @return the value of the option.
|
||||
*/
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getLabel();
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<option");
|
||||
// Add attribute
|
||||
if (getLabel() != null) {
|
||||
buf.append(" label=\"").append(getLabel()).append("\"");
|
||||
}
|
||||
buf.append(">");
|
||||
// Add element
|
||||
buf.append("<value>").append(StringUtils.escapeForXML(getValue())).append("</value>");
|
||||
|
||||
buf.append("</option>");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (obj == this)
|
||||
return true;
|
||||
if (obj.getClass() != getClass())
|
||||
return false;
|
||||
|
||||
Option other = (Option) obj;
|
||||
|
||||
if (!value.equals(other.value))
|
||||
return false;
|
||||
|
||||
String thisLabel = label == null ? "" : label;
|
||||
String otherLabel = other.label == null ? "" : other.label;
|
||||
|
||||
if (!thisLabel.equals(otherLabel))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = 1;
|
||||
result = 37 * result + value.hashCode();
|
||||
result = 37 * result + (label == null ? 0 : label.hashCode());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,304 @@
|
|||
/**
|
||||
*
|
||||
* 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.packet;
|
||||
|
||||
import org.jivesoftware.smack.packet.PacketExtension;
|
||||
import org.jivesoftware.smackx.xdata.Form;
|
||||
import org.jivesoftware.smackx.xdata.FormField;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents a form that could be use for gathering data as well as for reporting data
|
||||
* returned from a search.
|
||||
*
|
||||
* @author Gaston Dombiak
|
||||
*/
|
||||
public class DataForm implements PacketExtension {
|
||||
|
||||
private String type;
|
||||
private String title;
|
||||
private List<String> instructions = new ArrayList<String>();
|
||||
private ReportedData reportedData;
|
||||
private final List<Item> items = new ArrayList<Item>();
|
||||
private final List<FormField> fields = new ArrayList<FormField>();
|
||||
|
||||
public DataForm(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.<p>
|
||||
*
|
||||
* Possible form types are:
|
||||
* <ul>
|
||||
* <li>form -> This packet contains a form to fill out. Display it to the user (if your
|
||||
* program can).</li>
|
||||
* <li>submit -> The form is filled out, and this is the data that is being returned from
|
||||
* the form.</li>
|
||||
* <li>cancel -> The form was cancelled. Tell the asker that piece of information.</li>
|
||||
* <li>result -> Data results being returned from a search, or some other query.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @return the form's type.
|
||||
*/
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a List of the list of instructions that explain how to fill out the form and
|
||||
* what the form is about. The dataform could include multiple instructions since each
|
||||
* instruction could not contain newlines characters. Join the instructions together in order
|
||||
* to show them to the user.
|
||||
*
|
||||
* @return a List of the list of instructions that explain how to fill out the form.
|
||||
*/
|
||||
public List<String> getInstructions() {
|
||||
synchronized (instructions) {
|
||||
return Collections.unmodifiableList(new ArrayList<String>(instructions));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the fields that will be returned from a search.
|
||||
*
|
||||
* @return fields that will be returned from a search.
|
||||
*/
|
||||
public ReportedData getReportedData() {
|
||||
return reportedData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a List of the items returned from a search.
|
||||
*
|
||||
* @return a List of the items returned from a search.
|
||||
*/
|
||||
public List<Item> getItems() {
|
||||
synchronized (items) {
|
||||
return Collections.unmodifiableList(new ArrayList<Item>(items));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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<FormField> getFields() {
|
||||
synchronized (fields) {
|
||||
return Collections.unmodifiableList(new ArrayList<FormField>(fields));
|
||||
}
|
||||
}
|
||||
|
||||
public String getElementName() {
|
||||
return Form.ELEMENT;
|
||||
}
|
||||
|
||||
public String getNamespace() {
|
||||
return Form.NAMESPACE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the list of instructions that explain how to fill out the form and what the form is
|
||||
* about. The dataform could include multiple instructions since each instruction could not
|
||||
* contain newlines characters.
|
||||
*
|
||||
* @param instructions list of instructions that explain how to fill out the form.
|
||||
*/
|
||||
public void setInstructions(List<String> instructions) {
|
||||
this.instructions = instructions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the fields that will be returned from a search.
|
||||
*
|
||||
* @param reportedData the fields that will be returned from a search.
|
||||
*/
|
||||
public void setReportedData(ReportedData reportedData) {
|
||||
this.reportedData = reportedData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new field as part of the form.
|
||||
*
|
||||
* @param field the field to add to the form.
|
||||
*/
|
||||
public void addField(FormField field) {
|
||||
synchronized (fields) {
|
||||
fields.add(field);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new instruction to the list of instructions that explain how to fill out the form
|
||||
* and what the form is about. The dataform could include multiple instructions since each
|
||||
* instruction could not contain newlines characters.
|
||||
*
|
||||
* @param instruction the new instruction that explain how to fill out the form.
|
||||
*/
|
||||
public void addInstruction(String instruction) {
|
||||
synchronized (instructions) {
|
||||
instructions.add(instruction);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new item returned from a search.
|
||||
*
|
||||
* @param item the item returned from a search.
|
||||
*/
|
||||
public void addItem(Item item) {
|
||||
synchronized (items) {
|
||||
items.add(item);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this DataForm has at least one FORM_TYPE field which is
|
||||
* hidden. This method is used for sanity checks.
|
||||
*
|
||||
* @return true if there is at least one field which is hidden.
|
||||
*/
|
||||
public boolean hasHiddenFormTypeField() {
|
||||
boolean found = false;
|
||||
for (FormField f : fields) {
|
||||
if (f.getVariable().equals("FORM_TYPE") && f.getType() != null && f.getType().equals("hidden"))
|
||||
found = true;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<").append(getElementName()).append(" xmlns=\"").append(getNamespace()).append(
|
||||
"\" type=\"" + getType() +"\">");
|
||||
if (getTitle() != null) {
|
||||
buf.append("<title>").append(getTitle()).append("</title>");
|
||||
}
|
||||
for (String instruction : getInstructions()) {
|
||||
buf.append("<instructions>").append(instruction).append("</instructions>");
|
||||
}
|
||||
// Append the list of fields returned from a search
|
||||
if (getReportedData() != null) {
|
||||
buf.append(getReportedData().toXML());
|
||||
}
|
||||
// Loop through all the items returned from a search and append them to the string buffer
|
||||
for (Item item : getItems()) {
|
||||
buf.append(item.toXML());
|
||||
}
|
||||
// Loop through all the form fields and append them to the string buffer
|
||||
for (FormField field : getFields()) {
|
||||
buf.append(field.toXML());
|
||||
}
|
||||
buf.append("</").append(getElementName()).append(">");
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Represents the fields that will be returned from a search. This information is useful when
|
||||
* you try to use the jabber:iq:search namespace to return dynamic form information.
|
||||
*
|
||||
* @author Gaston Dombiak
|
||||
*/
|
||||
public static class ReportedData {
|
||||
private List<FormField> fields = new ArrayList<FormField>();
|
||||
|
||||
public ReportedData(List<FormField> fields) {
|
||||
this.fields = fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the fields returned from a search.
|
||||
*
|
||||
* @return the fields returned from a search.
|
||||
*/
|
||||
public List<FormField> getFields() {
|
||||
return Collections.unmodifiableList(new ArrayList<FormField>(fields));
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<reported>");
|
||||
// Loop through all the form items and append them to the string buffer
|
||||
for (FormField field : getFields()) {
|
||||
buf.append(field.toXML());
|
||||
}
|
||||
buf.append("</reported>");
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Represents items of reported data.
|
||||
*
|
||||
* @author Gaston Dombiak
|
||||
*/
|
||||
public static class Item {
|
||||
private List<FormField> fields = new ArrayList<FormField>();
|
||||
|
||||
public Item(List<FormField> fields) {
|
||||
this.fields = fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the fields that define the data that goes with the item.
|
||||
*
|
||||
* @return the fields that define the data that goes with the item.
|
||||
*/
|
||||
public List<FormField> getFields() {
|
||||
return Collections.unmodifiableList(new ArrayList<FormField>(fields));
|
||||
}
|
||||
|
||||
public String toXML() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append("<item>");
|
||||
// Loop through all the form items and append them to the string buffer
|
||||
for (FormField field : getFields()) {
|
||||
buf.append(field.toXML());
|
||||
}
|
||||
buf.append("</item>");
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,156 @@
|
|||
/**
|
||||
*
|
||||
* 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.provider;
|
||||
|
||||
import org.jivesoftware.smack.packet.PacketExtension;
|
||||
import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
||||
import org.jivesoftware.smackx.xdata.FormField;
|
||||
import org.jivesoftware.smackx.xdata.packet.DataForm;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The DataFormProvider parses DataForm packets.
|
||||
*
|
||||
* @author Gaston Dombiak
|
||||
*/
|
||||
public class DataFormProvider implements PacketExtensionProvider {
|
||||
|
||||
/**
|
||||
* Creates a new DataFormProvider.
|
||||
* ProviderManager requires that every PacketExtensionProvider has a public, no-argument constructor
|
||||
*/
|
||||
public DataFormProvider() {
|
||||
}
|
||||
|
||||
public PacketExtension parseExtension(XmlPullParser parser) throws Exception {
|
||||
boolean done = false;
|
||||
DataForm dataForm = new DataForm(parser.getAttributeValue("", "type"));
|
||||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
if (parser.getName().equals("instructions")) {
|
||||
dataForm.addInstruction(parser.nextText());
|
||||
}
|
||||
else if (parser.getName().equals("title")) {
|
||||
dataForm.setTitle(parser.nextText());
|
||||
}
|
||||
else if (parser.getName().equals("field")) {
|
||||
dataForm.addField(parseField(parser));
|
||||
}
|
||||
else if (parser.getName().equals("item")) {
|
||||
dataForm.addItem(parseItem(parser));
|
||||
}
|
||||
else if (parser.getName().equals("reported")) {
|
||||
dataForm.setReportedData(parseReported(parser));
|
||||
}
|
||||
} else if (eventType == XmlPullParser.END_TAG) {
|
||||
if (parser.getName().equals(dataForm.getElementName())) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return dataForm;
|
||||
}
|
||||
|
||||
private FormField parseField(XmlPullParser parser) throws Exception {
|
||||
boolean done = false;
|
||||
FormField formField = new FormField(parser.getAttributeValue("", "var"));
|
||||
formField.setLabel(parser.getAttributeValue("", "label"));
|
||||
formField.setType(parser.getAttributeValue("", "type"));
|
||||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
if (parser.getName().equals("desc")) {
|
||||
formField.setDescription(parser.nextText());
|
||||
}
|
||||
else if (parser.getName().equals("value")) {
|
||||
formField.addValue(parser.nextText());
|
||||
}
|
||||
else if (parser.getName().equals("required")) {
|
||||
formField.setRequired(true);
|
||||
}
|
||||
else if (parser.getName().equals("option")) {
|
||||
formField.addOption(parseOption(parser));
|
||||
}
|
||||
} else if (eventType == XmlPullParser.END_TAG) {
|
||||
if (parser.getName().equals("field")) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return formField;
|
||||
}
|
||||
|
||||
private DataForm.Item parseItem(XmlPullParser parser) throws Exception {
|
||||
boolean done = false;
|
||||
List<FormField> fields = new ArrayList<FormField>();
|
||||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
if (parser.getName().equals("field")) {
|
||||
fields.add(parseField(parser));
|
||||
}
|
||||
} else if (eventType == XmlPullParser.END_TAG) {
|
||||
if (parser.getName().equals("item")) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return new DataForm.Item(fields);
|
||||
}
|
||||
|
||||
private DataForm.ReportedData parseReported(XmlPullParser parser) throws Exception {
|
||||
boolean done = false;
|
||||
List<FormField> fields = new ArrayList<FormField>();
|
||||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
if (parser.getName().equals("field")) {
|
||||
fields.add(parseField(parser));
|
||||
}
|
||||
} else if (eventType == XmlPullParser.END_TAG) {
|
||||
if (parser.getName().equals("reported")) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return new DataForm.ReportedData(fields);
|
||||
}
|
||||
|
||||
private FormField.Option parseOption(XmlPullParser parser) throws Exception {
|
||||
boolean done = false;
|
||||
FormField.Option option = null;
|
||||
String label = parser.getAttributeValue("", "label");
|
||||
while (!done) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == XmlPullParser.START_TAG) {
|
||||
if (parser.getName().equals("value")) {
|
||||
option = new FormField.Option(label, parser.nextText());
|
||||
}
|
||||
} else if (eventType == XmlPullParser.END_TAG) {
|
||||
if (parser.getName().equals("option")) {
|
||||
done = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return option;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue