Search in sources :

Example 26 with FormField

use of org.jivesoftware.smackx.xdata.FormField in project Smack by igniterealtime.

the class DataFormProvider method parseField.

private static FormField parseField(XmlPullParser parser) throws XmlPullParserException, IOException {
    final int initialDepth = parser.getDepth();
    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) {
            case XmlPullParser.START_TAG:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                switch(name) {
                    case "desc":
                        formField.setDescription(parser.nextText());
                        break;
                    case "value":
                        formField.addValue(parser.nextText());
                        break;
                    case "required":
                        formField.setRequired(true);
                        break;
                    case "option":
                        formField.addOption(parseOption(parser));
                        break;
                    // See XEP-122 Data Forms Validation
                    case ValidateElement.ELEMENT:
                        if (namespace.equals(ValidateElement.NAMESPACE)) {
                            formField.setValidateElement(DataValidationProvider.parse(parser));
                        }
                        break;
                }
                break;
            case XmlPullParser.END_TAG:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
        }
    }
    return formField;
}
Also used : FormField(org.jivesoftware.smackx.xdata.FormField)

Example 27 with FormField

use of org.jivesoftware.smackx.xdata.FormField in project Smack by igniterealtime.

the class ConfigureForm method addField.

private void addField(ConfigureNodeFields nodeField, FormField.Type type) {
    String fieldName = nodeField.getFieldName();
    if (getField(fieldName) == null) {
        FormField field = new FormField(fieldName);
        field.setType(type);
        addField(field);
    }
}
Also used : FormField(org.jivesoftware.smackx.xdata.FormField)

Example 28 with FormField

use of org.jivesoftware.smackx.xdata.FormField in project Smack by igniterealtime.

the class DataFormTest method testLayout.

@Test
public void testLayout() throws Exception {
    //Build a Form
    DataForm df = new DataForm(DataForm.Type.submit);
    String instruction = "InstructionTest1";
    df.addInstruction(instruction);
    FormField field = new FormField("testField1");
    df.addField(field);
    DataLayout layout = new DataLayout("Label");
    Fieldref reffield = new Fieldref("testField1");
    layout.getPageLayout().add(reffield);
    Section section = new Section("section Label");
    section.getSectionLayout().add(new Text("SectionText"));
    layout.getPageLayout().add(section);
    layout.getPageLayout().add(new Text("PageText"));
    df.addExtensionElement(layout);
    assertNotNull(df.toXML());
    String output = df.toXML().toString();
    assertEquals(TEST_OUTPUT_2, output);
    XmlPullParser parser = PacketParserUtils.getParserFor(output);
    df = pr.parse(parser);
    assertNotNull(df);
    assertNotNull(df.getExtensionElements());
    assertEquals(1, df.getExtensionElements().size());
    Element element = df.getExtensionElements().get(0);
    assertNotNull(element);
    layout = (DataLayout) element;
    assertEquals(3, layout.getPageLayout().size());
    assertNotNull(df.toXML());
    output = df.toXML().toString();
    assertEquals(TEST_OUTPUT_2, output);
}
Also used : DataLayout(org.jivesoftware.smackx.xdatalayout.packet.DataLayout) Fieldref(org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Fieldref) Element(org.jivesoftware.smack.packet.Element) ValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement) RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) XmlPullParser(org.xmlpull.v1.XmlPullParser) Text(org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Text) FormField(org.jivesoftware.smackx.xdata.FormField) Section(org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Section) Test(org.junit.Test)

Example 29 with FormField

use of org.jivesoftware.smackx.xdata.FormField in project Smack by igniterealtime.

the class DataFormTest method test.

@Test
public void test() throws Exception {
    //Build a Form
    DataForm df = new DataForm(DataForm.Type.submit);
    String instruction = "InstructionTest1";
    df.addInstruction(instruction);
    FormField field = new FormField("testField1");
    df.addField(field);
    assertNotNull(df.toXML());
    String output = df.toXML().toString();
    assertEquals(TEST_OUTPUT_1, output);
    XmlPullParser parser = PacketParserUtils.getParserFor(output);
    df = pr.parse(parser);
    assertNotNull(df);
    assertNotNull(df.getFields());
    assertEquals(1, df.getFields().size());
    assertEquals(1, df.getInstructions().size());
    assertNotNull(df.toXML());
    output = df.toXML().toString();
    assertEquals(TEST_OUTPUT_1, output);
}
Also used : XmlPullParser(org.xmlpull.v1.XmlPullParser) FormField(org.jivesoftware.smackx.xdata.FormField) Test(org.junit.Test)

Example 30 with FormField

use of org.jivesoftware.smackx.xdata.FormField in project Smack by igniterealtime.

the class DataFormTest method testValidation.

@Test
public void testValidation() throws Exception {
    //Build a Form
    DataForm df = new DataForm(DataForm.Type.submit);
    String instruction = "InstructionTest1";
    df.addInstruction(instruction);
    FormField field = new FormField("testField1");
    df.addField(field);
    ValidateElement dv = new RangeValidateElement("xs:integer", "1111", "9999");
    field.setValidateElement(dv);
    assertNotNull(df.toXML());
    String output = df.toXML().toString();
    assertEquals(TEST_OUTPUT_3, output);
    XmlPullParser parser = PacketParserUtils.getParserFor(output);
    df = pr.parse(parser);
    assertNotNull(df);
    assertNotNull(df.getFields());
    assertEquals(1, df.getFields().size());
    Element element = df.getFields().get(0).getValidateElement();
    assertNotNull(element);
    dv = (ValidateElement) element;
    assertEquals("xs:integer", dv.getDatatype());
    assertNotNull(df.toXML());
    output = df.toXML().toString();
    assertEquals(TEST_OUTPUT_3, output);
}
Also used : RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) ValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement) RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) Element(org.jivesoftware.smack.packet.Element) ValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement) RangeValidateElement(org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement) XmlPullParser(org.xmlpull.v1.XmlPullParser) FormField(org.jivesoftware.smackx.xdata.FormField) Test(org.junit.Test)

Aggregations

FormField (org.jivesoftware.smackx.xdata.FormField)45 DataForm (org.jivesoftware.smackx.xdata.packet.DataForm)17 Test (org.junit.Test)10 DiscoverInfo (org.jivesoftware.smackx.disco.packet.DiscoverInfo)6 ArrayList (java.util.ArrayList)4 TreeSet (java.util.TreeSet)3 Element (org.jivesoftware.smack.packet.Element)3 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)3 Form (org.jivesoftware.smackx.xdata.Form)3 RangeValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement)3 XmlPullParser (org.xmlpull.v1.XmlPullParser)3 LinkedList (java.util.LinkedList)2 IQ (org.jivesoftware.smack.packet.IQ)2 RemoteCommand (org.jivesoftware.smackx.commands.RemoteCommand)2 StreamInitiation (org.jivesoftware.smackx.si.packet.StreamInitiation)2 ValidateElement (org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Method (java.lang.reflect.Method)1 MessageDigest (java.security.MessageDigest)1 HashMap (java.util.HashMap)1