Coverage Report - javax.faces.validator.LengthValidator
 
Classes in this File Line Coverage Branch Coverage Complexity
LengthValidator
26%
19/71
13%
6/44
2.579
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package javax.faces.validator;
 20  
 
 21  
 import javax.faces.component.PartialStateHolder;
 22  
 import javax.faces.component.UIComponent;
 23  
 import javax.faces.context.FacesContext;
 24  
 
 25  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspProperty;
 26  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
 27  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFValidator;
 28  
 
 29  
 /**
 30  
  * Creates a validator and associateds it with the nearest parent
 31  
  * UIComponent.  When invoked, the validator ensures that values are
 32  
  * valid strings with a length that lies within the minimum and maximum
 33  
  * values specified.
 34  
  * 
 35  
  * Commonly associated with a h:inputText entity.
 36  
  * 
 37  
  * Unless otherwise specified, all attributes accept static values or EL expressions.
 38  
  * 
 39  
  * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
 40  
  */
 41  
 @JSFValidator(
 42  
     name="f:validateLength",
 43  
     bodyContent="empty",
 44  
     tagClass="org.apache.myfaces.taglib.core.ValidateLengthTag")
 45  
 @JSFJspProperty(
 46  
     name="binding", 
 47  
     returnType = "javax.faces.validator.LengthValidator",
 48  
     longDesc = "A ValueExpression that evaluates to a LengthValidator.")
 49  
 public class LengthValidator
 50  
         implements Validator, PartialStateHolder
 51  
 {
 52  
     // FIELDS
 53  
     public static final String     MAXIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MAXIMUM";
 54  
     public static final String     MINIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MINIMUM";
 55  
     public static final String     VALIDATOR_ID        = "javax.faces.Length";
 56  
 
 57  2
     private Integer _minimum = null;
 58  2
     private Integer _maximum = null;
 59  2
     private boolean _transient = false;
 60  
 
 61  
     // CONSTRUCTORS
 62  
     public LengthValidator()
 63  2
     {
 64  2
     }
 65  
 
 66  
     public LengthValidator(int maximum)
 67  0
     {
 68  0
         _maximum = Integer.valueOf(maximum);
 69  0
     }
 70  
 
 71  
     public LengthValidator(int maximum,
 72  
                            int minimum)
 73  0
     {
 74  0
         _maximum = Integer.valueOf(maximum);
 75  0
         _minimum = Integer.valueOf(minimum);
 76  0
     }
 77  
 
 78  
     // VALIDATE
 79  
     public void validate(FacesContext facesContext,
 80  
                          UIComponent uiComponent,
 81  
                          Object value)
 82  
             throws ValidatorException
 83  
     {
 84  2
         if (facesContext == null)
 85  
         {
 86  0
             throw new NullPointerException("facesContext");
 87  
         }
 88  2
         if (uiComponent == null)
 89  
         {
 90  0
             throw new NullPointerException("uiComponent");
 91  
         }
 92  
 
 93  2
         if (value == null)
 94  
         {
 95  0
             return;
 96  
         }
 97  
 
 98  2
         int length = value instanceof String ?
 99  
             ((String)value).length() : value.toString().length();
 100  
 
 101  2
         if (_minimum != null)
 102  
         {
 103  2
             if (length < _minimum.intValue())
 104  
             {
 105  2
                 Object[] args = {_minimum,_MessageUtils.getLabel(facesContext, uiComponent)};
 106  2
                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MINIMUM_MESSAGE_ID, args));
 107  
             }
 108  
         }
 109  
 
 110  0
         if (_maximum != null)
 111  
         {
 112  0
             if (length > _maximum.intValue())
 113  
             {
 114  0
                 Object[] args = {_maximum,_MessageUtils.getLabel(facesContext, uiComponent)};
 115  0
                 throw new ValidatorException(_MessageUtils.getErrorMessage(facesContext, MAXIMUM_MESSAGE_ID, args));
 116  
             }
 117  
         }
 118  0
     }
 119  
 
 120  
     // SETTER & GETTER
 121  
     
 122  
     /** 
 123  
      * The largest value that should be considered valid.
 124  
      * 
 125  
      */
 126  
     @JSFProperty(deferredValueType="java.lang.Integer")
 127  
     public int getMaximum()
 128  
     {
 129  0
         return _maximum != null ? _maximum.intValue() : 0;
 130  
     }
 131  
 
 132  
     public void setMaximum(int maximum)
 133  
     {
 134  0
         _maximum = Integer.valueOf(maximum);
 135  0
         clearInitialState();
 136  0
     }
 137  
 
 138  
     /**
 139  
      * The smallest value that should be considered valid.
 140  
      *  
 141  
      */
 142  
     @JSFProperty(deferredValueType="java.lang.Integer")
 143  
     public int getMinimum()
 144  
     {
 145  0
         return _minimum != null ? _minimum.intValue() : 0;
 146  
     }
 147  
 
 148  
     public void setMinimum(int minimum)
 149  
     {
 150  2
         _minimum = Integer.valueOf(minimum);
 151  2
         clearInitialState();
 152  2
     }
 153  
 
 154  
     public boolean isTransient()
 155  
     {
 156  0
         return _transient;
 157  
     }
 158  
 
 159  
     public void setTransient(boolean transientValue)
 160  
     {
 161  0
         _transient = transientValue;
 162  0
     }
 163  
 
 164  
     // RESTORE & SAVE STATE
 165  
     public Object saveState(FacesContext context)
 166  
     {
 167  0
         if (!initialStateMarked())
 168  
         {
 169  0
             Object values[] = new Object[2];
 170  0
             values[0] = _maximum;
 171  0
             values[1] = _minimum;
 172  0
             return values;
 173  
         }
 174  0
         return null;
 175  
     }
 176  
 
 177  
     public void restoreState(FacesContext context,
 178  
                              Object state)
 179  
     {
 180  0
         if (state != null)
 181  
         {
 182  0
             Object values[] = (Object[])state;
 183  0
             _maximum = (Integer)values[0];
 184  0
             _minimum = (Integer)values[1];
 185  
         }
 186  0
     }
 187  
 
 188  
     // MISC
 189  
     @Override
 190  
     public boolean equals(Object o)
 191  
     {
 192  0
         if (this == o)
 193  
         {
 194  0
             return true;
 195  
         }
 196  0
         if (!(o instanceof LengthValidator))
 197  
         {
 198  0
             return false;
 199  
         }
 200  
 
 201  0
         LengthValidator lengthValidator = (LengthValidator)o;
 202  
 
 203  0
         if (_maximum != null ? !_maximum.equals(lengthValidator._maximum) : lengthValidator._maximum != null)
 204  
         {
 205  0
             return false;
 206  
         }
 207  0
         if (_minimum != null ? !_minimum.equals(lengthValidator._minimum) : lengthValidator._minimum != null)
 208  
         {
 209  0
             return false;
 210  
         }
 211  
 
 212  0
         return true;
 213  
     }
 214  
 
 215  
     @Override
 216  
     public int hashCode()
 217  
     {
 218  0
         int result = _minimum != null ? _minimum.hashCode() : 0;
 219  0
         result = 31 * result + (_maximum != null ? _maximum.hashCode() : 0);
 220  0
         return result;
 221  
     }
 222  
 
 223  2
     private boolean _initialStateMarked = false;
 224  
 
 225  
     public void clearInitialState()
 226  
     {
 227  2
         _initialStateMarked = false;
 228  2
     }
 229  
 
 230  
     public boolean initialStateMarked()
 231  
     {
 232  0
         return _initialStateMarked;
 233  
     }
 234  
 
 235  
     public void markInitialState()
 236  
     {
 237  0
         _initialStateMarked = true;
 238  0
     }
 239  
     
 240  
     @JSFProperty(faceletsOnly=true)
 241  
     @SuppressWarnings("unused")
 242  
     private Boolean isDisabled()
 243  
     {
 244  0
         return null;
 245  
     }
 246  
     
 247  
     @JSFProperty(faceletsOnly=true)
 248  
     @SuppressWarnings("unused")
 249  
     private String getFor()
 250  
     {
 251  0
         return null;
 252  
     }
 253  
 }