Search in sources :

Example 1 with FormFieldChildElement

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

the class FillableForm method write.

public final void write(FormField filledFormField) {
    if (filledFormField.getType() == FormField.Type.fixed) {
        throw new IllegalArgumentException();
    }
    if (!filledFormField.hasValueSet()) {
        throw new IllegalArgumentException();
    }
    String fieldName = filledFormField.getFieldName();
    if (!getDataForm().hasField(fieldName)) {
        throw new IllegalArgumentException();
    }
    // Perform validation, e.g. using XEP-0122.
    // TODO: We could also perform list-* option validation, but this has to take xep122's <open/> into account.
    FormField formFieldPrototype = getDataForm().getField(fieldName);
    for (FormFieldChildElement formFieldChildelement : formFieldPrototype.getFormFieldChildElements()) {
        formFieldChildelement.validate(filledFormField);
    }
    filledFields.put(fieldName, filledFormField);
    if (requiredFields.contains(fieldName)) {
        filledRequiredFields.add(fieldName);
        missingRequiredFields.remove(fieldName);
    }
}
Also used : AbstractSingleStringValueFormField(org.jivesoftware.smackx.xdata.AbstractSingleStringValueFormField) AbstractMultiFormField(org.jivesoftware.smackx.xdata.AbstractMultiFormField) FormField(org.jivesoftware.smackx.xdata.FormField) FormFieldChildElement(org.jivesoftware.smackx.xdata.FormFieldChildElement)

Example 2 with FormFieldChildElement

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

the class DataFormProvider method parseField.

