1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-12-22 20:47:57 +01:00

Make DataForm type an enum

This commit is contained in:
Florian Schmaus 2015-01-10 20:10:46 +01:00
parent 98c69f6895
commit f1a1215f35
14 changed files with 63 additions and 97 deletions

View file

@ -40,7 +40,6 @@ import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
import org.jivesoftware.smackx.filetransfer.FileTransferException.NoAcceptableTransferMechanisms;
import org.jivesoftware.smackx.filetransfer.FileTransferException.NoStreamMethodsOfferedException;
import org.jivesoftware.smackx.si.packet.StreamInitiation;
import org.jivesoftware.smackx.xdata.Form;
import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.packet.DataForm;
@ -367,7 +366,7 @@ public class FileTransferNegotiator extends Manager {
}
private DataForm createDefaultInitiationForm() {
DataForm form = new DataForm(Form.TYPE_FORM);
DataForm form = new DataForm(DataForm.Type.form);
FormField field = new FormField(STREAM_DATA_FIELD_NAME);
field.setType(FormField.Type.list_single);
if (!IBB_ONLY) {

View file

@ -27,7 +27,6 @@ import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.si.packet.StreamInitiation;
import org.jivesoftware.smackx.xdata.Form;
import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdata.packet.DataForm;
@ -61,7 +60,7 @@ public abstract class StreamNegotiator {
response.setType(IQ.Type.result);
response.setPacketID(streamInitiationOffer.getPacketID());
DataForm form = new DataForm(Form.TYPE_SUBMIT);
DataForm form = new DataForm(DataForm.Type.submit);
FormField field = new FormField(
FileTransferNegotiator.STREAM_DATA_FIELD_NAME);
for (String namespace : namespaces) {

View file

@ -65,9 +65,9 @@ public class ConfigureForm extends Form
* using the resulting form to create a answer form. See {@link #ConfigureForm(Form)}.
* @param formType
*/
public ConfigureForm(FormType formType)
public ConfigureForm(DataForm.Type formType)
{
super(formType.toString());
super(formType);
}
/**

View file

@ -1,29 +0,0 @@
/**
*
* Copyright the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package org.jivesoftware.smackx.pubsub;
import org.jivesoftware.smackx.xdata.Form;
/**
* Defines the allowable types for a {@link Form}
*
* @author Robin Collier
*/
public enum FormType
{
form, submit, cancel, result;
}

View file

@ -50,9 +50,9 @@ public class SubscribeForm extends Form
super(subscribeOptionsForm.getDataFormToSend());
}
public SubscribeForm(FormType formType)
public SubscribeForm(DataForm.Type formType)
{
super(formType.toString());
super(formType);
}
/**

View file

@ -160,7 +160,7 @@ public class UserSearch extends SimpleIQ {
private static void buildDataForm(SimpleUserSearch search,
String instructions, XmlPullParser parser)
throws XmlPullParserException, IOException, SmackException {
DataForm dataForm = new DataForm(Form.TYPE_FORM);
DataForm dataForm = new DataForm(DataForm.Type.form);
boolean done = false;
dataForm.setTitle("User Search");
dataForm.addInstruction(instructions);

View file

@ -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

View file

@ -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;
}

View file

@ -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) {

View file

@ -116,7 +116,7 @@ public class EntityCapsManagerTest extends InitExtensions {
di.addFeature("http://jabber.org/protocol/muc");
di.addFeature("http://jabber.org/protocol/disco#info");
DataForm df = new DataForm("result");
DataForm df = new DataForm(DataForm.Type.result);
FormField ff = new FormField("os");
ff.addValue("Mac");
@ -173,7 +173,7 @@ public class EntityCapsManagerTest extends InitExtensions {
// Failure 2: Duplicate features
di.addFeature("http://jabber.org/protocol/disco#info");
DataForm df = new DataForm("result");
DataForm df = new DataForm(DataForm.Type.result);
FormField ff = new FormField("os");
ff.addValue("Mac");
@ -205,7 +205,7 @@ public class EntityCapsManagerTest extends InitExtensions {
// Failure 3: Another service discovery information form with the same
// FORM_TYPE
df = new DataForm("result");
df = new DataForm(DataForm.Type.result);
ff = new FormField("FORM_TYPE");
ff.setType(FormField.Type.hidden);

View file

@ -28,7 +28,7 @@ import org.junit.Test;
public class RoomInfoTest {
@Test
public void validateRoomWithEmptyForm() {
DataForm dataForm = new DataForm("result");
DataForm dataForm = new DataForm(DataForm.Type.result);
DiscoverInfo discoInfo = new DiscoverInfo();
discoInfo.addExtension(dataForm);
@ -40,7 +40,7 @@ public class RoomInfoTest {
@Test
public void validateRoomWithForm() {
DataForm dataForm = new DataForm("result");
DataForm dataForm = new DataForm(DataForm.Type.result);
FormField desc = new FormField("muc#roominfo_description");
desc.addValue("The place for all good witches!");

View file

@ -30,6 +30,7 @@ import org.jivesoftware.smack.packet.XMPPError.Condition;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
import org.jivesoftware.smackx.pubsub.packet.PubSub;
import org.jivesoftware.smackx.xdata.packet.DataForm;
import org.junit.Assert;
import org.junit.Test;
@ -43,7 +44,7 @@ public class ConfigureFormTest
@Test
public void checkChildrenAssocPolicy()
{
ConfigureForm form = new ConfigureForm(FormType.submit);
ConfigureForm form = new ConfigureForm(DataForm.Type.submit);
form.setChildrenAssociationPolicy(ChildrenAssociationPolicy.owners);
assertEquals(ChildrenAssociationPolicy.owners, form.getChildrenAssociationPolicy());
}

View file

@ -44,14 +44,14 @@ import org.xmlpull.v1.XmlPullParserException;
*
*/
public class DataFormTest {
private static final String TEST_OUTPUT_1 = "<x xmlns='jabber:x:data' type='SUBMIT'><instructions>InstructionTest1</instructions><field var='testField1'></field></x>";
private static final String TEST_OUTPUT_2 = "<x xmlns='jabber:x:data' type='SUBMIT'><instructions>InstructionTest1</instructions><field var='testField1'></field><page xmlns='http://jabber.org/protocol/xdata-layout' label='Label'><fieldref var='testField1'/><section label='section Label'><text>SectionText</text></section><text>PageText</text></page></x>";
private static final String TEST_OUTPUT_3 = "<x xmlns='jabber:x:data' type='SUBMIT'><instructions>InstructionTest1</instructions><field var='testField1'><validate xmlns='http://jabber.org/protocol/xdata-validate' datatype='xs:integer'><range min='1111' max='9999'/></validate></field></x>";
private static final String TEST_OUTPUT_1 = "<x xmlns='jabber:x:data' type='submit'><instructions>InstructionTest1</instructions><field var='testField1'></field></x>";
private static final String TEST_OUTPUT_2 = "<x xmlns='jabber:x:data' type='submit'><instructions>InstructionTest1</instructions><field var='testField1'></field><page xmlns='http://jabber.org/protocol/xdata-layout' label='Label'><fieldref var='testField1'/><section label='section Label'><text>SectionText</text></section><text>PageText</text></page></x>";
private static final String TEST_OUTPUT_3 = "<x xmlns='jabber:x:data' type='submit'><instructions>InstructionTest1</instructions><field var='testField1'><validate xmlns='http://jabber.org/protocol/xdata-validate' datatype='xs:integer'><range min='1111' max='9999'/></validate></field></x>";
@Test
public void test() throws XmlPullParserException, IOException, SmackException {
//Build a Form
DataForm df = new DataForm("SUBMIT");
DataForm df = new DataForm(DataForm.Type.submit);
String instruction = "InstructionTest1";
df.addInstruction(instruction);
FormField field = new FormField("testField1");
@ -80,7 +80,7 @@ public class DataFormTest {
@Test
public void testLayout() throws XmlPullParserException, IOException, SmackException {
//Build a Form
DataForm df = new DataForm("SUBMIT");
DataForm df = new DataForm(DataForm.Type.submit);
String instruction = "InstructionTest1";
df.addInstruction(instruction);
FormField field = new FormField("testField1");
@ -124,7 +124,7 @@ public class DataFormTest {
@Test
public void testValidation() throws XmlPullParserException, IOException, SmackException {
//Build a Form
DataForm df = new DataForm("SUBMIT");
DataForm df = new DataForm(DataForm.Type.submit);
String instruction = "InstructionTest1";
df.addInstruction(instruction);
FormField field = new FormField("testField1");

View file

@ -382,7 +382,7 @@ public class Workgroup {
}
// Build dataform from metadata
Form form = new Form(Form.TYPE_SUBMIT);
Form form = new Form(DataForm.Type.submit);
Iterator<String> iter = metadata.keySet().iterator();
while (iter.hasNext()) {
String name = iter.next();