Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package javax.faces.validator;
 
 import java.util.*;

A Validator that delegates validation of the bean property to the Bean Validation API.

Since:
2.0
 
 public class BeanValidator implements ValidatorPartialStateHolder {
     
     private static final Logger LOGGER =
          Logger.getLogger("javax.faces.validator""javax.faces.LogStrings");
 
     private String validationGroups;
 
     private transient Class[] cachedValidationGroups;

    

The standard validator id for this validator, as defined by the JSF specification.

 
     public static final String VALIDATOR_ID = "javax.faces.Bean";

The message identifier of the javax.faces.application.FacesMessage to be created if a constraint failure is found. The message format string for this message may optionally include the following placeholders:

  • {0} replaced by the interpolated message from Bean Validation.
  • {1} replaced by a String whose value is the label of the input component that produced this message.

The message format string provided by the default implementation should be a the placeholder {0}, thus fully delegating the message handling to Bean Validation. A developer can override this message format string to make it conform to other JSF validator messages (i.e., by including the component label)

 
     public static final String MESSAGE_ID = "javax.faces.validator.BeanValidator.MESSAGE";

    

The name of the servlet context attribute which holds the object used by JSF to obtain Validator instances. If the servlet context attribute is missing or contains a null value, JSF is free to use this servlet context attribute to store the ValidatorFactory bootstrapped by this validator.

    public static final String VALIDATOR_FACTORY_KEY = "javax.faces.validator.beanValidator.ValidatorFactory";

    

The delimiter that is used to separate the list of fully-qualified group names as strings.

    public static final String VALIDATION_GROUPS_DELIMITER = ",";

    

The regular expression pattern that identifies an empty list of validation groups.

    public static final String EMPTY_VALIDATION_GROUPS_PATTERN = "^[\\W" +  + "]*$";
    
    

If this param is defined, and calling toLowerCase().equals(“true”) on a String representation of its value returns true, the runtime must not automatically add the validator with validator-id equal to the value of the symbolic constant VALIDATOR_ID to the list of default validators. Setting this parameter to true will have the effect of disabling the automatic installation of Bean Validation to every input component in every view in the application, though manual installation is still possible.

    public static final String DISABLE_DEFAULT_BEAN_VALIDATOR_PARAM_NAME =
            "javax.faces.validator.DISABLE_DEFAULT_BEAN_VALIDATOR";

    

A comma-separated list of validation groups which are used to filter which validations get checked by this validator. If the validationGroupsArray attribute is omitted or is empty, the validation groups will be inherited from the branch defaults or, if there are no branch defaults, the javax.validation.groups.Default group will be used.

Parameters:
validationGroups comma-separated list of validation groups (string with only spaces and commas treated as null)
    public void setValidationGroups(String validationGroups) {
        clearInitialState();        
        String newValidationGroups = validationGroups;
        
        // treat empty list as null
        if (newValidationGroups != null && newValidationGroups.matches()) {
            newValidationGroups = null;
        }
        
        // only clear cache of validation group classes if value is changing
        if (newValidationGroups == null && validationGroups != null) {
             = null;
        }
        
        if (newValidationGroups != null && validationGroups != null && !newValidationGroups.equals(validationGroups)) {
             = null;
        }
        
        if (newValidationGroups != null && validationGroups == null) {
             = null;
        }
        
        this. = newValidationGroups;
    }

    

Return the validation groups passed to the Validation API when checking constraints. If the validationGroupsArray attribute is omitted or empty, the validation groups will be inherited from the branch defaults, or if there are no branch defaults, the javax.validation.groups.Default group will be used.

    public String getValidationGroups() {
        return ;
    }

    

Verify that the value is valid according to the Bean Validation constraints.

Obtain a javax.validation.ValidatorFactory instance by calling javax.validation.Validation.buildDefaultValidatorFactory().

Let validationGroupsArray be a Class [] representing validator groups set on the component by the tag handler for this validator. The first search component terminates the search for the validation groups value. If no such value is found use the class name of javax.validation.groups.Default as the value of the validation groups.

Let valueExpression be the return from calling javax.faces.component.UIComponent.getValueExpression(java.lang.String) on the argument component, passing the literal string “value” (without the quotes) as an argument. If this application is running in an environment with a Unified EL Implementation for Java EE6 or later, obtain the ValueReference from valueExpression and let valueBaseClase be the return from calling ValueReference.getBase() and valueProperty be the return from calling ValueReference.getProperty(). If an earlier version of the Unified EL is present, use the appropriate methods to inspect valueExpression and derive values for valueBaseClass and valueProperty.

If no ValueReference can be obtained, take no action and return.

If ValueReference.getBase() return null, take no action and return.

Obtain the javax.validation.ValidatorContext from the javax.validation.ValidatorFactory.

Decorate the javax.validation.MessageInterpolator returned from javax.validation.ValidatorFactory.getMessageInterpolator() with one that leverages the Locale returned from javax.faces.component.UIViewRoot.getLocale(), and store it in the ValidatorContext using javax.validation.ValidatorContext.messageInterpolator(javax.validation.MessageInterpolator).

Obtain the javax.validation.Validator instance from the validatorContext.

Obtain a javax.validation.BeanDescriptor from the javax.validation.Validator. If hasConstraints() on the BeanDescriptor returns false, take no action and return. Otherwise proceed.

Call javax.validation.Validator.validateValue(java.lang.Class,java.lang.String,java.lang.Object,java.lang.Class[]), passing valueBaseClass, valueProperty, the value argument, and validatorGroupsArray as arguments.

If the returned Set<javax.validation.ConstraintViolation> is non-empty, for each element in the Set, create a javax.faces.application.FacesMessage where the summary and detail are the return from calling javax.validation.ConstraintViolation.getMessage(). Capture all such FacesMessage instances into a Collection and pass them to ValidatorException.(java.util.Collection), throwing the new exception.

Parameters:
context
component
value
Throws:
ValidatorException
    public void validate(FacesContext context,
                         UIComponent component,
                         Object value) {
        if (context == null) {
            throw new NullPointerException();
        }
        if (component == null) {
            throw new NullPointerException();
        }
        ValueExpression valueExpression = component.getValueExpression("value");
        if (valueExpression == null) {
            return;
        }
        ValidatorFactory validatorFactory;
        Object cachedObject = context.getExternalContext().getApplicationMap().get();
        if (cachedObject instanceof ValidatorFactory) {
            validatorFactory = (ValidatorFactorycachedObject;
        }
        else {
            try {
                validatorFactory = Validation.buildDefaultValidatorFactory();
            }
            catch (ValidationException e) {
                throw new FacesException("Could not build a default Bean Validator factory"e);
            }
            context.getExternalContext().getApplicationMap().put(validatorFactory);
        }
        ValidatorContext validatorContext = validatorFactory.usingContext();
        MessageInterpolator jsfMessageInterpolator = 
                new JsfAwareMessageInterpolator(context
                           validatorFactory.getMessageInterpolator());
        validatorContext.messageInterpolator(jsfMessageInterpolator);
        javax.validation.Validator beanValidator = validatorContext.getValidator();
        Class[] validationGroupsArray = parseValidationGroups(getValidationGroups());
        
        // PENDING(rlubke, driscoll): When EL 1.3 is present, we won't need
        // this.
        
        ValueExpressionAnalyzer expressionAnalyzer = 
                new ValueExpressionAnalyzer(valueExpression);
        
        ValueReference valueReference = expressionAnalyzer.getReference(context.getELContext());
        if (valueReference == null) {
            return;
        }
        if (isResolvable(valueReferencevalueExpression)) {
            Set<ConstraintViolationviolations = null;
            try {
                //noinspection unchecked
                violations =
                        beanValidator.validateValue(valueReference.getBaseClass(),
                                                    valueReference.getProperty(),
                                                    value,
                                                    validationGroupsArray);
            } catch (IllegalArgumentException iae) {
                String failureMessage = "Unable to validate expression " +
                        valueExpression.getExpressionString() +
               " using Bean Validation.  Unable to get value of expression. "+
                        " Message from Bean Validation: " + iae.getMessage();
                .fine(failureMessage);
            }
            if (violations != null && !violations.isEmpty()) {
                ValidatorException toThrow;
                if (1 == violations.size()) {
                    ConstraintViolation violation = violations.iterator().next();
                    toThrow = new ValidatorException(MessageFactory.getMessage(
                          context,
                          ,
                          violation.getMessage(),
                          MessageFactory.getLabel(contextcomponent)));
                } else {
                    Set<FacesMessagemessages = new LinkedHashSet<FacesMessage>(
                          violations.size());
                    for (ConstraintViolation violation : violations) {
                        messages.add(MessageFactory.getMessage(context,
                                                               ,
                                                               violation.getMessage(),
                                                               MessageFactory.getLabel(
                                                                     context,
                                                                     component)));
                    }
                    toThrow = new ValidatorException(messages);
                }
                throw toThrow;
            }
        }        
    }
    
    private boolean isResolvable(ValueReference ref
            ValueExpression valueExpression) {
        Boolean result = null;
        String failureMessage = null;
        
        if (null == valueExpression) {
            failureMessage = "Unable to validate expression using Bean "
                    "Validation.  Expression must not be null.";
            result = false;
        } else if (null == ref) {
            failureMessage = "Unable to validate expression " + 
                    valueExpression.getExpressionString() +
                    " using Bean Validation.  Unable to get value of expression.";
            result = false;
        } else {
            Class baseClass = ref.getBaseClass();
            // case 1, base classes of Map, List, or Array are not resolvable
            if (null != baseClass) {
                if (Map.class.isAssignableFrom(baseClass) ||
                        Collection.class.isAssignableFrom(baseClass) ||
                        Array.class.isAssignableFrom(baseClass)) {
                    failureMessage = "Unable to validate expression " + valueExpression.getExpressionString() +
                            " using Bean Validation.  Expression evaluates to a Map, List or array.";
                    result = false;
                }
            }
        }
        result = ((null != result) ? result : true);
        if (!result) {
            .fine(failureMessage);
        }
        return result;
    }
    
    private Class[] parseValidationGroups(String validationGroupsStr) {
        if ( != null) {
            return ;
        }
        if (validationGroupsStr == null) {
             = new Class[] { Default.class };
            return ;
        }
        List<ClassvalidationGroupsList = new ArrayList<Class>();
        String[] classNames = validationGroupsStr.split();
        for (String className : classNames) {
            className = className.trim();
            if (className.length() == 0) {
                continue;
            }
            if (className.equals(Default.class.getName())) {
                validationGroupsList.add(Default.class);
            }
            else {
                try {
                    validationGroupsList.add(Class.forName(classNamefalse, Thread.currentThread().getContextClassLoader()));
                } catch (ClassNotFoundException e1) {
                    try {
                        validationGroupsList.add(Class.forName(className));
                    } catch (ClassNotFoundException e2) {
                        throw new FacesException("Validation group not found: " + className);
                    }
                }
            }
        }
         = validationGroupsList.toArray(new Class[validationGroupsList.size()]);
        return ;
    }
    // ----------------------------------------------------- StateHolder Methods
    public Object saveState(FacesContext context) {
        if (context == null) {
            throw new NullPointerException();
        }
        if (!initialStateMarked()) {
            Object values[] = new Object[1];
            values[0] = ;
            return values;
        }
        return null;
    }
    public void restoreState(FacesContext contextObject state) {
        if (context == null) {
            throw new NullPointerException();
        }
        if (state != null) {
            Object values[] = (Object[]) state;
             = (Stringvalues[0];
        }
    }
    private boolean initialState;
    public void markInitialState() {
         = true;
    }
    public boolean initialStateMarked() {
        return ;
    }
    public void clearInitialState() {
         = false;
    }
    private boolean transientValue = false;
    public boolean isTransient() {
        return this.;
    }
    public void setTransient(boolean transientValue) {
        this. = transientValue;
    }
    private static class JsfAwareMessageInterpolator implements MessageInterpolator {
        private FacesContext context;
        private MessageInterpolator delegate;
        public JsfAwareMessageInterpolator(FacesContext contextMessageInterpolator delegate) {
            this. = context;
            this. = delegate;
        }
        public String interpolate(String messageMessageInterpolator.Context context) {
            Locale locale = this..getViewRoot().getLocale();
            if (locale == null) {
                locale = Locale.getDefault();
            }
            return .interpolate(messagecontextlocale);
        }
        public String interpolate(String messageMessageInterpolator.Context contextLocale locale) {
            return .interpolate(messagecontextlocale);
        }
    }
New to GrepCode? Check out our FAQ X