private static FormField parseField(XmlPullParser parser, XmlEnvironment xmlEnvironment, String formType, DataForm.ReportedData reportedData) throws XmlPullParserException, IOException, SmackParsingException {
    final int initialDepth = parser.getDepth();
    final String fieldName = parser.getAttributeValue("var");
    final String label = parser.getAttributeValue("", "label");
    FormField.Type type = null;
    {
        String fieldTypeString = parser.getAttributeValue("type");
        if (fieldTypeString != null) {
            type = FormField.Type.fromString(fieldTypeString);
        }
    }
    List<FormField.Value> values = new ArrayList<>();
    List<FormField.Option> options = new ArrayList<>();
    List<FormFieldChildElement> childElements = new ArrayList<>();
    boolean required = false;
    outerloop: while (true) {
        XmlPullParser.TagEvent eventType = parser.nextTag();
        switch(eventType) {
            case START_ELEMENT:
                QName qname = parser.getQName();
                if (qname.equals(FormField.Value.QNAME)) {
                    FormField.Value value = parseValue(parser);
                    values.add(value);
                } else if (qname.equals(FormField.Option.QNAME)) {
                    FormField.Option option = parseOption(parser);
                    options.add(option);
                } else if (qname.equals(FormField.Required.QNAME)) {
                    required = true;
                } else {
                    FormFieldChildElementProvider<?> provider = FormFieldChildElementProviderManager.getFormFieldChildElementProvider(qname);
                    if (provider == null) {
                        LOGGER.warning("Unknown form field child element " + qname + " ignored");
                        continue;
                    }
                    FormFieldChildElement formFieldChildElement = provider.parse(parser, XmlEnvironment.from(parser, xmlEnvironment));
                    childElements.add(formFieldChildElement);
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
        }
    }
    // XEP-0004 ยง 3.4 and SMACK-902
    if (type == null && reportedData != null) {
        FormField reportedFormField = reportedData.getField(fieldName);
        if (reportedFormField != null) {
            type = reportedFormField.getType();
        }
    }
    if (type == null) {
        // The field name 'FORM_TYPE' is magic.
        if (fieldName.equals(FormField.FORM_TYPE)) {
            type = FormField.Type.hidden;
        } else {
            // If no field type was explicitly provided, then we need to lookup the
            // field's type in the registry.
            type = FormFieldRegistry.lookup(formType, fieldName);
            if (type == null) {
                LOGGER.warning("The Field '" + fieldName + "' from FORM_TYPE '" + formType + "' is not registered. Field type is unknown, assuming text-single.");
                // As per XEP-0004, text-single is the default form field type, which we use as emergency fallback here.
                type = FormField.Type.text_single;
            }
        }
    }
    FormField.Builder<?, ?> builder;
    switch(type) {
        case bool:
            builder = parseBooleanFormField(fieldName, values);
            break;
        case fixed:
            builder = parseSingleKindFormField(FormField.fixedBuilder(fieldName), values);
            break;
        case hidden:
            builder = parseSingleKindFormField(FormField.hiddenBuilder(fieldName), values);
            break;
        case jid_multi:
            JidMultiFormField.Builder jidMultiBuilder = FormField.jidMultiBuilder(fieldName);
            for (FormField.Value value : values) {
                jidMultiBuilder.addValue(value);
            }
            builder = jidMultiBuilder;
            break;
        case jid_single:
            ensureAtMostSingleValue(type, values);
            JidSingleFormField.Builder jidSingleBuilder = FormField.jidSingleBuilder(fieldName);
            if (!values.isEmpty()) {
                FormField.Value value = values.get(0);
                jidSingleBuilder.setValue(value);
            }
            builder = jidSingleBuilder;
            break;
        case list_multi:
            ListMultiFormField.Builder listMultiBuilder = FormField.listMultiBuilder(fieldName);
            addOptionsToBuilder(options, listMultiBuilder);
            builder = parseMultiKindFormField(listMultiBuilder, values);
            break;
        case list_single:
            ListSingleFormField.Builder listSingleBuilder = FormField.listSingleBuilder(fieldName);
            addOptionsToBuilder(options, listSingleBuilder);
            builder = parseSingleKindFormField(listSingleBuilder, values);
            break;
        case text_multi:
            builder = parseMultiKindFormField(FormField.textMultiBuilder(fieldName), values);
            break;
        case text_private:
            builder = parseSingleKindFormField(FormField.textPrivateBuilder(fieldName), values);
            break;
        case text_single:
            builder = parseSingleKindFormField(FormField.textSingleBuilder(fieldName), values);
            break;
        default:
            // Should never happen, as we cover all types in the switch/case.
            throw new AssertionError("Unknown type " + type);
    }
    switch(type) {
        case list_multi:
        case list_single:
            break;
        default:
            if (!options.isEmpty()) {
                throw new SmackParsingException("Form fields of type " + type + " must not have options. This one had " + options.size());
            }
            break;
    }
    if (label != null) {
        builder.setLabel(label);
    }
    builder.setRequired(required);
    builder.addFormFieldChildElements(childElements);
    return builder.build();
}
Also used : ListSingleFormField(org.jivesoftware.smackx.xdata.ListSingleFormField) ArrayList(java.util.ArrayList) ListMultiFormField(org.jivesoftware.smackx.xdata.ListMultiFormField) JidSingleFormField(org.jivesoftware.smackx.xdata.JidSingleFormField) ListMultiFormField(org.jivesoftware.smackx.xdata.ListMultiFormField) AbstractSingleStringValueFormField(org.jivesoftware.smackx.xdata.AbstractSingleStringValueFormField) BooleanFormField(org.jivesoftware.smackx.xdata.BooleanFormField) FormField(org.jivesoftware.smackx.xdata.FormField) ListSingleFormField(org.jivesoftware.smackx.xdata.ListSingleFormField) JidMultiFormField(org.jivesoftware.smackx.xdata.JidMultiFormField) TextSingleFormField(org.jivesoftware.smackx.xdata.TextSingleFormField) AbstractMultiFormField(org.jivesoftware.smackx.xdata.AbstractMultiFormField) JidSingleFormField(org.jivesoftware.smackx.xdata.JidSingleFormField) FormFieldChildElement(org.jivesoftware.smackx.xdata.FormFieldChildElement) QName(javax.xml.namespace.QName) SmackParsingException(org.jivesoftware.smack.parsing.SmackParsingException) JidMultiFormField(org.jivesoftware.smackx.xdata.JidMultiFormField)

Example 3 with FormFieldChildElement

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

the class SoftwareInfoForm method getIcon.

/**
 * Returns the software icon if used by client.
 * <br>
 * @return {@link MediaElement} MediaElement or null
 */
public MediaElement getIcon() {
    FormField field = getField(ICON);
    if (field == null) {
        return null;
    }
    FormFieldChildElement media = field.getFormFieldChildElement(MediaElement.QNAME);
    if (media == null) {
        return null;
    }
    return (MediaElement) media;
}
Also used : MediaElement(org.jivesoftware.smackx.mediaelement.element.MediaElement) FormField(org.jivesoftware.smackx.xdata.FormField) TextSingleFormField(org.jivesoftware.smackx.xdata.TextSingleFormField) FormFieldChildElement(org.jivesoftware.smackx.xdata.FormFieldChildElement)

Aggregations

FormField (org.jivesoftware.smackx.xdata.FormField)3 FormFieldChildElement (org.jivesoftware.smackx.xdata.FormFieldChildElement)3 AbstractMultiFormField (org.jivesoftware.smackx.xdata.AbstractMultiFormField)2 AbstractSingleStringValueFormField (org.jivesoftware.smackx.xdata.AbstractSingleStringValueFormField)2 TextSingleFormField (org.jivesoftware.smackx.xdata.TextSingleFormField)2 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 SmackParsingException (org.jivesoftware.smack.parsing.SmackParsingException)1 MediaElement (org.jivesoftware.smackx.mediaelement.element.MediaElement)1 BooleanFormField (org.jivesoftware.smackx.xdata.BooleanFormField)1 JidMultiFormField (org.jivesoftware.smackx.xdata.JidMultiFormField)1 JidSingleFormField (org.jivesoftware.smackx.xdata.JidSingleFormField)1 ListMultiFormField (org.jivesoftware.smackx.xdata.ListMultiFormField)1 ListSingleFormField (org.jivesoftware.smackx.xdata.ListSingleFormField)1