Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: LengthValidator.java,v 1.49 2007/01/29 07:13:41 rlubke Exp $
   */
  
  /*
   * The contents of this file are subject to the terms
   * of the Common Development and Distribution License
   * (the License). You may not use this file except in
   * compliance with the License.
  * 
  * You can obtain a copy of the License at
  * https://javaserverfaces.dev.java.net/CDDL.html or
  * legal/CDDLv1.0.txt. 
  * See the License for the specific language governing
  * permission and limitations under the License.
  * 
  * When distributing Covered Code, include this CDDL
  * Header Notice in each file and include the License file
  * at legal/CDDLv1.0.txt.    
  * If applicable, add the following below the CDDL Header,
  * with the fields enclosed by brackets [] replaced by
  * your own identifying information:
  * "Portions Copyrighted [year] [name of copyright owner]"
  * 
  * [Name of File] [ver.__] [Date]
  * 
  * Copyright 2005 Sun Microsystems Inc. All Rights Reserved
  */
 
 package javax.faces.validator;
 
 

LengthValidator is a Validator that checks the number of characters in the String representation of the value of the associated component. The following algorithm is implemented:

  • Convert the passed value to a String, if necessary, by calling its toString() method.
  • If a maximum property has been configured on this Validator, check the length of the converted String against this limit. If the String length is larger than the specified maximum, throw a ValidatorException containing a a MAXIMUM_MESSAGE_ID message.
  • If a minimum property has been configured on this Validator, check the length of the converted String against this limit. If the String length is less than the specified minimum, throw a ValidatorException containing a a MINIMUM_MESSAGE_ID message.

For all of the above cases that cause a ValidatorException to be thrown, if there are parameters to the message that match up with validator parameters, the values of these parameters must be converted using the javax.faces.convert.Converter registered in the application under the converter id javax.faces.Number. This allows the values to be localized according to the current Locale.

 
 
 public class LengthValidator implements ValidatorStateHolder {
 
     // ------------------------------------------------------ Manifest Constants
 

    

The standard validator id for this validator.

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


    

The message identifier of the javax.faces.application.FacesMessage to be created if the maximum length check fails. The message format string for this message may optionally include the following placeholders:

  • {0} replaced by the configured maximum length.
  • {1} replaced by a String whose value is the label of the input component that produced this message.
 
     public static final String MAXIMUM_MESSAGE_ID =
          "javax.faces.validator.LengthValidator.MAXIMUM";


    

The message identifier of the javax.faces.application.FacesMessage to be created if the minimum length check fails. The message format string for this message may optionally include the following placeholders:

  • {0} replaced by the configured minimum length.
  • {1} replaced by a String whose value is the label of the input component that produced this message.
    public static final String MINIMUM_MESSAGE_ID =
         "javax.faces.validator.LengthValidator.MINIMUM";
    // ------------------------------------------------------------ Constructors


    

Construct a Validator with no preconfigured limits.

    public LengthValidator() {
        super();
    }


    

Construct a Validator with the specified preconfigured limit.

Parameters:
maximum Maximum value to allow
    public LengthValidator(int maximum) {
        super();
        setMaximum(maximum);
    }


    

Construct a Validator with the specified preconfigured limits.

Parameters:
maximum Maximum value to allow
minimum Minimum value to allow
    public LengthValidator(int maximumint minimum) {
        super();
        setMaximum(maximum);
        setMinimum(minimum);
    }
    // -------------------------------------------------------------- Properties
    private int maximum = 0;
    private boolean maximumSet = false;


    

Return the maximum length to be enforced by this Validator, or 0 if the maximum has not been set.

    public int getMaximum() {
        return (this.);
    }


    

Set the maximum length to be enforced by this Validator.

Parameters:
maximum The new maximum value
    public void setMaximum(int maximum) {
        this. = maximum;
        this. = true;
    }
    private int minimum = 0;
    private boolean minimumSet = false;


    

Return the minimum length to be enforced by this Validator, or 0 if the minimum has not been set.

    public int getMinimum() {
        return (this.);
    }


    

Set the minimum length to be enforced by this Validator.

Parameters:
minimum The new minimum value
    public void setMinimum(int minimum) {
        this. = minimum;
        this. = true;
    }
    // ------------------------------------------------------- Validator Methods

    
    public void validate(FacesContext context,
                         UIComponent component,
                         Object valuethrows ValidatorException {
        if ((context == null) || (component == null)) {
            throw new NullPointerException();
        }
        if (value != null) {
            String converted = stringValue(value);
            if ( &&
                 (converted.length() > )) {
                throw new ValidatorException(MessageFactory.getMessage
                     (context,
                          ,
                          integerToString(component,
                                    new Integer(), context),
                          MessageFactory.getLabel(contextcomponent)));
            }
            if ( &&
                 (converted.length() < )) {
                throw new ValidatorException(MessageFactory.getMessage
                     (context,
                          ,
                          integerToString(component,
                                    new Integer(), context),
                          MessageFactory.getLabel(contextcomponent)));
            }
        }
    }
    public boolean equals(Object otherObj) {
        if (!(otherObj instanceof LengthValidator)) {
            return false;
        }
        LengthValidator other = (LengthValidatorotherObj;
        return (( == other.maximum) &&
             ( == other.minimum) &&
             ( == other.maximumSet) &&
             ( == other.minimumSet));
    }
    public int hashCode() {
        int hashCode =  + 
             + Boolean.valueOf().hashCode()
             + Boolean.valueOf().hashCode();
        return (hashCode);
    }
    // -------------------------------------------------------- Private Methods


    

Return the specified attribute value, converted to a String.

Parameters:
attributeValue The attribute value to be converted
    private static String stringValue(Object attributeValue) {
        if (attributeValue == null) {
            return (null);
        } else if (attributeValue instanceof String) {
            return ((StringattributeValue);
        } else {
            return (attributeValue.toString());
        }
    }
    private static String integerToString(UIComponent component,
                                          Integer toConvert,
                                          FacesContext context) {
        Converter converter =
             context.getApplication().createConverter("javax.faces.Number");
        String result = converter.getAsString(contextcomponenttoConvert);
        return result;
    }
    // ----------------------------------------------------- StateHolder Methods
    public Object saveState(FacesContext context) {
        Object values[] = new Object[4];
        values[0] = new Integer();
        values[1] =  ? . : .;
        values[2] = new Integer();
        values[3] =  ? . : .;
        return (values);
    }
    public void restoreState(FacesContext contextObject state) {
        Object values[] = (Object[]) state;
         = ((Integervalues[0]).intValue();
         = ((Booleanvalues[1]).booleanValue();
         = ((Integervalues[2]).intValue();
         = ((Booleanvalues[3]).booleanValue();
    }
    private boolean transientValue = false;
    public boolean isTransient() {
        return (this.);
    }
    public void setTransient(boolean transientValue) {
        this. = transientValue;
    }
New to GrepCode? Check out our FAQ X