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