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

org.xmpp.forms.DataForm Maven / Gradle / Ivy

Go to download

Tinder is a Java based XMPP library, providing an implementation for XMPP stanzas and components. Tinders origins lie in code that's shared between Jive Software's Openfire and Whack implementations. The implementation that's provided in Tinder hasn't been written again from scratch. Instead, code has been moved from the original projects into Tinder, preserving al of the existing features and functionality. Most of the code that's now in Tinder is based on the org.xmpp package implementation that previously existed in Openfire and Whack. This is the code that defines classes such as Packet, JID, IQ, Component and their extensions. Additionally, some multi-purpose code (such as the DataForm and Result Set Management implementations have been moved to Tinder as well.

There is a newer version: 1.3.0
Show newest version
/**
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
 */

package org.xmpp.forms;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.Map.Entry;

import org.dom4j.Element;
import org.dom4j.QName;
import org.jivesoftware.util.FastDateFormat;
import org.xmpp.packet.PacketExtension;
import org.xmpp.util.XMPPConstants;

/**
 * Represents a form that could be use for gathering data as well as for reporting data
 * returned from a search.
 * 

* 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.
  • *
*

* In case the form represents a search, the report will be structured in columns and rows. Use * {@link #addReportedField(String,String,FormField.Type)} to set the columns of the report whilst * the report's rows can be configured using {@link #addItemFields(Map)}. * * @author Gaston Dombiak */ public class DataForm extends PacketExtension { private static final SimpleDateFormat UTC_FORMAT = new SimpleDateFormat( XMPPConstants.XMPP_DELAY_DATETIME_FORMAT); private static final FastDateFormat FAST_UTC_FORMAT = FastDateFormat.getInstance(XMPPConstants.XMPP_DELAY_DATETIME_FORMAT, TimeZone.getTimeZone("UTC")); /** * Element name of the packet extension. */ public static final String ELEMENT_NAME = "x"; /** * Namespace of the packet extension. */ public static final String NAMESPACE = "jabber:x:data"; static { UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC")); // Register that DataForms uses the jabber:x:data namespace registeredExtensions.put(QName.get(ELEMENT_NAME, NAMESPACE), DataForm.class); } /** * Returns the Date obtained by parsing the specified date representation. The date * representation is expected to be in the UTC GMT+0 format. * * @param date date representation in the UTC GMT+0 format. * @return the Date obtained by parsing the specified date representation. * @throws ParseException if an error occurs while parsing the date representation. */ public static Date parseDate(String date) throws ParseException { synchronized (UTC_FORMAT) { return UTC_FORMAT.parse(date); } } public static boolean parseBoolean(String booleanString) throws ParseException { return "1".equals(booleanString) || "true".equals(booleanString); } /** * Returns the String representation of an Object to be used as a field value. * * @param object the object to encode. * @return the String representation of an Object to be used as a field value. */ static String encode(Object object) { if (object instanceof String) { return object.toString(); } else if (object instanceof Boolean) { return Boolean.TRUE.equals(object) ? "1" : "0"; } else if (object instanceof Date) { return FAST_UTC_FORMAT.format((Date) object); } return object.toString(); } public DataForm(Type type) { super(ELEMENT_NAME, NAMESPACE); // Set the type of the data form element.addAttribute("type", type.toString()); } public DataForm(Element element) { super(element); } /** * Returns the type of this data form. * * @return the data form type. * @see org.xmpp.forms.DataForm.Type */ public DataForm.Type getType() { String type = element.attributeValue("type"); if (type != null) { return DataForm.Type.valueOf(type); } else { return null; } } /** * Sets the description of the data. It is similar to the title on a web page or an X window. * You can put a on either a form to fill out, or a set of data results. * * @param title description of the data. */ public void setTitle(String title) { // Remove an existing title element. if (element.element("title") != null) { element.remove(element.element("title")); } element.addElement("title").setText(title); } /** * Returns the description of the data form. 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 element.elementTextTrim("title"); } /** * Returns an unmodifiable 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. * * @return an unmodifiable list of instructions that explain how to fill out the form. */ @SuppressWarnings("unchecked") public List<String> getInstructions() { List<String> answer = new ArrayList<String>(); for (Iterator<Element> it = element.elementIterator("instructions"); it.hasNext();) { answer.add(it.next().getTextTrim()); } return Collections.unmodifiableList(answer); } /** * 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. * <p> * Nothing will be set, if the provided argument is <tt>null</tt> or an empty String. * * @param instruction the new instruction that explain how to fill out the form. */ public void addInstruction(String instruction) { if (instruction == null || instruction.trim().length() == 0) { return; } element.addElement("instructions").setText(instruction); } /** * Clears all the stored instructions in this packet extension. */ @SuppressWarnings("unchecked") public void clearInstructions() { for (Iterator<Element> it = element.elementIterator("instructions"); it.hasNext();) { it.next(); it.remove(); } } /** * Adds a new field as part of the form. * * @return the newly created field. */ public FormField addField() { return new FormField(element.addElement("field")); } /** * Adds a new field as part of the form. The provided arguments are optional * (they are allowed to be <tt>null</tt>). * * @param variable the unique identifier of the field in the context of the * form. Optional parameter. * @param type an indicative of the format for the data. Optional parameter. * @param label the label of the question. Optional parameter. * @return the newly created field. */ public FormField addField(String variable, String label, FormField.Type type) { final FormField result = addField(); if (variable == null || variable.trim().length() == 0) { result.setVariable(variable); } if (type != null) { result.setType(type); } if (label != null && label.trim().length() == 0) { result.setLabel(label); } return result; } /** * Returns the fields that are part of the form. * * @return fields that are part of the form. */ @SuppressWarnings("unchecked") public List<FormField> getFields() { List<FormField> answer = new ArrayList<FormField>(); for (Iterator<Element> it = element.elementIterator("field"); it.hasNext();) { answer.add(new FormField(it.next())); } return answer; } /** * Returns the field whose variable matches the specified variable. * * @param variable the variable name of the field to search. * @return the field whose variable matches the specified variable */ @SuppressWarnings("unchecked") public FormField getField(String variable) { for (Iterator<Element> it = element.elementIterator("field"); it.hasNext();) { FormField formField = new FormField(it.next()); if (variable.equals(formField.getVariable())) { return formField; } } return null; } /** * Removes the field whose variable matches the specified variable. * * @param variable the variable name of the field to remove. * @return true if the field was removed. */ @SuppressWarnings("unchecked") public boolean removeField(String variable) { for (Iterator<Element> it = element.elementIterator("field"); it.hasNext();) { Element field = it.next(); String fieldVariable = field.attributeValue("var"); if (variable.equals(fieldVariable)) { return element.remove(field); } } return false; } /** * Adds a field to the list of fields that will be returned from a search. Each field represents * a column in the report. The order of the columns in the report will honor the sequence in * which they were added. * * @param variable variable name of the new column. This value will be used in * {@link #addItemFields} when adding reported items. * @param label label that corresponds to the new column. Optional parameter. * @param type indicates the type of field of the new column. Optional parameter. */ public void addReportedField(String variable, String label, FormField.Type type) { Element reported = element.element("reported"); synchronized (element) { if (reported == null) { reported = element.element("reported"); if (reported == null) { reported = element.addElement("reported"); } } } FormField newField = new FormField(reported.addElement("field")); newField.setVariable(variable); newField.setType(type); newField.setLabel(label); } /** * Adds a new row of items of reported data. For each entry in the <tt>fields</tt> parameter * a <tt>field</tt> element will be added to the <item> element. The variable of the new * <tt>field</tt> will be the key of the entry. The new <tt>field</tt> will have several values * if the entry's value is a {@link Collection}. Since the value is of type {@link Object} it * is possible to include any type of object as a value. The actual value to include in the * data form is the result of the {@link #encode(Object)} method. * * @param fields list of <variable,value> to be added as a new item. */ @SuppressWarnings("unchecked") public void addItemFields(Map<String,Object> fields) { Element item = element.addElement("item"); // Add a field element to the item element for each row in fields for (Entry<String, Object> entry : fields.entrySet()) { Element field = item.addElement("field"); field.addAttribute("var", entry.getKey()); final Object value = entry.getValue(); if (value == null) { continue; } if (value instanceof Collection) { // Add a value element for each entry in the collection for (Object colValue : (Collection) value) { if (colValue != null) { field.addElement("value").setText(encode(colValue)); } } } else { field.addElement("value").setText(encode(value)); } } } public DataForm createCopy() { return new DataForm(this.getElement().createCopy()); } /** * Type-safe enumeration to represent the type of the Data forms. */ public enum Type { /** * The forms-processing entity is asking the forms-submitting entity to complete a form. */ form, /** * The forms-submitting entity is submitting data to the forms-processing entity. */ submit, /** * The forms-submitting entity has cancelled submission of data to the forms-processing * entity. */ cancel, /** * The forms-processing entity is returning data (e.g., search results) to the * forms-submitting entity, or the data is a generic data set. */ result; } } </code></pre> <br/> <br/> <div id="right-banner"> </div> <div id="left-banner"> </div> <div class='clear'></div> <aside class="related-items"> <section> <div class="panel panel-primary"> <div class="panel-heading margin-bottom">Related Artifacts</div> <div class=""> <a title='This artifact is from the group mysql' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/mysql/mysql-connector-java' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> mysql-connector-java <small class='group-info' >mysql</small></a><br/><a title='This artifact is from the group com.github.codedrinker' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.github.codedrinker/facebook-messenger' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> facebook-messenger <small class='group-info' >com.github.codedrinker</small></a><br/><a title='This artifact is from the group org.seleniumhq.selenium' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.seleniumhq.selenium/selenium-java' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> selenium-java <small class='group-info' >org.seleniumhq.selenium</small></a><br/><a title='This artifact is from the group com.github.sola92' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.github.sola92/instagram-java' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> instagram-java <small class='group-info' >com.github.sola92</small></a><br/><a title='This artifact is from the group com.google.code.gson' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.google.code.gson/gson' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> gson <small class='group-info' >com.google.code.gson</small></a><br/><a title='This artifact is from the group org.apache.poi' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.apache.poi/poi' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> poi <small class='group-info' >org.apache.poi</small></a><br/><a title='This artifact is from the group org.apache.httpcomponents' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.apache.httpcomponents/httpclient' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> httpclient <small class='group-info' >org.apache.httpcomponents</small></a><br/><a title='This artifact is from the group org.json' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.json/json' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> json <small class='group-info' >org.json</small></a><br/><a title='This artifact is from the group com.google.code.facebook-java-api' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.google.code.facebook-java-api/facebook-java-api' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> facebook-java-api <small class='group-info' >com.google.code.facebook-java-api</small></a><br/><a title='This artifact is from the group org.apache.poi' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.apache.poi/poi-ooxml' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> poi-ooxml <small class='group-info' >org.apache.poi</small></a><br/><a title='This artifact is from the group com.fasterxml.jackson.core' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.fasterxml.jackson.core/jackson-databind' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> jackson-databind <small class='group-info' >com.fasterxml.jackson.core</small></a><br/><a title='This artifact is from the group junit' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/junit/junit' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> junit <small class='group-info' >junit</small></a><br/><a title='This artifact is from the group org.primefaces' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.primefaces/primefaces' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> primefaces <small class='group-info' >org.primefaces</small></a><br/><a title='This artifact is from the group com.github.noraui' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.github.noraui/ojdbc7' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> ojdbc7 <small class='group-info' >com.github.noraui</small></a><br/><a title='This artifact is from the group com.jfoenix' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.jfoenix/jfoenix' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> jfoenix <small class='group-info' >com.jfoenix</small></a><br/><a title='This artifact is from the group org.testng' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.testng/testng' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> testng <small class='group-info' >org.testng</small></a><br/><a title='This artifact is from the group com.googlecode.json-simple' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.googlecode.json-simple/json-simple' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> json-simple <small class='group-info' >com.googlecode.json-simple</small></a><br/><a title='This artifact is from the group org.seleniumhq.selenium' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.seleniumhq.selenium/selenium-server' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> selenium-server <small class='group-info' >org.seleniumhq.selenium</small></a><br/><a title='This artifact is from the group com.itextpdf' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.itextpdf/itextpdf' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> itextpdf <small class='group-info' >com.itextpdf</small></a><br/><a title='This artifact is from the group org.springframework' class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.springframework/spring-core' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> spring-core <small class='group-info' >org.springframework</small></a><br/> </div> </div> </section> <section> <div class="panel panel-primary"> <div class="panel-heading margin-bottom">Related Groups</div> <div class=""> <a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.springframework' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> org.springframework</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.apache.poi' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> org.apache.poi</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.hibernate' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> org.hibernate</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.springframework.boot' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> org.springframework.boot</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.fasterxml.jackson.core' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> com.fasterxml.jackson.core</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.itextpdf' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> com.itextpdf</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.seleniumhq.selenium' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> org.seleniumhq.selenium</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/mysql' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> mysql</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.finos.legend.engine' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> org.finos.legend.engine</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.apache.httpcomponents' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> org.apache.httpcomponents</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.apache.logging.log4j' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> org.apache.logging.log4j</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.openjfx' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> org.openjfx</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.apache.commons' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> org.apache.commons</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/org.json' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> org.json</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.google.guava' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> com.google.guava</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.google.zxing' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> com.google.zxing</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/net.sf.jasperreports' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> net.sf.jasperreports</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/javax.xml.bind' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> javax.xml.bind</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/ojdbc' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> ojdbc</a><br/><a class='btn btn-default btn-xs small-margin-bottom ellipsis sidebar-btn' href='/artifacts/com.google.code.facebook-java-api' ><i class="fa fa-arrow-circle-right" aria-hidden="true"></i> com.google.code.facebook-java-api</a><br/> </div> </div> </section> </aside> <div class='clear'></div> </main> </div> <br/><br/> <div class="align-center">© 2015 - 2024 <a href="/legal-notice.php">Weber Informatics LLC</a> | <a href="/data-protection.php">Privacy Policy</a></div> <br/><br/><br/><br/><br/><br/> </body> </html>