use of org.jivesoftware.smackx.xdata.TextSingleFormField in project Smack by igniterealtime.
the class FormFieldRegistry method register.
@SuppressWarnings("ReferenceEquality")
public static void register(DataForm dataForm) {
// TODO: Also allow forms of type 'result'?
if (dataForm.getType() != DataForm.Type.form) {
throw new IllegalArgumentException();
}
String formType = null;
TextSingleFormField hiddenFormTypeField = dataForm.getHiddenFormTypeField();
if (hiddenFormTypeField != null) {
formType = hiddenFormTypeField.getValue();
}
for (FormField formField : dataForm.getFields()) {
// Note that we can compare here by reference equality to skip the hidden form type field.
if (formField == hiddenFormTypeField) {
continue;
}
FormField.Type type = formField.getType();
if (type == FormField.Type.fixed) {
continue;
}
String fieldName = formField.getFieldName();
register(formType, fieldName, type);
}
}
use of org.jivesoftware.smackx.xdata.TextSingleFormField in project Smack by igniterealtime.
the class DataFormProvider method parse.
@Override
public DataForm parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
DataForm.Type dataFormType = DataForm.Type.fromString(parser.getAttributeValue("", "type"));
DataForm.Builder dataForm = DataForm.builder(dataFormType);
String formType = null;
DataForm.ReportedData reportedData = null;
outerloop: while (true) {
XmlPullParser.Event eventType = parser.next();
switch(eventType) {
case START_ELEMENT:
String name = parser.getName();
String namespace = parser.getNamespace();
XmlEnvironment elementXmlEnvironment = XmlEnvironment.from(parser, xmlEnvironment);
switch(name) {
case "instructions":
dataForm.addInstruction(parser.nextText());
break;
case "title":
dataForm.setTitle(parser.nextText());
break;
case "field":
// Note that we parse this form field without any potential reportedData. We only use reportedData
// to lookup form field types of fields under <item/>.
FormField formField = parseField(parser, elementXmlEnvironment, formType);
TextSingleFormField hiddenFormTypeField = formField.asHiddenFormTypeFieldIfPossible();
if (hiddenFormTypeField != null) {
if (formType != null) {
throw new SmackParsingException("Multiple hidden form type fields");
}
formType = hiddenFormTypeField.getValue();
}
dataForm.addField(formField);
break;
case "item":
DataForm.Item item = parseItem(parser, elementXmlEnvironment, formType, reportedData);
dataForm.addItem(item);
break;
case "reported":
if (reportedData != null) {
throw new SmackParsingException("Data form with multiple <reported/> elements");
}
reportedData = parseReported(parser, elementXmlEnvironment, formType);
dataForm.setReportedData(reportedData);
break;
// See XEP-133 Example 32 for a corner case where the data form contains this extension.
case RosterPacket.ELEMENT:
if (namespace.equals(RosterPacket.NAMESPACE)) {
dataForm.addExtensionElement(RosterPacketProvider.INSTANCE.parse(parser));
}
break;
// See XEP-141 Data Forms Layout
case DataLayout.ELEMENT:
if (namespace.equals(DataLayout.NAMESPACE)) {
dataForm.addExtensionElement(DataLayoutProvider.parse(parser));
}
break;
}
break;
case END_ELEMENT:
if (parser.getDepth() == initialDepth) {
break outerloop;
}
break;
default:
// Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
break;
}
}
return dataForm.build();
}
use of org.jivesoftware.smackx.xdata.TextSingleFormField in project Smack by igniterealtime.
the class SoftwareInfoFormTest method faultySoftwareInfoFormsTest.
@Test
public void faultySoftwareInfoFormsTest() {
DataForm.Builder dataFormbuilder = DataForm.builder(DataForm.Type.result);
TextSingleFormField formField = FormField.buildHiddenFormType("faulty_formtype");
dataFormbuilder.addField(formField);
assertThrows(IllegalArgumentException.class, () -> {
SoftwareInfoForm.getBuilder().setDataForm(dataFormbuilder.build()).build();
});
DataForm.Builder builderWithoutFormType = DataForm.builder(DataForm.Type.result);
assertThrows(IllegalArgumentException.class, () -> {
SoftwareInfoForm.getBuilder().setDataForm(builderWithoutFormType.build()).build();
});
}
use of org.jivesoftware.smackx.xdata.TextSingleFormField in project Smack by igniterealtime.
the class SoftwareInfoFormTest method createSoftwareInfoFormUsingDataForm.
public static SoftwareInfoForm createSoftwareInfoFormUsingDataForm() throws URISyntaxException {
DataForm.Builder dataFormBuilder = DataForm.builder(DataForm.Type.result);
TextSingleFormField formField = FormField.buildHiddenFormType(SoftwareInfoForm.FORM_TYPE);
dataFormBuilder.addField(formField);
dataFormBuilder.addField(FormField.builder("icon").addFormFieldChildElement(createMediaElement()).build());
dataFormBuilder.addField(FormField.builder("os").setValue("Windows").build());
dataFormBuilder.addField(FormField.builder("os_version").setValue("XP").build());
dataFormBuilder.addField(FormField.builder("software").setValue("Exodus").build());
dataFormBuilder.addField(FormField.builder("software_version").setValue("0.9.1").build());
SoftwareInfoForm softwareInfoForm = SoftwareInfoForm.getBuilder().setDataForm(dataFormBuilder.build()).build();
return softwareInfoForm;
}
Aggregations