All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.jsefa.common.annotation.ValidatorFactory Maven / Gradle / Ivy

Go to download

JSefa (Java Simple exchange format api) is a simple library for stream-based serialization of java objects to XML, CSV, FLR or any other format and back again using an iterator-style interface independent of the serialization format. The mapping between java object types and types of the serialization format (e. g. xml complex element types) can be defined either by annotating the java classes or programmatically using a simple API. The current implementation supports XML, CSV and FLR - for XML it is based on JSR 173.

The newest version!
/*
 * Copyright 2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.sf.jsefa.common.annotation;

import static net.sf.jsefa.common.annotation.AnnotationParameterNames.CONSTRAINTS;
import static net.sf.jsefa.common.annotation.AnnotationParameterNames.DEFAULT_VALIDATOR_TYPE;
import static net.sf.jsefa.common.annotation.AnnotationParameterNames.LIST_ITEM;
import static net.sf.jsefa.common.annotation.AnnotationParameterNames.OBJECT_TYPE;
import static net.sf.jsefa.common.annotation.AnnotationParameterNames.VALIDATOR_TYPE;
import static net.sf.jsefa.common.annotation.AnnotationParameterNames.VALUE_REQUIRED;
import static net.sf.jsefa.common.validator.ValidationErrorCodes.MISSING_VALUE;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.sf.jsefa.ObjectPathElement;
import net.sf.jsefa.common.accessor.ObjectAccessor;
import net.sf.jsefa.common.accessor.ObjectAccessorProvider;
import net.sf.jsefa.common.util.ReflectionUtil;
import net.sf.jsefa.common.validator.ValidationError;
import net.sf.jsefa.common.validator.ValidationResult;
import net.sf.jsefa.common.validator.Validator;
import net.sf.jsefa.common.validator.provider.ValidatorProvider;

/**
 * Factory for creating {@link Validator}s from annotated classes.
 * 
 * @author Norman Lahme-Huetig
 * 
 */
public final class ValidatorFactory {

    private final ObjectAccessorProvider objectAccessorProvider;

    private final ValidatorProvider validatorProvider;

    /**
     * Constructs a new ValidatorFactory.
     * 
     * @param validatorProvider the validator provider
     * @param objectAccessorProvider the object accessor provider
     */
    public ValidatorFactory(ValidatorProvider validatorProvider, ObjectAccessorProvider objectAccessorProvider) {
        this.validatorProvider = validatorProvider;
        this.objectAccessorProvider = objectAccessorProvider;
    }

    /**
     * Creates a contextual validator for the given object type.
     * 
     * @param objectType the object type to create a validator for
     * @param field the field to create a contextual validator for (may be null)
     * @param contextAnnotation the annotation providing parameters for constructing the validator
     * @param dataTypeAnnotationType the type of the data type annotation providing default parameters for
     *                constructing the validator
     * @return a validator
     */
    @SuppressWarnings("unchecked")
    public Validator createContextualValidator(Class objectType, Field field, Annotation contextAnnotation,
            Class dataTypeAnnotationType) {
        Class validatorType = null;
        Validator validator = null;
        String[] constraints = null;
        if (contextAnnotation != null) {
            validatorType = AnnotationDataProvider.get(contextAnnotation, VALIDATOR_TYPE);
            constraints = AnnotationDataProvider.get(contextAnnotation, CONSTRAINTS);
        }
        if (validatorType == null) {
            if (this.validatorProvider.hasValidatorFor(objectType)) {
                validator = this.validatorProvider.getForObjectType(objectType, constraints);
            } else {
                validatorType = AnnotationDataProvider.get(objectType, DEFAULT_VALIDATOR_TYPE,
                        dataTypeAnnotationType);
            }
        }
        if (validator == null && validatorType != null) {
            validator = this.validatorProvider.getForValidatorType(validatorType, objectType, constraints);
        }
        if (hasCollectionType(objectType) && contextAnnotation != null
                && AnnotationDataProvider.hasParameter(contextAnnotation.getClass(), LIST_ITEM)) {
            Annotation itemAnnotation = AnnotationDataProvider.get(contextAnnotation, LIST_ITEM);
            Class itemObjectType = AnnotationDataProvider.get(itemAnnotation, OBJECT_TYPE);
            if (itemObjectType == null) {
                itemObjectType = ReflectionUtil.getActualTypeParameter(field, 0);
            }
            Validator itemValidator = createContextualValidator(itemObjectType, field, itemAnnotation,
                    dataTypeAnnotationType);
            if (itemValidator != null) {
                Validator itemListValidator = new SimpleListTypeValidator(itemValidator);
                if (validator != null) {
                    validator = new AndValidator(validator, itemListValidator);
                } else {
                    validator = itemListValidator;
                }
            }
        }
        return validator;
    }

    /**
     * Creates a context free validator for the given object type.
     * 
     * @param objectType the object type to create a validator for
     * @param fieldAnnotationTypes the possible annotation types for the fields of the given object type providing
     *                parameters for constructing the validator
     * @return a validator
     */
    public Validator createValidator(Class objectType, Class... fieldAnnotationTypes) {
        List requiredFieldNames = new ArrayList();
        for (Field field : AnnotatedFieldsProvider.getAnnotatedFields(objectType, fieldAnnotationTypes)) {
            Boolean required = AnnotationDataProvider.get(field, VALUE_REQUIRED, fieldAnnotationTypes);
            if (required != null && required) {
                requiredFieldNames.add(field.getName());
            }
        }
        if (requiredFieldNames.isEmpty()) {
            return null;
        } else {
            return new RequiredFieldsValidator(this.objectAccessorProvider.get(objectType), requiredFieldNames);
        }
    }

    private boolean hasCollectionType(Class objectType) {
        return Collection.class.isAssignableFrom(objectType);
    }

    private static final class RequiredFieldsValidator implements Validator {

        private List requiredFieldNames;

        private ObjectAccessor objectAccessor;

        private RequiredFieldsValidator(ObjectAccessor objectAccessor, List requiredFieldNames) {
            this.objectAccessor = objectAccessor;
            this.requiredFieldNames = requiredFieldNames;
        }

        public ValidationResult validate(Object value) {
            List errors = new ArrayList();
            for (String fieldName : this.requiredFieldNames) {
                if (this.objectAccessor.getValue(value, fieldName) == null) {
                    String errorText = "The field " + fieldName + " is required but no value is present";
                    errors.add(ValidationError.create(MISSING_VALUE, errorText,
                            new ObjectPathElement(value.getClass(), fieldName)));
                }
            }
            return ValidationResult.create(errors);
        }

    }

    private static final class SimpleListTypeValidator implements Validator {
        private Validator itemValidator;

        SimpleListTypeValidator(Validator itemValidator) {
            this.itemValidator = itemValidator;
        }

        public ValidationResult validate(Object value) {
            List errors = new ArrayList();
            for (Object item : (List) value) {
                errors.addAll(this.itemValidator.validate(item).getErrors());
            }
            return ValidationResult.create(errors);
        }

    }

    private static final class AndValidator implements Validator {
        private Validator validatorA;

        private Validator validatorB;

        AndValidator(Validator validatorA, Validator validatorB) {
            this.validatorA = validatorA;
            this.validatorB = validatorB;
        }

        public ValidationResult validate(Object value) {
            List errors = new ArrayList();
            errors.addAll(validatorA.validate(value).getErrors());
            errors.addAll(validatorB.validate(value).getErrors());
            return ValidationResult.create(errors);
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy