|  1 |     | 
     | 
  |  2 |     | 
     | 
  |  3 |     | 
     | 
  |  4 |     | 
     | 
  |  5 |     | 
     | 
  |  6 |     | 
     | 
  |  7 |     | 
     | 
  |  8 |     | 
     | 
  |  9 |     | 
     | 
  |  10 |     | 
     | 
  |  11 |     | 
     | 
  |  12 |     | 
     | 
  |  13 |     | 
     | 
  |  14 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
   package javax.faces.validator;  | 
  |  17 |     | 
     | 
  |  18 |     | 
   import javax.faces.component.StateHolder;  | 
  |  19 |     | 
   import javax.faces.component.UIComponent;  | 
  |  20 |     | 
   import javax.faces.context.FacesContext;  | 
  |  21 |     | 
   import javax.faces.internal.FacesMessageUtil;  | 
  |  22 |     | 
   import javax.faces.internal.UIComponentUtil;  | 
  |  23 |     | 
   import javax.faces.internal.UIInputUtil;  | 
  |  24 |     | 
     | 
  |  25 |     | 
   import org.seasar.framework.util.AssertionUtil;  | 
  |  26 |     | 
     | 
  |  27 |     | 
     | 
  |  28 |     | 
     | 
  |  29 |     | 
     | 
  |  30 |     | 
   public class LengthValidator implements Validator, StateHolder { | 
  |  31 |     | 
     | 
  |  32 |     | 
       public static final String VALIDATOR_ID = "javax.faces.Length";  | 
  |  33 |     | 
     | 
  |  34 |     | 
       public static final String MAXIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MAXIMUM";  | 
  |  35 |     | 
     | 
  |  36 |     | 
       public static final String MINIMUM_MESSAGE_ID = "javax.faces.validator.LengthValidator.MINIMUM";  | 
  |  37 |     | 
     | 
  |  38 |    65 |        private int maximum = -1;  | 
  |  39 |     | 
     | 
  |  40 |    65 |        private int minimum = -1;  | 
  |  41 |     | 
     | 
  |  42 |    65 |        private boolean transientValue = false;  | 
  |  43 |     | 
     | 
  |  44 |     | 
       public LengthValidator() { | 
  |  45 |    60 |            super();  | 
  |  46 |    60 |        }  | 
  |  47 |     | 
     | 
  |  48 |     | 
       public LengthValidator(int maximum) { | 
  |  49 |    1 |            super();  | 
  |  50 |    1 |            this.maximum = maximum;  | 
  |  51 |    1 |        }  | 
  |  52 |     | 
     | 
  |  53 |     | 
       public LengthValidator(int maximum, int minimum) { | 
  |  54 |    4 |            super();  | 
  |  55 |    4 |            this.maximum = maximum;  | 
  |  56 |    4 |            this.minimum = minimum;  | 
  |  57 |    4 |        }  | 
  |  58 |     | 
     | 
  |  59 |     | 
       public boolean equals(Object obj) { | 
  |  60 |    0 |            if (!(obj instanceof LengthValidator)) { | 
  |  61 |    0 |                return false;  | 
  |  62 |     | 
           }  | 
  |  63 |     | 
     | 
  |  64 |    0 |            LengthValidator v = (LengthValidator) obj;  | 
  |  65 |    0 |            return maximum == v.maximum && minimum == v.minimum;  | 
  |  66 |     | 
       }  | 
  |  67 |     | 
     | 
  |  68 |     | 
       public int hashCode() { | 
  |  69 |    0 |            return maximum * minimum * 17;  | 
  |  70 |     | 
       }  | 
  |  71 |     | 
     | 
  |  72 |     | 
       public int getMaximum() { | 
  |  73 |    21 |            return maximum;  | 
  |  74 |     | 
       }  | 
  |  75 |     | 
     | 
  |  76 |     | 
       public int getMinimum() { | 
  |  77 |    5 |            return minimum;  | 
  |  78 |     | 
       }  | 
  |  79 |     | 
     | 
  |  80 |     | 
       public void setMaximum(int maximum) { | 
  |  81 |    35 |            this.maximum = maximum;  | 
  |  82 |    35 |        }  | 
  |  83 |     | 
     | 
  |  84 |     | 
       public void setMinimum(int minimum) { | 
  |  85 |    19 |            this.minimum = minimum;  | 
  |  86 |    19 |        }  | 
  |  87 |     | 
     | 
  |  88 |     | 
       public boolean isTransient() { | 
  |  89 |    16 |            return transientValue;  | 
  |  90 |     | 
       }  | 
  |  91 |     | 
     | 
  |  92 |     | 
       public void validate(FacesContext context, UIComponent component,  | 
  |  93 |     | 
               Object value) throws ValidatorException { | 
  |  94 |    21 |            AssertionUtil.assertNotNull("context", context); | 
  |  95 |    20 |            AssertionUtil.assertNotNull("component", component); | 
  |  96 |    19 |            if (UIInputUtil.isEmpty(value)) { | 
  |  97 |    2 |                return;  | 
  |  98 |     | 
           }  | 
  |  99 |    17 |            int length = getConvertedValueLength(value);  | 
  |  100 |    17 |            if (minimum > -1 && length < minimum) { | 
  |  101 |    16 |                Object[] args = { new Integer(minimum), | 
  |  102 |     | 
                       UIComponentUtil.getLabel(component) };  | 
  |  103 |    16 |                throw new ValidatorException(FacesMessageUtil.getMessage(context,  | 
  |  104 |     | 
                       getMinimumMessageId(), args), getMinimumMessageId(), args);  | 
  |  105 |     | 
           }  | 
  |  106 |     | 
     | 
  |  107 |    1 |            if (maximum > -1 && length > maximum) { | 
  |  108 |    1 |                Object[] args = { new Integer(maximum), | 
  |  109 |     | 
                       UIComponentUtil.getLabel(component) };  | 
  |  110 |    1 |                throw new ValidatorException(FacesMessageUtil.getMessage(context,  | 
  |  111 |     | 
                       getMaximumMessageId(), args), getMaximumMessageId(), args);  | 
  |  112 |     | 
           }  | 
  |  113 |    0 |        }  | 
  |  114 |     | 
     | 
  |  115 |     | 
       public void setTransient(boolean transientValue) { | 
  |  116 |    0 |            this.transientValue = transientValue;  | 
  |  117 |    0 |        }  | 
  |  118 |     | 
     | 
  |  119 |     | 
       public Object saveState(FacesContext context) { | 
  |  120 |    16 |            Object[] values = new Object[2];  | 
  |  121 |    16 |            values[0] = new Integer(maximum);  | 
  |  122 |    16 |            values[1] = new Integer(minimum);  | 
  |  123 |    16 |            return values;  | 
  |  124 |     | 
       }  | 
  |  125 |     | 
     | 
  |  126 |     | 
       public void restoreState(FacesContext context, Object state) { | 
  |  127 |    16 |            Object[] values = (Object[]) state;  | 
  |  128 |    16 |            maximum = ((Integer) values[0]).intValue();  | 
  |  129 |    16 |            minimum = ((Integer) values[1]).intValue();  | 
  |  130 |    16 |        }  | 
  |  131 |     | 
     | 
  |  132 |     | 
       protected int getConvertedValueLength(Object value) { | 
  |  133 |    17 |            int length = 0;  | 
  |  134 |    17 |            if (value instanceof String) { | 
  |  135 |    2 |                length = ((String) value).length();  | 
  |  136 |     | 
           } else { | 
  |  137 |    15 |                length = value.toString().length();  | 
  |  138 |     | 
           }  | 
  |  139 |    17 |            return length;  | 
  |  140 |     | 
       }  | 
  |  141 |     | 
     | 
  |  142 |     | 
       protected String getMinimumMessageId() { | 
  |  143 |    32 |            return MINIMUM_MESSAGE_ID;  | 
  |  144 |     | 
       }  | 
  |  145 |     | 
     | 
  |  146 |     | 
       protected String getMaximumMessageId() { | 
  |  147 |    2 |            return MAXIMUM_MESSAGE_ID;  | 
  |  148 |     | 
       }  | 
  |  149 |     | 
   }  |