View Javadoc

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  
20  package org.apache.myfaces.tobago.validator;
21  
22  import org.apache.myfaces.tobago.apt.annotation.Validator;
23  import org.apache.myfaces.tobago.util.MessageUtils;
24  
25  import javax.faces.application.FacesMessage;
26  import javax.faces.component.EditableValueHolder;
27  import javax.faces.component.UIComponent;
28  import javax.faces.context.FacesContext;
29  import javax.faces.validator.LengthValidator;
30  import javax.faces.validator.ValidatorException;
31  
32  /**
33   * <p><strong>SubmittedLengthValidator</strong> is a {@link Validator} that checks
34   * the number of characters in the submitted value of the
35   * associated component.
36   */
37  @Validator(id = SubmittedValueLengthValidator.VALIDATOR_ID)
38  public class SubmittedValueLengthValidator extends LengthValidator {
39    public static final String VALIDATOR_ID = "org.apache.myfaces.tobago.SubmittedValueLength";
40  
41    private Integer minimum;
42    private Integer maximum;
43  
44    public SubmittedValueLengthValidator() {
45    }
46  
47    public SubmittedValueLengthValidator(int maximum) {
48      setMaximum(maximum);
49    }
50  
51    public SubmittedValueLengthValidator(int maximum, int minimum) {
52      setMaximum(maximum);
53      setMinimum(minimum);
54    }
55  
56    public int getMinimum() {
57      return minimum != null ? minimum : 0;
58    }
59  
60    public void setMinimum(int minimum) {
61      if (minimum > 0) {
62        this.minimum = minimum;
63      }
64    }
65  
66    public int getMaximum() {
67      return maximum != null ? maximum : 0;
68    }
69  
70    public void setMaximum(int maximum) {
71      if (maximum > 0) {
72        this.maximum = maximum;
73      }
74    }
75  
76    public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) throws ValidatorException {
77      if (value != null && uiComponent instanceof EditableValueHolder) {
78        String submittedValue = ((EditableValueHolder) uiComponent).getSubmittedValue().toString();
79        if (maximum != null && submittedValue.length() > maximum) {
80          Object[] args = {maximum, uiComponent.getId()};
81          FacesMessage facesMessage = MessageUtils.getMessage(facesContext,
82              facesContext.getViewRoot().getLocale(), FacesMessage.SEVERITY_ERROR, MAXIMUM_MESSAGE_ID, args);
83          throw new ValidatorException(facesMessage);
84        }
85        if (minimum != null && submittedValue.length() < minimum) {
86          Object[] args = {minimum, uiComponent.getId()};
87          FacesMessage facesMessage = MessageUtils.getMessage(facesContext,
88              facesContext.getViewRoot().getLocale(), FacesMessage.SEVERITY_ERROR, MINIMUM_MESSAGE_ID, args);
89          throw new ValidatorException(facesMessage);
90        }
91      }
92    }
93  
94    public Object saveState(FacesContext context) {
95      Object[] values = new Object[2];
96      values[0] = maximum;
97      values[1] = minimum;
98      return values;
99    }
100 
101   public void restoreState(FacesContext context, Object state) {
102     Object[] values = (Object[]) state;
103     maximum = (Integer) values[0];
104     minimum = (Integer) values[1];
105   }
106 
107   public boolean equals(Object o) {
108     if (this == o) {
109       return true;
110     }
111     if (o == null || getClass() != o.getClass()) {
112       return false;
113     }
114     if (!super.equals(o)) {
115       return false;
116     }
117 
118     SubmittedValueLengthValidator validator = (SubmittedValueLengthValidator) o;
119 
120     if (maximum != null ? !maximum.equals(validator.maximum) : validator.maximum != null) {
121       return false;
122     }
123     if (minimum != null ? !minimum.equals(validator.minimum) : validator.minimum != null) {
124       return false;
125     }
126 
127     return true;
128   }
129 
130   public int hashCode() {
131     int result;
132     result = (minimum != null ? minimum.hashCode() : 0);
133     result = 31 * result + (maximum != null ? maximum.hashCode() : 0);
134     return result;
135   }
136 }