Java Code Examples for org.jivesoftware.smackx.xdata.FormField#getType()

The following examples show how to use org.jivesoftware.smackx.xdata.FormField#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FormFieldRegistry.java    From Smack with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("ReferenceEquality")
public static synchronized 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;
        }

        String fieldName = formField.getFieldName();
        FormField.Type type = formField.getType();
        register(formType, fieldName, type);
    }
}
 
Example 2
Source File: FillableForm.java    From Smack with Apache License 2.0 5 votes vote down vote up
public void setAnswer(String fieldName, Collection<? extends CharSequence> answers) {
    FormField blankField = getFieldOrThrow(fieldName);
    FormField.Type type = blankField.getType();

    FormField filledFormField;
    switch (type) {
    case list_multi:
    case text_multi:
        filledFormField = createMultiKindFieldbuilder(fieldName, type)
            .addValues(answers)
            .build();
        break;
    case jid_multi:
        List<Jid> jids = new ArrayList<>(answers.size());
        List<XmppStringprepException> exceptions = new ArrayList<>();
        JidUtil.jidsFrom(answers, jids, exceptions);
        if (!exceptions.isEmpty()) {
            // TODO: Report all exceptions here.
            throw new IllegalArgumentException(exceptions.get(0));
        }
        filledFormField = FormField.jidMultiBuilder(fieldName)
                        .addValues(jids)
                        .build();
        break;
    default:
        throw new IllegalArgumentException("");
    }
    write(filledFormField);
}
 
Example 3
Source File: FillableForm.java    From Smack with Apache License 2.0 5 votes vote down vote up
public void setAnswer(String fieldName, boolean answer) {
    FormField blankField = getFieldOrThrow(fieldName);
    if (blankField.getType() != Type.bool) {
        throw new IllegalArgumentException();
    }

    FormField filledFormField = FormField.booleanBuilder(fieldName)
                    .setValue(answer)
                    .build();
    write(filledFormField);
}
 
Example 4
Source File: FillableForm.java    From Smack with Apache License 2.0 5 votes vote down vote up
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();
    }
    if (filledFields.containsKey(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);
    }
}
 
Example 5
Source File: FillableForm.java    From Smack with Apache License 2.0 4 votes vote down vote up
public void setAnswer(String fieldName, CharSequence answer) {
    FormField blankField = getFieldOrThrow(fieldName);
    FormField.Type type = blankField.getType();

    FormField filledFormField;
    switch (type) {
    case list_multi:
    case jid_multi:
        throw new IllegalArgumentException("Can not answer fields of type '" + type + "' with a CharSequence");
    case fixed:
        throw new IllegalArgumentException("Fields of type 'fixed' are not answerable");
    case list_single:
    case text_private:
    case text_single:
    case hidden:
        filledFormField = createSingleKindFieldBuilder(fieldName, type)
            .setValue(answer)
            .build();
        break;
    case bool:
        filledFormField = FormField.booleanBuilder(fieldName)
            .setValue(answer)
            .build();
        break;
    case jid_single:
        Jid jid;
        try {
            jid = JidCreate.from(answer);
        } catch (XmppStringprepException e) {
            throw new IllegalArgumentException(e);
        }
        filledFormField = FormField.jidSingleBuilder(fieldName)
            .setValue(jid)
            .build();
        break;
    case text_multi:
        filledFormField = createMultiKindFieldbuilder(fieldName, type)
            .addValue(answer)
            .build();
        break;
    default:
        throw new AssertionError();
    }
    write(filledFormField);
}