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