Android Open Source - baracus-framework Validation Factory






From Project

Back to project page baracus-framework.

License

The source code is released under:

Apache License

If you think the Android project baracus-framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.mantucon.baracus.validation;
/*from  w w  w  .  ja va  2 s.  c  o  m*/
import android.view.View;
import android.view.ViewGroup;
import net.mantucon.baracus.annotations.Bean;
import net.mantucon.baracus.context.BaracusApplicationContext;
import net.mantucon.baracus.errorhandling.ErrorHandlingFactory;
import net.mantucon.baracus.errorhandling.ErrorSeverity;
import net.mantucon.baracus.lifecycle.Destroyable;
import net.mantucon.baracus.lifecycle.Initializeable;
import net.mantucon.baracus.util.Logger;
import net.mantucon.baracus.validation.builtins.*;

import java.util.HashMap;
import java.util.Map;

import static net.mantucon.baracus.util.StringUtil.*;

/**
 * User: marcus
 * Date: 24.09.13
 * <p/>
 * Validation factory used to register validators. You can use the ApplicationContext
 * to call this methods statically (delegated thru BeanContainer).
 */
@Bean
public class ValidationFactory implements Initializeable, Destroyable {

    private static final Map<String, Validator<?>> namedValidators = new HashMap<String, Validator<?>>();
    private static final Logger logger = new Logger(ValidationFactory.class);

    @Bean
    ErrorHandlingFactory errorHandlingFactory;

    /**
     * Exception thrown, if an invalid validator name is used within the code
     */
    public static final class InvalidValidatorNameException extends RuntimeException {
        private final String validatorName;

        public InvalidValidatorNameException(String validatorName) {
            super();
            this.validatorName = validatorName;
        }

        @Override
        public String getMessage() {
            return "A VALIDATOR NAMED " + validatorName + " COULD NOT BE FOUND! AVAILABLE VALIDATORS :" + join(namedValidators.keySet());
        }
    }


    /**
     * register a named validator
     *
     * @param name      - name of the validator to be used in XML
     * @param validator - the validator instance
     */
    public synchronized void registerValidator(String name, Validator<?> validator) {
        namedValidators.put(name, validator);
    }

    /**
     * register a named validator, the name will be derived from the class name
     *
     * @param validator - the validator instance
     */
    public synchronized void registerValidator(Validator<?> validator) {
        String name = firstByteToLower(validator.getClass().getSimpleName());
        namedValidators.put(name, validator);
    }

    /**
     * verify that all validators in the list are bound to an existing validator
     *
     * @param validatorList - a comma seperated string containing all validators
     */
    public void verifyValidators(String validatorList) {
        if (validatorList != null && validatorList.trim().length() > 0) {
            String[] validators = validatorList.split(",");
            for (String v : validators) {
                if (!namedValidators.containsKey(v)) {
                    logger.error("A named validator $1 could not be found. Available validators : $2", v, join(namedValidators.keySet()));
                    throw new InvalidValidatorNameException(v);
                }
            }
        }
    }

    /**
     * Verify an entire View. This function will recurse all child views and look for
     * View Components implementing the ConstrainedView interface.
     * <p/>
     * If a component is found, it will be validated by all validators denoted through the
     * validatedBy attribute. If a violation occurs, it is matter
     *
     * @param v - the view to verify
     */
    protected void validateView(View root, View v) {
        if (ConstrainedView.class.isAssignableFrom(v.getClass())) {
            ConstrainedView cv = (ConstrainedView) v;
            String[] validators = splitPurified(cv.getValidators(), ",");
            if (validators != null && validators.length > 0) {
                boolean stop = false;
                int i = 0;
                while (!stop) {
                    String validatorName = validators[i++];
                    Validator validator = namedValidators.get(validatorName);
                    if (validator != null && !validator.validate(cv)) {
                        errorHandlingFactory.addErrorToView(root, v.getId(), validator.getMessageId(), ErrorSeverity.ERROR, validator.viewToMessageParams(v));
                        stop = true;
                    }
                    stop |= (i >= validators.length);
                }
            }
        }

        if (ViewGroup.class.isAssignableFrom(v.getClass())) {
            ViewGroup viewGroup = (ViewGroup) v;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View nextChild = viewGroup.getChildAt(i);
                if (nextChild != null) {
                    validateView(root, nextChild);
                }
            }
        }
    }


    /**
     * Entry function for view validation
     *
     * @param v
     */
    public void validateView(View v) {
        validateView(v, v);
    }

    /**
     * registers the built in validators
     */
    @Override
    public void postConstruct() {
        registerValidator(new StringNotEmpty());
        registerValidator(new StringIsNumericDouble());
        registerValidator(new StringIsNumericInteger());
        registerValidator(new NumberMustBeGreaterThanZero());
        registerValidator(new DateFromNow());
    }

    @Override
    public void onDestroy() {
        namedValidators.clear();
    }


    /**
     * walkt the view tree in order to find components to register
     *
     * @param root
     * @param v
     */
    protected void registerValidationListener(final View root, final View v) {
        if (ConstrainedView.class.isAssignableFrom(v.getClass())) {
            v.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    BaracusApplicationContext.validateView(root);
                    if (ValidatableView.class.isAssignableFrom(root.getClass())) {
                        ValidatableView validatableView = (ValidatableView) root;
                        validatableView.onValidation();
                    }
                }
            });
        }

        if (ViewGroup.class.isAssignableFrom(v.getClass())) {
            ViewGroup viewGroup = (ViewGroup) v;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View nextChild = viewGroup.getChildAt(i);
                if (nextChild != null) {
                    registerValidationListener(root, nextChild);
                }
            }
        }
    }

    /**
     * registers an onFocusChangeListener to all view elements implementing
     * the @see ConstrainedView interface to perform on-the-fly-validation.
     * If you want Your View to be able to receive a validation callback
     * - e.g. in order to manage the visibility of an OK-Button or sth. -
     * Your View must implement the @see ValidatableView interface in
     * order to receive a validation notification callbacks
     *
     * @param root - the view to perform validation on
     */
    public void registerValidationListener(final View root) {
        registerValidationListener(root, root);
    }

}




