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;
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations