Java Code Examples for org.jivesoftware.smackx.xdata.FormField#Type

The following examples show how to use org.jivesoftware.smackx.xdata.FormField#Type . 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: FormFieldRegistry.java    From Smack with Apache License 2.0 5 votes vote down vote up
public static synchronized void register(String formType, String fieldName, FormField.Type type) {
    if (formType == null) {
        FormFieldInformation formFieldInformation = lookup(fieldName);
        if (formFieldInformation != null) {
            if (Objects.equals(formType, formFieldInformation.formType)
                            && type.equals(formFieldInformation.formFieldType)) {
                // The field is already registered, nothing to do here.
                return;
            }

            String message = "There is already a field with the name'" + fieldName
                            + "' registered with the field type '" + formFieldInformation.formFieldType
                            + "', while this tries to register the field with the type '" + type + '\'';
            throw new IllegalArgumentException(message);
        }

        LOOKASIDE_REGISTRY.put(fieldName, type);
        return;
    }

    Map<String, FormField.Type> fieldNameToType = REGISTRY.get(formType);
    if (fieldNameToType == null) {
        fieldNameToType = new HashMap<>();
        REGISTRY.put(formType, fieldNameToType);
    } else {
        FormField.Type previousType = fieldNameToType.get(fieldName);
        if (previousType != null && previousType != type) {
            throw new IllegalArgumentException();
        }
    }
    fieldNameToType.put(fieldName, type);

    FIELD_NAME_TO_FORM_TYPE.put(fieldName, formType);
}
 
Example 3
Source File: FormFieldRegistry.java    From Smack with Apache License 2.0 5 votes vote down vote up
public static synchronized void register(String fieldName, FormField.Type type) {
    FormField.Type previousType = LOOKASIDE_REGISTRY.get(fieldName);
    if (previousType != null) {
        if (previousType == type) {
            // Nothing to do here.
            return;
        }
        throw new IllegalArgumentException("There is already a field with the name '" + fieldName
                        + "' registered with type " + previousType
                        + ", while trying to register this field with type '" + type + "'");
    }

    LOOKASIDE_REGISTRY.put(fieldName, type);
}
 
Example 4
Source File: FormFieldRegistry.java    From Smack with Apache License 2.0 5 votes vote down vote up
public static synchronized FormFieldInformation lookup(String fieldName) {
    String formType = FIELD_NAME_TO_FORM_TYPE.get(fieldName);
    FormField.Type type = lookup(formType, fieldName);
    if (type == null) {
        return null;
    }

    return new FormFieldInformation(type, formType);
}
 
Example 5
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 6
Source File: FillableForm.java    From Smack with Apache License 2.0 5 votes vote down vote up
private static AbstractMultiFormField.Builder<?, ?> createMultiKindFieldbuilder(String fieldName, FormField.Type type) {
    switch (type) {
    case list_multi:
        return FormField.listMultiBuilder(fieldName);
    case text_multi:
        return FormField.textMultiBuilder(fieldName);
    default:
        throw new IllegalArgumentException();
    }
}
 
Example 7
Source File: FillableForm.java    From Smack with Apache License 2.0 5 votes vote down vote up
private static AbstractSingleStringValueFormField.Builder<?, ?> createSingleKindFieldBuilder(String fieldName, FormField.Type type) {
    switch (type) {
    case text_private:
        return FormField.textPrivateBuilder(fieldName);
    case text_single:
        return FormField.textSingleBuilder(fieldName);
    case hidden:
        return FormField.hiddenBuilder(fieldName);
    case list_multi:
        return FormField.listSingleBuilder(fieldName);
    default:
        throw new IllegalArgumentException();
    }
}
 
Example 8
Source File: FormFieldRegistry.java    From Smack with Apache License 2.0 4 votes vote down vote up
private FormFieldInformation(FormField.Type formFieldType, String formType) {
    this.formFieldType = formFieldType;
    this.formType = formType;
}
 
Example 9
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);
}
 
Example 10
Source File: DataFormProvider.java    From Smack with Apache License 2.0 4 votes vote down vote up
private static void ensureAtMostSingleValue(FormField.Type type, List<FormField.Value> values) throws SmackParsingException {
    if (values.size() > 1) {
        throw new SmackParsingException(type + " fields can have at most one value, this one had " + values.size());
    }
}
 
Example 11
Source File: ReportedData.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new column with the specified definition.
 *
 * @param label the columns's label.
 * @param variable the variable name of the column.
 * @param type the format for the returned data.
 */
public Column(String label, String variable, FormField.Type type) {
    this.label = label;
    this.variable = variable;
    this.type = type;
}
 
Example 12
Source File: ReportedData.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the column's data format.
 *
 * @return format for the returned data.
 */
public FormField.Type getType() {
    return type;
}