Java Source Code List

net.mantucon.baracus.annotations.Bean.java
net.mantucon.baracus.context.AnnotationScanner.java
net.mantucon.baracus.context.BaracusApplicationContext.java
net.mantucon.baracus.context.BeanContainer.java
net.mantucon.baracus.context.Exceptions.java
net.mantucon.baracus.context.ManagedActivity.java
net.mantucon.baracus.context.ManagedFragment.java
net.mantucon.baracus.dao.BaracusOpenHelper.java
net.mantucon.baracus.dao.BaseDao.java
net.mantucon.baracus.dao.ConfigurationDao.java
net.mantucon.baracus.errorhandling.CustomErrorHandler.java
net.mantucon.baracus.errorhandling.ErrorHandler.java
net.mantucon.baracus.errorhandling.ErrorHandlingFactory.java
net.mantucon.baracus.errorhandling.ErrorSeverity.java
net.mantucon.baracus.errorhandling.StandardErrorHandler.java
net.mantucon.baracus.errorhandling.TextEditErrorHandler.java
net.mantucon.baracus.lifecycle.ApplicationContextInitializer.java
net.mantucon.baracus.lifecycle.Destroyable.java
net.mantucon.baracus.lifecycle.Initializeable.java
net.mantucon.baracus.migr8.MigrationStep.java
net.mantucon.baracus.migr8.ModelVersion000.java
net.mantucon.baracus.model.ConfigurationParameter.java
net.mantucon.baracus.orm.AbstractModelBase.java
net.mantucon.baracus.orm.FieldList.java
net.mantucon.baracus.orm.Field.java
net.mantucon.baracus.orm.Identifiable.java
net.mantucon.baracus.orm.LazyCollection.java
net.mantucon.baracus.orm.LazyMorphicCollection.java
net.mantucon.baracus.orm.LazyReference.java
net.mantucon.baracus.orm.LegacyModelBase.java
net.mantucon.baracus.orm.ModelBase.java
net.mantucon.baracus.orm.NullReference.java
net.mantucon.baracus.orm.ObjectReference.java
net.mantucon.baracus.orm.OptimisticLockingModelBase.java
net.mantucon.baracus.orm.OptmisticLocking.java
net.mantucon.baracus.orm.ReferenceLoader.java
net.mantucon.baracus.orm.Reference.java
net.mantucon.baracus.orm.Timestamped.java
net.mantucon.baracus.signalling.ConfigurationChangeListener.java
net.mantucon.baracus.signalling.DataChangeAwareComponent.java
net.mantucon.baracus.signalling.DataSetChangeAwareComponent.java
net.mantucon.baracus.signalling.DeleteAwareComponent.java
net.mantucon.baracus.signalling.GenericEventAwareComponent.java
net.mantucon.baracus.signalling.GenericEvent.java
net.mantucon.baracus.ui.ConstrainedEditText.java
net.mantucon.baracus.ui.ConstrainedSpinner.java
net.mantucon.baracus.ui.ErrorView.java
net.mantucon.baracus.ui.Popup.java
net.mantucon.baracus.util.DBBackup.java
net.mantucon.baracus.util.DataUtil.java
net.mantucon.baracus.util.DateUtil.java
net.mantucon.baracus.util.DayDate.java
net.mantucon.baracus.util.Logger.java
net.mantucon.baracus.util.StringUtil.java
net.mantucon.baracus.validation.AbstractValidator.java
net.mantucon.baracus.validation.ConstrainedView.java
net.mantucon.baracus.validation.ValidatableView.java
net.mantucon.baracus.validation.ValidationFactory.java
net.mantucon.baracus.validation.Validator.java
net.mantucon.baracus.validation.builtins.DateFromNow.java
net.mantucon.baracus.validation.builtins.NumberMustBeGreaterThanZero.java
net.mantucon.baracus.validation.builtins.StringIsNumericDouble.java
net.mantucon.baracus.validation.builtins.StringIsNumericInteger.java
net.mantucon.baracus.validation.builtins.StringNotEmpty.java