mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-12-22 20:47:57 +01:00
Fix DataFormProvider parsing 'fixed' FormFields
b71039660b
made FormField.setType(Type)
throw an illegal argument exception if type is fixed, but the
DataFormProvider was not changed, so it still would call
setType(Type.fixed).
Change DataFormProvider so that the non-argument constructer of
FormField is used when type == fixed.
This commit is contained in:
parent
616768a5a9
commit
2cba6a68eb
3 changed files with 30 additions and 12 deletions
|
@ -113,7 +113,16 @@ public class FormField implements NamedElement {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a form field type from the given string. If <code>string</code> is null, then null will be returned.
|
||||
*
|
||||
* @param string the string to transform or null.
|
||||
* @return the type or null.
|
||||
*/
|
||||
public static Type fromString(String string) {
|
||||
if (string == null) {
|
||||
return null;
|
||||
}
|
||||
switch (string) {
|
||||
case "boolean":
|
||||
return bool;
|
||||
|
|
|
@ -94,12 +94,18 @@ public class DataFormProvider extends PacketExtensionProvider<DataForm> {
|
|||
|
||||
private FormField parseField(XmlPullParser parser) throws XmlPullParserException, IOException {
|
||||
final int initialDepth = parser.getDepth();
|
||||
FormField formField = new FormField(parser.getAttributeValue("", "var"));
|
||||
formField.setLabel(parser.getAttributeValue("", "label"));
|
||||
String typeString = parser.getAttributeValue("", "type");
|
||||
if (typeString != null) {
|
||||
formField.setType(FormField.Type.fromString(typeString));
|
||||
final String var = parser.getAttributeValue("", "var");
|
||||
final FormField.Type type = FormField.Type.fromString(parser.getAttributeValue("", "type"));
|
||||
|
||||
final FormField formField;
|
||||
if (type == FormField.Type.fixed) {
|
||||
formField = new FormField();
|
||||
} else {
|
||||
formField = new FormField(var);
|
||||
formField.setType(type);
|
||||
}
|
||||
formField.setLabel(parser.getAttributeValue("", "label"));
|
||||
|
||||
outerloop: while (true) {
|
||||
int eventType = parser.next();
|
||||
switch (eventType) {
|
||||
|
|
|
@ -25,6 +25,7 @@ import org.jivesoftware.smack.SmackException;
|
|||
import org.jivesoftware.smack.packet.Element;
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
import org.jivesoftware.smackx.xdata.FormField;
|
||||
import org.jivesoftware.smackx.xdata.FormField.Type;
|
||||
import org.jivesoftware.smackx.xdata.provider.DataFormProvider;
|
||||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout;
|
||||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Fieldref;
|
||||
|
@ -32,7 +33,6 @@ import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Section;
|
|||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Text;
|
||||
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement;
|
||||
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
@ -48,6 +48,8 @@ public class DataFormTest {
|
|||
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>";
|
||||
|
||||
DataFormProvider pr = new DataFormProvider();
|
||||
|
||||
@Test
|
||||
public void test() throws XmlPullParserException, IOException, SmackException {
|
||||
//Build a Form
|
||||
|
@ -61,8 +63,6 @@ public class DataFormTest {
|
|||
String output = df.toXML().toString();
|
||||
assertEquals(TEST_OUTPUT_1, output);
|
||||
|
||||
DataFormProvider pr = new DataFormProvider();
|
||||
|
||||
XmlPullParser parser = PacketParserUtils.getParserFor(output);
|
||||
|
||||
df = pr.parse(parser);
|
||||
|
@ -101,8 +101,6 @@ public class DataFormTest {
|
|||
String output = df.toXML().toString();
|
||||
assertEquals(TEST_OUTPUT_2, output);
|
||||
|
||||
DataFormProvider pr = new DataFormProvider();
|
||||
|
||||
XmlPullParser parser = PacketParserUtils.getParserFor(output);
|
||||
|
||||
df = pr.parse(parser);
|
||||
|
@ -137,8 +135,6 @@ public class DataFormTest {
|
|||
String output = df.toXML().toString();
|
||||
assertEquals(TEST_OUTPUT_3, output);
|
||||
|
||||
DataFormProvider pr = new DataFormProvider();
|
||||
|
||||
XmlPullParser parser = PacketParserUtils.getParserFor(output);
|
||||
|
||||
df = pr.parse(parser);
|
||||
|
@ -156,4 +152,11 @@ public class DataFormTest {
|
|||
output = df.toXML().toString();
|
||||
assertEquals(TEST_OUTPUT_3, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFixedField() throws XmlPullParserException, IOException, SmackException {
|
||||
final String formWithFixedField = "<x xmlns='jabber:x:data' type='submit'><instructions>InstructionTest1</instructions><field type='fixed'></field></x>";
|
||||
DataForm df = pr.parse(PacketParserUtils.getParserFor(formWithFixedField));
|
||||
assertEquals(Type.fixed, df.getFields().get(0).getType());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue