Search in sources :

Example 1 with ParamField

use of org.apache.tika.config.ParamField in project tika by apache.

the class AnnotationUtils method assignFieldParams.

/**
     * Assigns the param values to bean
     * @throws TikaConfigException when an error occurs while assigning params
     */
public static void assignFieldParams(Object bean, Map<String, Param> params) throws TikaConfigException {
    Class<?> beanClass = bean.getClass();
    if (!PARAM_INFO.containsKey(beanClass)) {
        synchronized (TikaConfig.class) {
            if (!PARAM_INFO.containsKey(beanClass)) {
                List<AccessibleObject> aObjs = collectInfo(beanClass, org.apache.tika.config.Field.class);
                List<ParamField> fields = new ArrayList<>(aObjs.size());
                for (AccessibleObject aObj : aObjs) {
                    fields.add(new ParamField(aObj));
                }
                PARAM_INFO.put(beanClass, fields);
            }
        }
    }
    List<ParamField> fields = PARAM_INFO.get(beanClass);
    Set<String> validFieldNames = new HashSet<>();
    for (ParamField field : fields) {
        validFieldNames.add(field.getName());
        Param<?> param = params.get(field.getName());
        if (param != null) {
            if (field.getType().isAssignableFrom(param.getType())) {
                try {
                    field.assignValue(bean, param.getValue());
                } catch (Exception e) {
                    throw new TikaConfigException(e.getMessage(), e);
                }
            } else {
                String msg = String.format(Locale.ROOT, "Value '%s' of type '%s' cant be" + " assigned to field '%s' of defined type '%s'", param.getValue(), param.getValue().getClass(), field.getName(), field.getType());
                throw new TikaConfigException(msg);
            }
        } else if (field.isRequired()) {
            //param not supplied but field is declared as required?
            String msg = String.format(Locale.ROOT, "Param %s is required for %s," + " but it is not given in config.", field.getName(), bean.getClass().getName());
            throw new TikaConfigException(msg);
        } else {
        //FIXME: SLF4j is not showing up for import, fix it and send this to LOG.debug
        //LOG.debug("Param not supplied, field is not mandatory");
        }
    }
}
Also used : ParamField(org.apache.tika.config.ParamField) TikaConfig(org.apache.tika.config.TikaConfig) ArrayList(java.util.ArrayList) TikaConfigException(org.apache.tika.exception.TikaConfigException) TikaConfigException(org.apache.tika.exception.TikaConfigException) AccessibleObject(java.lang.reflect.AccessibleObject) HashSet(java.util.HashSet)

Aggregations

AccessibleObject (java.lang.reflect.AccessibleObject)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ParamField (org.apache.tika.config.ParamField)1 TikaConfig (org.apache.tika.config.TikaConfig)1 TikaConfigException (org.apache.tika.exception.TikaConfigException)1