mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-12-14 06:51:08 +01:00
Make DataForm type an enum
This commit is contained in:
parent
98c69f6895
commit
f1a1215f35
14 changed files with 63 additions and 97 deletions
|
|
@ -43,11 +43,6 @@ import org.jivesoftware.smackx.xdata.packet.DataForm;
|
|||
*/
|
||||
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";
|
||||
|
||||
private DataForm dataForm;
|
||||
|
||||
/**
|
||||
|
|
@ -80,20 +75,11 @@ public class Form {
|
|||
}
|
||||
|
||||
/**
|
||||
* 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>
|
||||
*
|
||||
* Creates a new Form of a given type from scratch.
|
||||
*
|
||||
* @param type the form's type (e.g. form, submit,cancel,result).
|
||||
*/
|
||||
public Form(String type) {
|
||||
public Form(DataForm.Type type) {
|
||||
this.dataForm = new DataForm(type);
|
||||
}
|
||||
|
||||
|
|
@ -408,20 +394,11 @@ public class Form {
|
|||
|
||||
/**
|
||||
* 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>
|
||||
* to fill out, a form submission or data results.
|
||||
*
|
||||
* @return the form's type.
|
||||
*/
|
||||
public String getType() {
|
||||
public DataForm.Type getType() {
|
||||
return dataForm.getType();
|
||||
}
|
||||
|
||||
|
|
@ -481,7 +458,7 @@ public class Form {
|
|||
* @return if the form is a form to fill out.
|
||||
*/
|
||||
private boolean isFormType() {
|
||||
return TYPE_FORM.equals(dataForm.getType());
|
||||
return DataForm.Type.form == dataForm.getType();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -490,7 +467,7 @@ public class Form {
|
|||
* @return if the form is a form to submit.
|
||||
*/
|
||||
private boolean isSubmitType() {
|
||||
return TYPE_SUBMIT.equals(dataForm.getType());
|
||||
return DataForm.Type.submit == dataForm.getType();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -512,7 +489,7 @@ public class Form {
|
|||
throw new IllegalStateException("Only forms of type \"form\" could be answered");
|
||||
}
|
||||
// Create a new Form
|
||||
Form form = new Form(TYPE_SUBMIT);
|
||||
Form form = new Form(DataForm.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
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import org.jivesoftware.smackx.xdata.FormField;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Represents a form that could be use for gathering data as well as for reporting data
|
||||
|
|
@ -38,7 +39,34 @@ public class DataForm implements PacketExtension {
|
|||
public static final String NAMESPACE = "jabber:x:data";
|
||||
public static final String ELEMENT = "x";
|
||||
|
||||
private String type;
|
||||
public enum Type {
|
||||
/**
|
||||
* This packet contains a form to fill out. Display it to the user (if your program can).
|
||||
*/
|
||||
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;
|
||||
private String title;
|
||||
private List<String> instructions = new ArrayList<String>();
|
||||
private ReportedData reportedData;
|
||||
|
|
@ -46,27 +74,17 @@ public class DataForm implements PacketExtension {
|
|||
private final List<FormField> fields = new ArrayList<FormField>();
|
||||
private final List<Element> extensionElements = new ArrayList<Element>();
|
||||
|
||||
public DataForm(String type) {
|
||||
public DataForm(Type 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>
|
||||
* to fill out, a form submission or data results.
|
||||
*
|
||||
* @return the form's type.
|
||||
*/
|
||||
public String getType() {
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -44,7 +44,8 @@ public class DataFormProvider extends PacketExtensionProvider<DataForm> {
|
|||
@Override
|
||||
public DataForm parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException,
|
||||
SmackException {
|
||||
DataForm dataForm = new DataForm(parser.getAttributeValue("", "type"));
|
||||
DataForm.Type dataFormType = DataForm.Type.fromString(parser.getAttributeValue("", "type"));
|
||||
DataForm dataForm = new DataForm(dataFormType);
|
||||
outerloop: while (true) {
|
||||
int eventType = parser.next();
|
||||
switch (eventType) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue