mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-22 12:02:05 +01:00
Make FormField.Type an enum
This commit is contained in:
parent
0c68d59ade
commit
755765120d
13 changed files with 178 additions and 146 deletions
|
@ -186,7 +186,7 @@ public class XmlStringBuilder implements Appendable, CharSequence {
|
|||
|
||||
public XmlStringBuilder optAttribute(String name, Enum<?> value) {
|
||||
if (value != null) {
|
||||
attribute(name, value.name());
|
||||
attribute(name, value.toString());
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
|
|
@ -369,7 +369,7 @@ public class FileTransferNegotiator extends Manager {
|
|||
private DataForm createDefaultInitiationForm() {
|
||||
DataForm form = new DataForm(Form.TYPE_FORM);
|
||||
FormField field = new FormField(STREAM_DATA_FIELD_NAME);
|
||||
field.setType(FormField.TYPE_LIST_SINGLE);
|
||||
field.setType(FormField.Type.list_single);
|
||||
if (!IBB_ONLY) {
|
||||
field.addOption(new FormField.Option(Bytestream.NAMESPACE));
|
||||
}
|
||||
|
|
|
@ -92,7 +92,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setAccessModel(AccessModel accessModel)
|
||||
{
|
||||
addField(ConfigureNodeFields.access_model, FormField.TYPE_LIST_SINGLE);
|
||||
addField(ConfigureNodeFields.access_model, FormField.Type.list_single);
|
||||
setAnswer(ConfigureNodeFields.access_model.getFieldName(), getListSingle(accessModel.toString()));
|
||||
}
|
||||
|
||||
|
@ -115,7 +115,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setBodyXSLT(String bodyXslt)
|
||||
{
|
||||
addField(ConfigureNodeFields.body_xslt, FormField.TYPE_TEXT_SINGLE);
|
||||
addField(ConfigureNodeFields.body_xslt, FormField.Type.text_single);
|
||||
setAnswer(ConfigureNodeFields.body_xslt.getFieldName(), bodyXslt);
|
||||
}
|
||||
|
||||
|
@ -136,7 +136,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setChildren(List<String> children)
|
||||
{
|
||||
addField(ConfigureNodeFields.children, FormField.TYPE_TEXT_MULTI);
|
||||
addField(ConfigureNodeFields.children, FormField.Type.text_multi);
|
||||
setAnswer(ConfigureNodeFields.children.getFieldName(), children);
|
||||
}
|
||||
|
||||
|
@ -162,7 +162,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setChildrenAssociationPolicy(ChildrenAssociationPolicy policy)
|
||||
{
|
||||
addField(ConfigureNodeFields.children_association_policy, FormField.TYPE_LIST_SINGLE);
|
||||
addField(ConfigureNodeFields.children_association_policy, FormField.Type.list_single);
|
||||
List<String> values = new ArrayList<String>(1);
|
||||
values.add(policy.toString());
|
||||
setAnswer(ConfigureNodeFields.children_association_policy.getFieldName(), values);
|
||||
|
@ -189,7 +189,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setChildrenAssociationWhitelist(List<String> whitelist)
|
||||
{
|
||||
addField(ConfigureNodeFields.children_association_whitelist, FormField.TYPE_JID_MULTI);
|
||||
addField(ConfigureNodeFields.children_association_whitelist, FormField.Type.jid_multi);
|
||||
setAnswer(ConfigureNodeFields.children_association_whitelist.getFieldName(), whitelist);
|
||||
}
|
||||
|
||||
|
@ -210,7 +210,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setChildrenMax(int max)
|
||||
{
|
||||
addField(ConfigureNodeFields.children_max, FormField.TYPE_TEXT_SINGLE);
|
||||
addField(ConfigureNodeFields.children_max, FormField.Type.text_single);
|
||||
setAnswer(ConfigureNodeFields.children_max.getFieldName(), max);
|
||||
}
|
||||
|
||||
|
@ -231,7 +231,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setCollection(String collection)
|
||||
{
|
||||
addField(ConfigureNodeFields.collection, FormField.TYPE_TEXT_SINGLE);
|
||||
addField(ConfigureNodeFields.collection, FormField.Type.text_single);
|
||||
setAnswer(ConfigureNodeFields.collection.getFieldName(), collection);
|
||||
}
|
||||
|
||||
|
@ -256,7 +256,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setDataformXSLT(String url)
|
||||
{
|
||||
addField(ConfigureNodeFields.dataform_xslt, FormField.TYPE_TEXT_SINGLE);
|
||||
addField(ConfigureNodeFields.dataform_xslt, FormField.Type.text_single);
|
||||
setAnswer(ConfigureNodeFields.dataform_xslt.getFieldName(), url);
|
||||
}
|
||||
|
||||
|
@ -277,7 +277,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setDeliverPayloads(boolean deliver)
|
||||
{
|
||||
addField(ConfigureNodeFields.deliver_payloads, FormField.TYPE_BOOLEAN);
|
||||
addField(ConfigureNodeFields.deliver_payloads, FormField.Type.bool);
|
||||
setAnswer(ConfigureNodeFields.deliver_payloads.getFieldName(), deliver);
|
||||
}
|
||||
|
||||
|
@ -303,7 +303,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setItemReply(ItemReply reply)
|
||||
{
|
||||
addField(ConfigureNodeFields.itemreply, FormField.TYPE_LIST_SINGLE);
|
||||
addField(ConfigureNodeFields.itemreply, FormField.Type.list_single);
|
||||
setAnswer(ConfigureNodeFields.itemreply.getFieldName(), getListSingle(reply.toString()));
|
||||
}
|
||||
|
||||
|
@ -326,7 +326,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setMaxItems(int max)
|
||||
{
|
||||
addField(ConfigureNodeFields.max_items, FormField.TYPE_TEXT_SINGLE);
|
||||
addField(ConfigureNodeFields.max_items, FormField.Type.text_single);
|
||||
setAnswer(ConfigureNodeFields.max_items.getFieldName(), max);
|
||||
}
|
||||
|
||||
|
@ -347,7 +347,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setMaxPayloadSize(int max)
|
||||
{
|
||||
addField(ConfigureNodeFields.max_payload_size, FormField.TYPE_TEXT_SINGLE);
|
||||
addField(ConfigureNodeFields.max_payload_size, FormField.Type.text_single);
|
||||
setAnswer(ConfigureNodeFields.max_payload_size.getFieldName(), max);
|
||||
}
|
||||
|
||||
|
@ -373,7 +373,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setNodeType(NodeType type)
|
||||
{
|
||||
addField(ConfigureNodeFields.node_type, FormField.TYPE_LIST_SINGLE);
|
||||
addField(ConfigureNodeFields.node_type, FormField.Type.list_single);
|
||||
setAnswer(ConfigureNodeFields.node_type.getFieldName(), getListSingle(type.toString()));
|
||||
}
|
||||
|
||||
|
@ -394,7 +394,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setNotifyConfig(boolean notify)
|
||||
{
|
||||
addField(ConfigureNodeFields.notify_config, FormField.TYPE_BOOLEAN);
|
||||
addField(ConfigureNodeFields.notify_config, FormField.Type.bool);
|
||||
setAnswer(ConfigureNodeFields.notify_config.getFieldName(), notify);
|
||||
}
|
||||
|
||||
|
@ -415,7 +415,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setNotifyDelete(boolean notify)
|
||||
{
|
||||
addField(ConfigureNodeFields.notify_delete, FormField.TYPE_BOOLEAN);
|
||||
addField(ConfigureNodeFields.notify_delete, FormField.Type.bool);
|
||||
setAnswer(ConfigureNodeFields.notify_delete.getFieldName(), notify);
|
||||
}
|
||||
|
||||
|
@ -438,7 +438,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setNotifyRetract(boolean notify)
|
||||
{
|
||||
addField(ConfigureNodeFields.notify_retract, FormField.TYPE_BOOLEAN);
|
||||
addField(ConfigureNodeFields.notify_retract, FormField.Type.bool);
|
||||
setAnswer(ConfigureNodeFields.notify_retract.getFieldName(), notify);
|
||||
}
|
||||
|
||||
|
@ -459,7 +459,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setPersistentItems(boolean persist)
|
||||
{
|
||||
addField(ConfigureNodeFields.persist_items, FormField.TYPE_BOOLEAN);
|
||||
addField(ConfigureNodeFields.persist_items, FormField.Type.bool);
|
||||
setAnswer(ConfigureNodeFields.persist_items.getFieldName(), persist);
|
||||
}
|
||||
|
||||
|
@ -480,7 +480,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setPresenceBasedDelivery(boolean presenceBased)
|
||||
{
|
||||
addField(ConfigureNodeFields.presence_based_delivery, FormField.TYPE_BOOLEAN);
|
||||
addField(ConfigureNodeFields.presence_based_delivery, FormField.Type.bool);
|
||||
setAnswer(ConfigureNodeFields.presence_based_delivery.getFieldName(), presenceBased);
|
||||
}
|
||||
|
||||
|
@ -506,7 +506,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setPublishModel(PublishModel publish)
|
||||
{
|
||||
addField(ConfigureNodeFields.publish_model, FormField.TYPE_LIST_SINGLE);
|
||||
addField(ConfigureNodeFields.publish_model, FormField.Type.list_single);
|
||||
setAnswer(ConfigureNodeFields.publish_model.getFieldName(), getListSingle(publish.toString()));
|
||||
}
|
||||
|
||||
|
@ -527,7 +527,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setReplyRoom(List<String> replyRooms)
|
||||
{
|
||||
addField(ConfigureNodeFields.replyroom, FormField.TYPE_LIST_MULTI);
|
||||
addField(ConfigureNodeFields.replyroom, FormField.Type.list_multi);
|
||||
setAnswer(ConfigureNodeFields.replyroom.getFieldName(), replyRooms);
|
||||
}
|
||||
|
||||
|
@ -548,7 +548,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setReplyTo(List<String> replyTos)
|
||||
{
|
||||
addField(ConfigureNodeFields.replyto, FormField.TYPE_LIST_MULTI);
|
||||
addField(ConfigureNodeFields.replyto, FormField.Type.list_multi);
|
||||
setAnswer(ConfigureNodeFields.replyto.getFieldName(), replyTos);
|
||||
}
|
||||
|
||||
|
@ -569,7 +569,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setRosterGroupsAllowed(List<String> groups)
|
||||
{
|
||||
addField(ConfigureNodeFields.roster_groups_allowed, FormField.TYPE_LIST_MULTI);
|
||||
addField(ConfigureNodeFields.roster_groups_allowed, FormField.Type.list_multi);
|
||||
setAnswer(ConfigureNodeFields.roster_groups_allowed.getFieldName(), groups);
|
||||
}
|
||||
|
||||
|
@ -601,7 +601,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setSubscribe(boolean subscribe)
|
||||
{
|
||||
addField(ConfigureNodeFields.subscribe, FormField.TYPE_BOOLEAN);
|
||||
addField(ConfigureNodeFields.subscribe, FormField.Type.bool);
|
||||
setAnswer(ConfigureNodeFields.subscribe.getFieldName(), subscribe);
|
||||
}
|
||||
|
||||
|
@ -622,7 +622,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setTitle(String title)
|
||||
{
|
||||
addField(ConfigureNodeFields.title, FormField.TYPE_TEXT_SINGLE);
|
||||
addField(ConfigureNodeFields.title, FormField.Type.text_single);
|
||||
setAnswer(ConfigureNodeFields.title.getFieldName(), title);
|
||||
}
|
||||
|
||||
|
@ -643,7 +643,7 @@ public class ConfigureForm extends Form
|
|||
*/
|
||||
public void setDataType(String type)
|
||||
{
|
||||
addField(ConfigureNodeFields.type, FormField.TYPE_TEXT_SINGLE);
|
||||
addField(ConfigureNodeFields.type, FormField.Type.text_single);
|
||||
setAnswer(ConfigureNodeFields.type.getFieldName(), type);
|
||||
}
|
||||
|
||||
|
@ -695,7 +695,7 @@ public class ConfigureForm extends Form
|
|||
return formField.getValues();
|
||||
}
|
||||
|
||||
private void addField(ConfigureNodeFields nodeField, String type)
|
||||
private void addField(ConfigureNodeFields nodeField, FormField.Type type)
|
||||
{
|
||||
String fieldName = nodeField.getFieldName();
|
||||
|
||||
|
|
|
@ -72,7 +72,7 @@ public class SubscribeForm extends Form
|
|||
*/
|
||||
public void setDeliverOn(boolean deliverNotifications)
|
||||
{
|
||||
addField(SubscribeOptionFields.deliver, FormField.TYPE_BOOLEAN);
|
||||
addField(SubscribeOptionFields.deliver, FormField.Type.bool);
|
||||
setAnswer(SubscribeOptionFields.deliver.getFieldName(), deliverNotifications);
|
||||
}
|
||||
|
||||
|
@ -93,7 +93,7 @@ public class SubscribeForm extends Form
|
|||
*/
|
||||
public void setDigestOn(boolean digestOn)
|
||||
{
|
||||
addField(SubscribeOptionFields.deliver, FormField.TYPE_BOOLEAN);
|
||||
addField(SubscribeOptionFields.deliver, FormField.Type.bool);
|
||||
setAnswer(SubscribeOptionFields.deliver.getFieldName(), digestOn);
|
||||
}
|
||||
|
||||
|
@ -114,7 +114,7 @@ public class SubscribeForm extends Form
|
|||
*/
|
||||
public void setDigestFrequency(int frequency)
|
||||
{
|
||||
addField(SubscribeOptionFields.digest_frequency, FormField.TYPE_TEXT_SINGLE);
|
||||
addField(SubscribeOptionFields.digest_frequency, FormField.Type.text_single);
|
||||
setAnswer(SubscribeOptionFields.digest_frequency.getFieldName(), frequency);
|
||||
}
|
||||
|
||||
|
@ -145,7 +145,7 @@ public class SubscribeForm extends Form
|
|||
*/
|
||||
public void setExpiry(Date expire)
|
||||
{
|
||||
addField(SubscribeOptionFields.expire, FormField.TYPE_TEXT_SINGLE);
|
||||
addField(SubscribeOptionFields.expire, FormField.Type.text_single);
|
||||
setAnswer(SubscribeOptionFields.expire.getFieldName(), XmppDateTime.formatXEP0082Date(expire));
|
||||
}
|
||||
|
||||
|
@ -168,7 +168,7 @@ public class SubscribeForm extends Form
|
|||
*/
|
||||
public void setIncludeBody(boolean include)
|
||||
{
|
||||
addField(SubscribeOptionFields.include_body, FormField.TYPE_BOOLEAN);
|
||||
addField(SubscribeOptionFields.include_body, FormField.Type.bool);
|
||||
setAnswer(SubscribeOptionFields.include_body.getFieldName(), include);
|
||||
}
|
||||
|
||||
|
@ -203,7 +203,7 @@ public class SubscribeForm extends Form
|
|||
{
|
||||
values.add(state.toString());
|
||||
}
|
||||
addField(SubscribeOptionFields.show_values, FormField.TYPE_LIST_MULTI);
|
||||
addField(SubscribeOptionFields.show_values, FormField.Type.list_multi);
|
||||
setAnswer(SubscribeOptionFields.show_values.getFieldName(), values);
|
||||
}
|
||||
|
||||
|
@ -227,7 +227,7 @@ public class SubscribeForm extends Form
|
|||
return formField.getValues();
|
||||
}
|
||||
|
||||
private void addField(SubscribeOptionFields nodeField, String type)
|
||||
private void addField(SubscribeOptionFields nodeField, FormField.Type type)
|
||||
{
|
||||
String fieldName = nodeField.getFieldName();
|
||||
|
||||
|
|
|
@ -143,9 +143,9 @@ public class ReportedData {
|
|||
* @author Gaston Dombiak
|
||||
*/
|
||||
public static class Column {
|
||||
private String label;
|
||||
private String variable;
|
||||
private String type;
|
||||
private final String label;
|
||||
private final String variable;
|
||||
private final FormField.Type type;
|
||||
|
||||
/**
|
||||
* Creates a new column with the specified definition.
|
||||
|
@ -154,7 +154,7 @@ public class ReportedData {
|
|||
* @param variable the variable name of the column.
|
||||
* @param type the format for the returned data.
|
||||
*/
|
||||
public Column(String label, String variable, String type) {
|
||||
public Column(String label, String variable, FormField.Type type) {
|
||||
this.label = label;
|
||||
this.variable = variable;
|
||||
this.type = type;
|
||||
|
@ -171,27 +171,11 @@ public class ReportedData {
|
|||
|
||||
|
||||
/**
|
||||
* Returns the column's data format. Valid formats are:
|
||||
*
|
||||
* <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>
|
||||
* Returns the column's data format.
|
||||
*
|
||||
* @return format for the returned data.
|
||||
*/
|
||||
public String getType() {
|
||||
public FormField.Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
|
|
@ -93,7 +93,7 @@ class SimpleUserSearch extends IQ {
|
|||
|
||||
protected void parseItems(XmlPullParser parser) throws XmlPullParserException, IOException {
|
||||
ReportedData data = new ReportedData();
|
||||
data.addColumn(new ReportedData.Column("JID", "jid", "text-single"));
|
||||
data.addColumn(new ReportedData.Column("JID", "jid", FormField.Type.text_single));
|
||||
|
||||
boolean done = false;
|
||||
|
||||
|
@ -135,7 +135,7 @@ class SimpleUserSearch extends IQ {
|
|||
|
||||
// Column name should be the same
|
||||
if (!exists) {
|
||||
ReportedData.Column column = new ReportedData.Column(name, name, "text-single");
|
||||
ReportedData.Column column = new ReportedData.Column(name, name, FormField.Type.text_single);
|
||||
data.addColumn(column);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -185,7 +185,7 @@ public class UserSearch extends SimpleIQ {
|
|||
field.setLabel("Nickname");
|
||||
}
|
||||
|
||||
field.setType(FormField.TYPE_TEXT_SINGLE);
|
||||
field.setType(FormField.Type.text_single);
|
||||
dataForm.addField(field);
|
||||
}
|
||||
else if (eventType == XmlPullParser.END_TAG) {
|
||||
|
|
|
@ -125,11 +125,14 @@ public class Form {
|
|||
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())) {
|
||||
switch (field.getType()) {
|
||||
case text_multi:
|
||||
case text_private:
|
||||
case text_single:
|
||||
case jid_single:
|
||||
case hidden:
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("This field is not of type String.");
|
||||
}
|
||||
setAnswer(field, value);
|
||||
|
@ -151,11 +154,7 @@ public class Form {
|
|||
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.");
|
||||
}
|
||||
validateThatFieldIsText(field);
|
||||
setAnswer(field, value);
|
||||
}
|
||||
|
||||
|
@ -175,11 +174,7 @@ public class Form {
|
|||
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.");
|
||||
}
|
||||
validateThatFieldIsText(field);
|
||||
setAnswer(field, value);
|
||||
}
|
||||
|
||||
|
@ -199,11 +194,7 @@ public class Form {
|
|||
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.");
|
||||
}
|
||||
validateThatFieldIsText(field);
|
||||
setAnswer(field, value);
|
||||
}
|
||||
|
||||
|
@ -223,14 +214,21 @@ public class Form {
|
|||
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.");
|
||||
}
|
||||
validateThatFieldIsText(field);
|
||||
setAnswer(field, value);
|
||||
}
|
||||
|
||||
private static void validateThatFieldIsText(FormField field) {
|
||||
switch(field.getType()) {
|
||||
case text_multi:
|
||||
case text_private:
|
||||
case text_single:
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("This field is not of type text (multi, private or single).");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a new boolean value to a given form's field. The field whose variable matches the
|
||||
* requested variable will be completed with the specified value. If no field could be found
|
||||
|
@ -247,7 +245,7 @@ public class Form {
|
|||
if (field == null) {
|
||||
throw new IllegalArgumentException("Field not found for the specified variable name.");
|
||||
}
|
||||
if (!FormField.TYPE_BOOLEAN.equals(field.getType())) {
|
||||
if (field.getType() != FormField.Type.bool) {
|
||||
throw new IllegalArgumentException("This field is not of type boolean.");
|
||||
}
|
||||
setAnswer(field, (value ? "1" : "0"));
|
||||
|
@ -300,11 +298,14 @@ public class Form {
|
|||
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())) {
|
||||
switch (field.getType()) {
|
||||
case jid_multi:
|
||||
case list_multi:
|
||||
case list_single:
|
||||
case text_multi:
|
||||
case hidden:
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("This field only accept list of values.");
|
||||
}
|
||||
// Clear the old values
|
||||
|
@ -520,7 +521,7 @@ public class Form {
|
|||
newField.setType(field.getType());
|
||||
form.addField(newField);
|
||||
// Set the answer ONLY to the hidden fields
|
||||
if (FormField.TYPE_HIDDEN.equals(field.getType())) {
|
||||
if (field.getType() == FormField.Type.hidden) {
|
||||
// Since a hidden field could have many values we need to collect them
|
||||
// in a list
|
||||
List<String> values = new ArrayList<String>();
|
||||
|
|
|
@ -34,22 +34,94 @@ public class FormField {
|
|||
|
||||
public static final String ELEMENT = "field";
|
||||
|
||||
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";
|
||||
/**
|
||||
* Form Field Types as defined in XEP-4 § 3.3.
|
||||
*
|
||||
* @see <a href="http://xmpp.org/extensions/xep-0004.html#protocol-fieldtypes">XEP-4 § 3.3 Field Types</a>
|
||||
*/
|
||||
public enum Type {
|
||||
|
||||
/**
|
||||
* Boolean type. Can be 0 or 1, true or false, yes or no. Default value is 0.
|
||||
* <p>
|
||||
* Note that in XEP-4 this type is called 'boolean', but since that String is a restricted keyword in Java, it
|
||||
* is named 'bool' in Smack.
|
||||
* </p>
|
||||
*/
|
||||
bool,
|
||||
|
||||
/**
|
||||
* Fixed for putting in text to show sections, or just advertise your web site in the middle of the form
|
||||
*/
|
||||
fixed,
|
||||
|
||||
/**
|
||||
* Is not given to the user at all, but returned with the questionnaire
|
||||
*/
|
||||
hidden,
|
||||
|
||||
/**
|
||||
* multiple entries for JIDs
|
||||
*/
|
||||
jid_multi,
|
||||
|
||||
/**
|
||||
* Jabber ID - choosing a JID from your roster, and entering one based on the rules for a JID.
|
||||
*/
|
||||
jid_single,
|
||||
|
||||
/**
|
||||
* Given a list of choices, pick one or more.
|
||||
*/
|
||||
list_multi,
|
||||
|
||||
/**
|
||||
* Given a list of choices, pick one.
|
||||
*/
|
||||
list_single,
|
||||
|
||||
/**
|
||||
* Multiple lines of text entry.
|
||||
*/
|
||||
text_multi,
|
||||
|
||||
/**
|
||||
* Instead of showing the user what they typed, you show ***** to protect it.
|
||||
*/
|
||||
text_private,
|
||||
|
||||
/**
|
||||
* Single line or word of text.
|
||||
*/
|
||||
text_single,
|
||||
;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
switch (this) {
|
||||
case bool:
|
||||
return "boolean";
|
||||
default:
|
||||
return this.name().replace('_', '-');
|
||||
}
|
||||
}
|
||||
|
||||
public static Type fromString(String string) {
|
||||
switch (string) {
|
||||
case "boolean":
|
||||
return bool;
|
||||
default:
|
||||
string = string.replace('-', '_');
|
||||
return Type.valueOf(string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String description;
|
||||
private boolean required = false;
|
||||
private String label;
|
||||
private String variable;
|
||||
private String type;
|
||||
private Type type;
|
||||
private final List<Option> options = new ArrayList<Option>();
|
||||
private final List<String> values = new ArrayList<String>();
|
||||
|
||||
|
@ -68,7 +140,7 @@ public class FormField {
|
|||
* name.
|
||||
*/
|
||||
public FormField() {
|
||||
this.type = FormField.TYPE_FIXED;
|
||||
this.type = Type.fixed;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -116,27 +188,12 @@ public class FormField {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
* Returns an indicative of the format for the data to answer.
|
||||
*
|
||||
* @return format for the data to answer.
|
||||
* @see Type
|
||||
*/
|
||||
public String getType() {
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
@ -195,27 +252,12 @@ public class FormField {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
* Sets an indicative of the format for the data to answer.
|
||||
*
|
||||
* @param type an indicative of the format for the data to answer.
|
||||
* @see Type
|
||||
*/
|
||||
public void setType(String type) {
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
|
|
|
@ -215,8 +215,10 @@ public class DataForm implements PacketExtension {
|
|||
public boolean hasHiddenFormTypeField() {
|
||||
boolean found = false;
|
||||
for (FormField f : fields) {
|
||||
if (f.getVariable().equals("FORM_TYPE") && f.getType() != null && f.getType().equals("hidden"))
|
||||
if (f.getVariable().equals("FORM_TYPE") && f.getType() == FormField.Type.hidden) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
|
|
@ -93,7 +93,10 @@ public class DataFormProvider extends PacketExtensionProvider<DataForm> {
|
|||
final int initialDepth = parser.getDepth();
|
||||
FormField formField = new FormField(parser.getAttributeValue("", "var"));
|
||||
formField.setLabel(parser.getAttributeValue("", "label"));
|
||||
formField.setType(parser.getAttributeValue("", "type"));
|
||||
String typeString = parser.getAttributeValue("", "type");
|
||||
if (typeString != null) {
|
||||
formField.setType(FormField.Type.fromString(typeString));
|
||||
}
|
||||
outerloop: while (true) {
|
||||
int eventType = parser.next();
|
||||
switch (eventType) {
|
||||
|
|
|
@ -123,7 +123,7 @@ public class EntityCapsManagerTest extends InitExtensions {
|
|||
df.addField(ff);
|
||||
|
||||
ff = new FormField("FORM_TYPE");
|
||||
ff.setType("hidden");
|
||||
ff.setType(FormField.Type.hidden);
|
||||
ff.addValue("urn:xmpp:dataforms:softwareinfo");
|
||||
df.addField(ff);
|
||||
|
||||
|
@ -180,7 +180,7 @@ public class EntityCapsManagerTest extends InitExtensions {
|
|||
df.addField(ff);
|
||||
|
||||
ff = new FormField("FORM_TYPE");
|
||||
ff.setType("hidden");
|
||||
ff.setType(FormField.Type.hidden);
|
||||
ff.addValue("urn:xmpp:dataforms:softwareinfo");
|
||||
df.addField(ff);
|
||||
|
||||
|
@ -208,7 +208,7 @@ public class EntityCapsManagerTest extends InitExtensions {
|
|||
df = new DataForm("result");
|
||||
|
||||
ff = new FormField("FORM_TYPE");
|
||||
ff.setType("hidden");
|
||||
ff.setType(FormField.Type.hidden);
|
||||
ff.addValue("urn:xmpp:dataforms:softwareinfo");
|
||||
df.addField(ff);
|
||||
|
||||
|
|
|
@ -388,7 +388,7 @@ public class Workgroup {
|
|||
String value = metadata.get(name).toString();
|
||||
|
||||
FormField field = new FormField(name);
|
||||
field.setType(FormField.TYPE_TEXT_SINGLE);
|
||||
field.setType(FormField.Type.text_single);
|
||||
form.addField(field);
|
||||
form.setAnswer(name, value);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue