From f1a1215f35bd4ba2eaed9d9e9e5a6ca8e520d54a Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sat, 10 Jan 2015 20:10:46 +0100 Subject: [PATCH] Make DataForm type an enum --- .../filetransfer/FileTransferNegotiator.java | 3 +- .../smackx/filetransfer/StreamNegotiator.java | 3 +- .../smackx/pubsub/ConfigureForm.java | 4 +- .../jivesoftware/smackx/pubsub/FormType.java | 29 ------------ .../smackx/pubsub/SubscribeForm.java | 4 +- .../smackx/search/UserSearch.java | 2 +- .../org/jivesoftware/smackx/xdata/Form.java | 39 ++++------------ .../smackx/xdata/packet/DataForm.java | 46 +++++++++++++------ .../xdata/provider/DataFormProvider.java | 3 +- .../smackx/caps/EntityCapsManagerTest.java | 6 +-- .../jivesoftware/smackx/muc/RoomInfoTest.java | 4 +- .../smackx/pubsub/ConfigureFormTest.java | 3 +- .../smackx/xdata/packet/DataFormTest.java | 12 ++--- .../smackx/workgroup/user/Workgroup.java | 2 +- 14 files changed, 63 insertions(+), 97 deletions(-) delete mode 100644 smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/FormType.java diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransferNegotiator.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransferNegotiator.java index b4a7ae253..4fb202ace 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransferNegotiator.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FileTransferNegotiator.java @@ -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) { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/StreamNegotiator.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/StreamNegotiator.java index 92dc82878..40658f5fa 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/StreamNegotiator.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/StreamNegotiator.java @@ -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) { diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/ConfigureForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/ConfigureForm.java index a7adfa37a..e7639cdd8 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/ConfigureForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/ConfigureForm.java @@ -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); } /** diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/FormType.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/FormType.java deleted file mode 100644 index 855c8dbbf..000000000 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/FormType.java +++ /dev/null @@ -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; -} diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/SubscribeForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/SubscribeForm.java index ecda366c9..f10b7f4d5 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/SubscribeForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/SubscribeForm.java @@ -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); } /** diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/search/UserSearch.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/search/UserSearch.java index f4cae192b..5c58aa6dd 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/search/UserSearch.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/search/UserSearch.java @@ -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); diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/Form.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/Form.java index 81bc69e53..d474ae938 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/Form.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/Form.java @@ -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.

- * - * Possible form types are: - *

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

- * - * Possible form types are: - *

+ * 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 diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java index ecba14505..2f2d5d3e5 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/packet/DataForm.java @@ -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 instructions = new ArrayList(); private ReportedData reportedData; @@ -46,27 +74,17 @@ public class DataForm implements PacketExtension { private final List fields = new ArrayList(); private final List extensionElements = new ArrayList(); - 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.

- * - * Possible form types are: - *

    - *
  • form -> This packet contains a form to fill out. Display it to the user (if your - * program can).
  • - *
  • 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.
  • - *
+ * to fill out, a form submission or data results. * * @return the form's type. */ - public String getType() { + public Type getType() { return type; } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java index 5c0184f9c..a5ad417c4 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/xdata/provider/DataFormProvider.java @@ -44,7 +44,8 @@ public class DataFormProvider extends PacketExtensionProvider { @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) { diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/caps/EntityCapsManagerTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/caps/EntityCapsManagerTest.java index 558128923..3ba2bbacf 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/caps/EntityCapsManagerTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/caps/EntityCapsManagerTest.java @@ -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); diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/muc/RoomInfoTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/muc/RoomInfoTest.java index 3da038710..e70d50e0f 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/muc/RoomInfoTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/muc/RoomInfoTest.java @@ -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!"); diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ConfigureFormTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ConfigureFormTest.java index 8639a3e5e..dc6074b81 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ConfigureFormTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ConfigureFormTest.java @@ -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()); } diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/packet/DataFormTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/packet/DataFormTest.java index d21a3cf8e..0424dd7db 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/packet/DataFormTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/xdata/packet/DataFormTest.java @@ -44,14 +44,14 @@ import org.xmlpull.v1.XmlPullParserException; * */ public class DataFormTest { - private static final String TEST_OUTPUT_1 = "InstructionTest1"; - private static final String TEST_OUTPUT_2 = "InstructionTest1
SectionText
PageText
"; - private static final String TEST_OUTPUT_3 = "InstructionTest1"; + private static final String TEST_OUTPUT_1 = "InstructionTest1"; + private static final String TEST_OUTPUT_2 = "InstructionTest1
SectionText
PageText
"; + private static final String TEST_OUTPUT_3 = "InstructionTest1"; @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"); diff --git a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/user/Workgroup.java b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/user/Workgroup.java index 4fa29e65f..eb616e58c 100644 --- a/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/user/Workgroup.java +++ b/smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/user/Workgroup.java @@ -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 iter = metadata.keySet().iterator(); while (iter.hasNext()) { String name = iter.next();