mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-12-14 06:51:08 +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
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue