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(final int maximum) {
48      setMaximum(maximum);
49    }
50  
51    public SubmittedValueLengthValidator(final int maximum, final 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(final 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(final int maximum) {
71      if (maximum > 0) {
72        this.maximum = maximum;
73      }
74    }
75  
76    public void validate(final FacesContext facesContext, final UIComponent uiComponent, final Object value)
77        throws ValidatorException {
78      if (value != null && uiComponent instanceof EditableValueHolder) {
79        final String submittedValue = ((EditableValueHolder) uiComponent).getSubmittedValue().toString();
80        if (maximum != null && submittedValue.length() > maximum) {
81          final Object[] args = {maximum, uiComponent.getId()};
82          final FacesMessage facesMessage = MessageUtils.getMessage(facesContext,
83              facesContext.getViewRoot().getLocale(), FacesMessage.SEVERITY_ERROR, MAXIMUM_MESSAGE_ID, args);
84          throw new ValidatorException(facesMessage);
85        }
86        if (minimum != null && submittedValue.length() < minimum) {
87          final Object[] args = {minimum, uiComponent.getId()};
88          final FacesMessage facesMessage = MessageUtils.getMessage(facesContext,
89              facesContext.getViewRoot().getLocale(), FacesMessage.SEVERITY_ERROR, MINIMUM_MESSAGE_ID, args);
90          throw new ValidatorException(facesMessage);
91        }
92      }
93    }
94  
95    public Object saveState(final FacesContext context) {
96      final Object[] values = new Object[2];
97      values[0] = maximum;
98      values[1] = minimum;
99      return values;
100   }
101 
102   public void restoreState(final FacesContext context, final Object state) {
103     final Object[] values = (Object[]) state;
104     maximum = (Integer) values[0];
105     minimum = (Integer) values[1];
106   }
107 
108   public boolean equals(final Object o) {
109     if (this == o) {
110       return true;
111     }
112     if (o == null || getClass() != o.getClass()) {
113       return false;
114     }
115     if (!super.equals(o)) {
116       return false;
117     }
118 
119     final SubmittedValueLengthValidator validator = (SubmittedValueLengthValidator) o;
120 
121     if (maximum != null ? !maximum.equals(validator.maximum) : validator.maximum != null) {
122       return false;
123     }
124     if (minimum != null ? !minimum.equals(validator.minimum) : validator.minimum != null) {
125       return false;
126     }
127 
128     return true;
129   }
130 
131   public int hashCode() {
132     int result;
133     result = (minimum != null ? minimum.hashCode() : 0);
134     result = 31 * result + (maximum != null ? maximum.hashCode() : 0);
135     return result;
136   }
137 }