Example usage for org.apache.commons.validator Validator validate

List of usage examples for org.apache.commons.validator Validator validate

Introduction

In this page you can find the example usage for org.apache.commons.validator Validator validate.

Prototype

public ValidatorResults validate() throws IOException 

Source Link

Document

Performs validations based on the configured resources.

Usage

From source file:org.openmobster.core.common.validation.ObjectValidator.java

/**
 * Validates the value of the specified Field on the target Object
 * /*from w w  w .j a va  2 s. c o  m*/
 * @param object
 * @param fieldName
 * @return a Set of Validation Error Keys
 * @throws ValidationException
 */
public Set<String> validate(Object object, String fieldName) throws ValidationException {
    try {
        Set<String> errorKeys = new HashSet<String>();
        String objectId = object.getClass().getName();

        //Setup the Validator
        Validator validator = new Validator(this.validatorResources, objectId);
        validator.setParameter(Validator.BEAN_PARAM, object);
        validator.setFieldName(fieldName);

        ValidatorResults results = validator.validate();

        Form form = this.validatorResources.getForm(Locale.getDefault(), objectId);
        Iterator propertyNames = results.getPropertyNames().iterator();
        while (propertyNames.hasNext()) {
            String property = (String) propertyNames.next();
            ValidatorResult result = results.getValidatorResult(property);
            Map actionMap = result.getActionMap();
            Iterator keys = actionMap.keySet().iterator();
            while (keys.hasNext()) {
                String actionName = (String) keys.next();
                if (!result.isValid(actionName)) {
                    Field field = form.getField(property);
                    Arg[] args = field.getArgs(actionName);
                    if (args != null) {
                        for (int i = 0; i < args.length; i++) {
                            errorKeys.add(args[i].getKey());
                        }
                    }
                }
            }
        }

        return errorKeys;
    } catch (Exception e) {
        log.error(this, e);
        throw new ValidationException(e);
    }
}

From source file:org.openmobster.core.common.validation.ObjectValidator.java

/**
 * Fully validates all the Fields of the specified Object
 * // w w w . ja va 2  s .  c  o m
 * @param object
 * @return a Map of Fields to their corresponding Validation Error Keys
 * 
 * @throws ValidationException
 */
public Map<String, String[]> validate(Object object) throws ValidationException {
    try {
        Map<String, String[]> errorKeys = new HashMap<String, String[]>();
        String objectId = object.getClass().getName();

        //Setup the Validator
        Validator validator = new Validator(this.validatorResources, objectId);
        validator.setParameter(Validator.BEAN_PARAM, object);

        ValidatorResults results = validator.validate();

        Form form = this.validatorResources.getForm(Locale.getDefault(), objectId);
        Iterator propertyNames = results.getPropertyNames().iterator();
        while (propertyNames.hasNext()) {
            Set<String> cour = new HashSet<String>();
            String property = (String) propertyNames.next();
            ValidatorResult result = results.getValidatorResult(property);
            Map actionMap = result.getActionMap();
            Iterator keys = actionMap.keySet().iterator();
            boolean errorFound = false;
            while (keys.hasNext()) {
                String actionName = (String) keys.next();
                if (!result.isValid(actionName)) {
                    Field field = form.getField(property);
                    Arg[] args = field.getArgs(actionName);
                    if (args != null) {
                        for (int i = 0; i < args.length; i++) {
                            cour.add(args[i].getKey());
                            errorFound = true;
                        }
                    }
                }
            }
            if (errorFound) {
                errorKeys.put(property, cour.toArray(new String[cour.size()]));
            }
        }

        return errorKeys;
    } catch (Exception e) {
        log.error(this, e);
        throw new ValidationException(e);
    }
}

From source file:org.seasar.struts.action.ActionWrapper.java

/**
 * ?????/*from   w  w w  .j a  v  a2 s . c  om*/
 * 
 * @param request
 *            
 * @param executeConfig
 *            
 * @return 
 */
protected ActionMessages validateUsingValidator(HttpServletRequest request, S2ExecuteConfig executeConfig) {
    ServletContext application = ServletContextUtil.getServletContext();
    ActionMessages errors = new ActionMessages();
    String validationKey = actionMapping.getName() + "_" + executeConfig.getMethod().getName();
    Validator validator = Resources.initValidator(validationKey,
            ActionFormUtil.getActionForm(request, actionMapping), application, request, errors, 0);
    try {
        validator.validate();
    } catch (ValidatorException e) {
        throw new RuntimeException(e);
    }
    return errors;
}