2004-03-29 23:29:55 +02:00
|
|
|
/**
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2004-03-29 23:29:55 +02:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2004-11-03 00:37:00 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2004-03-29 23:29:55 +02:00
|
|
|
*
|
2004-11-03 00:37:00 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2004-03-29 23:29:55 +02:00
|
|
|
*
|
2004-11-03 00:37:00 +01:00
|
|
|
* 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.
|
2004-03-29 23:29:55 +02:00
|
|
|
*/
|
2004-11-03 00:37:00 +01:00
|
|
|
|
2014-02-15 11:35:08 +01:00
|
|
|
package org.jivesoftware.smackx.xdata.packet;
|
2004-03-29 23:29:55 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2018-06-03 21:19:04 +02:00
|
|
|
import java.util.Collection;
|
2004-03-29 23:29:55 +02:00
|
|
|
import java.util.Collections;
|
2020-04-18 19:01:25 +02:00
|
|
|
import java.util.Iterator;
|
2015-04-12 19:27:22 +02:00
|
|
|
import java.util.LinkedHashMap;
|
2004-03-29 23:29:55 +02:00
|
|
|
import java.util.List;
|
2015-01-10 20:10:46 +01:00
|
|
|
import java.util.Locale;
|
2015-04-12 19:27:22 +02:00
|
|
|
import java.util.Map;
|
2004-03-29 23:29:55 +02:00
|
|
|
|
2019-10-29 11:14:55 +01:00
|
|
|
import javax.xml.namespace.QName;
|
|
|
|
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.packet.Element;
|
|
|
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
2019-10-29 11:14:55 +01:00
|
|
|
import org.jivesoftware.smack.packet.StanzaView;
|
2019-06-10 16:58:38 +02:00
|
|
|
import org.jivesoftware.smack.packet.XmlEnvironment;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
|
|
|
|
|
|
|
import org.jivesoftware.smackx.xdata.FormField;
|
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
|
|
|
* Represents a form that could be use for gathering data as well as for reporting data
|
|
|
|
* returned from a search.
|
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
2015-02-26 18:41:17 +01:00
|
|
|
public class DataForm implements ExtensionElement {
|
2004-03-29 23:29:55 +02:00
|
|
|
|
2014-06-19 11:48:01 +02:00
|
|
|
public static final String NAMESPACE = "jabber:x:data";
|
|
|
|
public static final String ELEMENT = "x";
|
|
|
|
|
2019-10-29 11:14:55 +01:00
|
|
|
public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
|
|
|
|
|
2015-01-10 20:10:46 +01:00
|
|
|
public enum Type {
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* This stanza contains a form to fill out. Display it to the user (if your program can).
|
2015-01-10 20:10:46 +01:00
|
|
|
*/
|
|
|
|
form,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The form is filled out, and this is the data that is being returned from the form.
|
|
|
|
*/
|
|
|
|
submit,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The form was cancelled. Tell the asker that piece of information.
|
|
|
|
*/
|
|
|
|
cancel,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data results being returned from a search, or some other query.
|
|
|
|
*/
|
|
|
|
result,
|
|
|
|
;
|
|
|
|
|
|
|
|
public static Type fromString(String string) {
|
|
|
|
return Type.valueOf(string.toLowerCase(Locale.US));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Type type;
|
2004-03-29 23:29:55 +02:00
|
|
|
private String title;
|
2015-09-13 18:12:33 +02:00
|
|
|
private final List<String> instructions = new ArrayList<>();
|
2004-03-29 23:29:55 +02:00
|
|
|
private ReportedData reportedData;
|
2017-12-13 23:10:11 +01:00
|
|
|
private final List<Item> items = new ArrayList<>();
|
2015-04-12 19:27:22 +02:00
|
|
|
private final Map<String, FormField> fields = new LinkedHashMap<>();
|
2017-12-13 23:10:11 +01:00
|
|
|
private final List<Element> extensionElements = new ArrayList<>();
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2015-01-10 20:10:46 +01:00
|
|
|
public DataForm(Type type) {
|
2004-03-29 23:29:55 +02:00
|
|
|
this.type = type;
|
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
|
|
|
* Returns the meaning of the data within the context. The data could be part of a form
|
2015-01-10 20:10:46 +01:00
|
|
|
* to fill out, a form submission or data results.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @return the form's type.
|
|
|
|
*/
|
2015-01-10 20:10:46 +01:00
|
|
|
public Type getType() {
|
2018-05-09 23:06:12 +02:00
|
|
|
return type;
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* Returns the description of the data. It is similar to the title on a web page or an X
|
2017-12-25 12:51:41 +01:00
|
|
|
* window. You can put a <title/> on either a form to fill out, or a set of data results.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @return description of the data.
|
|
|
|
*/
|
|
|
|
public String getTitle() {
|
|
|
|
return title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* 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
|
2014-04-09 08:26:28 +02:00
|
|
|
* to show them to the user.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-04-09 08:26:28 +02:00
|
|
|
* @return a List of the list of instructions that explain how to fill out the form.
|
2004-03-29 23:29:55 +02:00
|
|
|
*/
|
2014-04-09 08:26:28 +02:00
|
|
|
public List<String> getInstructions() {
|
2004-04-26 04:33:06 +02:00
|
|
|
synchronized (instructions) {
|
2017-12-13 23:10:11 +01:00
|
|
|
return Collections.unmodifiableList(new ArrayList<>(instructions));
|
2004-04-26 04:33:06 +02:00
|
|
|
}
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the fields that will be returned from a search.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @return fields that will be returned from a search.
|
|
|
|
*/
|
|
|
|
public ReportedData getReportedData() {
|
|
|
|
return reportedData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-09 08:26:28 +02:00
|
|
|
* Returns a List of the items returned from a search.
|
2004-03-29 23:29:55 +02:00
|
|
|
*
|
2014-04-09 08:26:28 +02:00
|
|
|
* @return a List of the items returned from a search.
|
2004-03-29 23:29:55 +02:00
|
|
|
*/
|
2014-04-09 08:26:28 +02:00
|
|
|
public List<Item> getItems() {
|
2004-03-29 23:29:55 +02:00
|
|
|
synchronized (items) {
|
2017-12-13 23:10:11 +01:00
|
|
|
return Collections.unmodifiableList(new ArrayList<>(items));
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-04-09 08:26:28 +02:00
|
|
|
* Returns a List of the fields that are part of the form.
|
2004-03-29 23:29:55 +02:00
|
|
|
*
|
2014-04-09 08:26:28 +02:00
|
|
|
* @return a List of the fields that are part of the form.
|
2004-03-29 23:29:55 +02:00
|
|
|
*/
|
2014-04-09 08:26:28 +02:00
|
|
|
public List<FormField> getFields() {
|
2004-03-29 23:29:55 +02:00
|
|
|
synchronized (fields) {
|
2015-04-12 19:27:22 +02:00
|
|
|
return new ArrayList<>(fields.values());
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 18:01:07 +01:00
|
|
|
/**
|
|
|
|
* Return the form field with the given variable name or null.
|
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param variableName TODO javadoc me please
|
2015-01-10 18:01:07 +01:00
|
|
|
* @return the form field or null.
|
|
|
|
* @since 4.1
|
|
|
|
*/
|
|
|
|
public FormField getField(String variableName) {
|
|
|
|
synchronized (fields) {
|
2015-04-12 19:27:22 +02:00
|
|
|
return fields.get(variableName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-10 16:58:38 +02:00
|
|
|
public FormField replaceField(FormField field) {
|
|
|
|
String fieldVariableName = field.getVariable();
|
|
|
|
|
|
|
|
synchronized (fields) {
|
|
|
|
if (!fields.containsKey(fieldVariableName)) {
|
|
|
|
throw new IllegalArgumentException("No field with the name " + fieldVariableName + " exists");
|
|
|
|
}
|
|
|
|
return fields.put(fieldVariableName, field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-12 19:27:22 +02:00
|
|
|
/**
|
|
|
|
* Check if a form field with the given variable name exists.
|
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param variableName TODO javadoc me please
|
2015-04-12 19:27:22 +02:00
|
|
|
* @return true if a form field with the variable name exists, false otherwise.
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
|
|
|
public boolean hasField(String variableName) {
|
|
|
|
synchronized (fields) {
|
|
|
|
return fields.containsKey(variableName);
|
2015-01-10 18:01:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2004-03-29 23:29:55 +02:00
|
|
|
public String getElementName() {
|
2014-06-19 11:48:01 +02:00
|
|
|
return ELEMENT;
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2004-03-29 23:29:55 +02:00
|
|
|
public String getNamespace() {
|
2014-06-19 11:48:01 +02:00
|
|
|
return NAMESPACE;
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the description of the data. It is similar to the title on a web page or an X window.
|
2017-12-25 12:51:41 +01:00
|
|
|
* You can put a <title/> on either a form to fill out, or a set of data results.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @param title description of the data.
|
|
|
|
*/
|
|
|
|
public void setTitle(String title) {
|
|
|
|
this.title = title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2004-04-26 04:33:06 +02:00
|
|
|
* @param instructions list of instructions that explain how to fill out the form.
|
2004-03-29 23:29:55 +02:00
|
|
|
*/
|
2006-07-18 07:14:33 +02:00
|
|
|
public void setInstructions(List<String> instructions) {
|
2015-09-13 18:12:33 +02:00
|
|
|
synchronized (this.instructions) {
|
|
|
|
this.instructions.clear();
|
|
|
|
this.instructions.addAll(instructions);
|
|
|
|
}
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the fields that will be returned from a search.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @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.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @param field the field to add to the form.
|
|
|
|
*/
|
|
|
|
public void addField(FormField field) {
|
2015-01-10 18:01:07 +01:00
|
|
|
String fieldVariableName = field.getVariable();
|
2015-11-20 17:39:02 +01:00
|
|
|
// Form field values must be unique unless they are of type 'fixed', in
|
|
|
|
// which case their variable name may be 'null', and therefore could
|
|
|
|
// appear multiple times within the same form.
|
|
|
|
if (fieldVariableName != null && hasField(fieldVariableName)) {
|
2015-01-10 18:01:07 +01:00
|
|
|
throw new IllegalArgumentException("This data form already contains a form field with the variable name '"
|
|
|
|
+ fieldVariableName + "'");
|
|
|
|
}
|
2004-03-29 23:29:55 +02:00
|
|
|
synchronized (fields) {
|
2015-04-12 19:27:22 +02:00
|
|
|
fields.put(fieldVariableName, field);
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2018-06-03 21:19:04 +02:00
|
|
|
/**
|
|
|
|
* Add the given fields to this form.
|
|
|
|
*
|
2019-08-30 12:08:30 +02:00
|
|
|
* @param fieldsToAdd TODO javadoc me please
|
2018-06-03 21:19:04 +02:00
|
|
|
* @return true if a field was overridden.
|
|
|
|
* @since 4.3.0
|
|
|
|
*/
|
|
|
|
public boolean addFields(Collection<FormField> fieldsToAdd) {
|
|
|
|
boolean fieldOverridden = false;
|
|
|
|
synchronized (fields) {
|
|
|
|
for (FormField field : fieldsToAdd) {
|
|
|
|
FormField previousField = fields.put(field.getVariable(), field);
|
|
|
|
if (previousField != null) {
|
|
|
|
fieldOverridden = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fieldOverridden;
|
|
|
|
}
|
|
|
|
|
2004-04-26 04:33:06 +02:00
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* 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.
|
|
|
|
*
|
2004-04-26 04:33:06 +02:00
|
|
|
* @param instruction the new instruction that explain how to fill out the form.
|
|
|
|
*/
|
|
|
|
public void addInstruction(String instruction) {
|
|
|
|
synchronized (instructions) {
|
|
|
|
instructions.add(instruction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
|
|
|
* Adds a new item returned from a search.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2004-04-08 21:09:19 +02:00
|
|
|
* @param item the item returned from a search.
|
2004-03-29 23:29:55 +02:00
|
|
|
*/
|
|
|
|
public void addItem(Item item) {
|
|
|
|
synchronized (items) {
|
|
|
|
items.add(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 13:43:04 +02:00
|
|
|
public void addExtensionElement(Element element) {
|
|
|
|
extensionElements.add(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Element> getExtensionElements() {
|
|
|
|
return Collections.unmodifiableList(extensionElements);
|
|
|
|
}
|
|
|
|
|
2020-04-18 19:01:25 +02:00
|
|
|
/**
|
|
|
|
* Return the form type from the hidden form type field.
|
|
|
|
*
|
|
|
|
* @return the form type or <code>null</code> if this form has none set.
|
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
public String getFormType() {
|
|
|
|
FormField formTypeField = getHiddenFormTypeField();
|
|
|
|
if (formTypeField == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return formTypeField.getFirstValue();
|
|
|
|
}
|
|
|
|
|
2015-01-10 18:01:07 +01:00
|
|
|
/**
|
|
|
|
* Returns the hidden FORM_TYPE field or null if this data form has none.
|
|
|
|
*
|
|
|
|
* @return the hidden FORM_TYPE field or null.
|
|
|
|
* @since 4.1
|
|
|
|
*/
|
|
|
|
public FormField getHiddenFormTypeField() {
|
|
|
|
FormField field = getField(FormField.FORM_TYPE);
|
|
|
|
if (field != null && field.getType() == FormField.Type.hidden) {
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-03-18 09:50:48 +01:00
|
|
|
/**
|
|
|
|
* Returns true if this DataForm has at least one FORM_TYPE field which is
|
|
|
|
* hidden. This method is used for sanity checks.
|
|
|
|
*
|
2014-03-10 18:31:45 +01:00
|
|
|
* @return true if there is at least one field which is hidden.
|
2013-03-18 09:50:48 +01:00
|
|
|
*/
|
2013-03-31 16:24:50 +02:00
|
|
|
public boolean hasHiddenFormTypeField() {
|
2015-01-10 18:01:07 +01:00
|
|
|
return getHiddenFormTypeField() != null;
|
2013-03-18 09:50:48 +01:00
|
|
|
}
|
|
|
|
|
2014-06-19 11:48:01 +02:00
|
|
|
@Override
|
2019-06-10 16:58:38 +02:00
|
|
|
public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
|
2019-09-07 18:17:08 +02:00
|
|
|
XmlStringBuilder buf = new XmlStringBuilder(this, xmlEnvironment);
|
2014-06-19 11:48:01 +02:00
|
|
|
buf.attribute("type", getType());
|
2014-08-20 21:25:14 +02:00
|
|
|
buf.rightAngleBracket();
|
2014-06-19 11:48:01 +02:00
|
|
|
|
|
|
|
buf.optElement("title", getTitle());
|
2014-04-09 08:26:28 +02:00
|
|
|
for (String instruction : getInstructions()) {
|
2014-06-19 11:48:01 +02:00
|
|
|
buf.element("instructions", instruction);
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
// 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
|
2014-04-09 08:26:28 +02:00
|
|
|
for (Item item : getItems()) {
|
2004-03-29 23:29:55 +02:00
|
|
|
buf.append(item.toXML());
|
|
|
|
}
|
2019-06-10 16:58:38 +02:00
|
|
|
// Add all form fields.
|
2019-09-07 18:17:08 +02:00
|
|
|
buf.append(getFields());
|
2014-09-12 13:43:04 +02:00
|
|
|
for (Element element : extensionElements) {
|
2019-02-04 13:27:41 +01:00
|
|
|
buf.append(element.toXML());
|
2014-09-12 13:43:04 +02:00
|
|
|
}
|
2014-06-19 11:48:01 +02:00
|
|
|
buf.closeElement(this);
|
|
|
|
return buf;
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
|
2014-09-16 22:14:10 +02:00
|
|
|
/**
|
2019-10-29 11:14:55 +01:00
|
|
|
* Get data form from a stanza.
|
|
|
|
*
|
|
|
|
* @param stanzaView the stanza to get data form from.
|
2014-09-16 22:14:10 +02:00
|
|
|
* @return the DataForm or null
|
|
|
|
*/
|
2019-10-29 11:14:55 +01:00
|
|
|
public static DataForm from(StanzaView stanzaView) {
|
2020-04-05 22:10:05 +02:00
|
|
|
return stanzaView.getExtension(DataForm.class);
|
2014-09-16 22:14:10 +02:00
|
|
|
}
|
|
|
|
|
2020-04-18 19:01:25 +02:00
|
|
|
/**
|
|
|
|
* Get the data form with the given form type from a stanza view.
|
|
|
|
*
|
|
|
|
* @param stanzaView the stanza view to retrieve the data form from
|
|
|
|
* @param formType the form type
|
|
|
|
* @return the retrieved data form or <code>null</code> if there is no matching one
|
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
public static DataForm from(StanzaView stanzaView, String formType) {
|
|
|
|
List<DataForm> dataForms = stanzaView.getExtensions(DataForm.class);
|
|
|
|
return from(dataForms, formType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the first matching data form with the given form type from the given collection of data forms.
|
|
|
|
*
|
|
|
|
* @param dataForms the collection of data forms
|
|
|
|
* @param formType the form type to match for
|
|
|
|
* @return the first matching data form or <code>null</code> if there is none
|
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
public static DataForm from(Collection<DataForm> dataForms, String formType) {
|
|
|
|
for (DataForm dataForm : dataForms) {
|
|
|
|
if (formType.equals(dataForm.getFormType())) {
|
|
|
|
return dataForm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the first matching data form with the given form type from the given collection.
|
|
|
|
*
|
|
|
|
* @param dataForms the collection of data forms
|
|
|
|
* @param formType the form type to match for
|
|
|
|
* @return the removed data form or <code>null</code> if there was none removed
|
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
public static DataForm remove(Collection<DataForm> dataForms, String formType) {
|
|
|
|
Iterator<DataForm> it = dataForms.iterator();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
DataForm dataForm = it.next();
|
|
|
|
if (formType.equals(dataForm.getFormType())) {
|
|
|
|
it.remove();
|
|
|
|
return dataForm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
|
|
|
* Represents the fields that will be returned from a search. This information is useful when
|
2004-03-29 23:29:55 +02:00
|
|
|
* you try to use the jabber:iq:search namespace to return dynamic form information.
|
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
|
|
|
public static class ReportedData {
|
2014-06-19 11:48:01 +02:00
|
|
|
public static final String ELEMENT = "reported";
|
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
private List<FormField> fields = new ArrayList<>();
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
public ReportedData(List<FormField> fields) {
|
2004-03-29 23:29:55 +02:00
|
|
|
this.fields = fields;
|
|
|
|
}
|
2014-04-09 08:26:28 +02:00
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
|
|
|
* Returns the fields returned from a search.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @return the fields returned from a search.
|
|
|
|
*/
|
2014-04-09 08:26:28 +02:00
|
|
|
public List<FormField> getFields() {
|
2017-12-13 23:10:11 +01:00
|
|
|
return Collections.unmodifiableList(new ArrayList<>(fields));
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
2014-04-09 08:26:28 +02:00
|
|
|
|
2014-06-19 11:48:01 +02:00
|
|
|
public CharSequence toXML() {
|
|
|
|
XmlStringBuilder buf = new XmlStringBuilder();
|
|
|
|
buf.openElement(ELEMENT);
|
2004-03-29 23:29:55 +02:00
|
|
|
// Loop through all the form items and append them to the string buffer
|
2014-04-09 08:26:28 +02:00
|
|
|
for (FormField field : getFields()) {
|
2019-02-04 13:27:41 +01:00
|
|
|
buf.append(field.toXML());
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
2014-06-19 11:48:01 +02:00
|
|
|
buf.closeElement(ELEMENT);
|
|
|
|
return buf;
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* Represents items of reported data.
|
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
|
|
|
public static class Item {
|
2014-06-19 11:48:01 +02:00
|
|
|
public static final String ELEMENT = "item";
|
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
private List<FormField> fields = new ArrayList<>();
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
public Item(List<FormField> fields) {
|
2004-03-29 23:29:55 +02:00
|
|
|
this.fields = fields;
|
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2004-03-29 23:29:55 +02:00
|
|
|
/**
|
|
|
|
* Returns the fields that define the data that goes with the item.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2004-03-29 23:29:55 +02:00
|
|
|
* @return the fields that define the data that goes with the item.
|
|
|
|
*/
|
2014-04-09 08:26:28 +02:00
|
|
|
public List<FormField> getFields() {
|
2017-12-13 23:10:11 +01:00
|
|
|
return Collections.unmodifiableList(new ArrayList<>(fields));
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2014-06-19 11:48:01 +02:00
|
|
|
public CharSequence toXML() {
|
|
|
|
XmlStringBuilder buf = new XmlStringBuilder();
|
|
|
|
buf.openElement(ELEMENT);
|
2004-03-29 23:29:55 +02:00
|
|
|
// Loop through all the form items and append them to the string buffer
|
2014-04-09 08:26:28 +02:00
|
|
|
for (FormField field : getFields()) {
|
2019-02-04 13:27:41 +01:00
|
|
|
buf.append(field.toXML());
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
2014-06-19 11:48:01 +02:00
|
|
|
buf.closeElement(ELEMENT);
|
|
|
|
return buf;
|
2004-03-29 23:29:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|