001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.myfaces.tobago.validator;
021
022 import org.apache.myfaces.tobago.util.MessageFactory;
023 import org.apache.myfaces.tobago.apt.annotation.Validator;
024
025 import javax.faces.validator.LengthValidator;
026 import javax.faces.validator.ValidatorException;
027 import javax.faces.context.FacesContext;
028 import javax.faces.component.UIComponent;
029 import javax.faces.component.EditableValueHolder;
030 import javax.faces.application.FacesMessage;
031
032 /*
033 * Date: Oct 16, 2006
034 * Time: 11:58:47 PM
035 */
036 /**
037 * <p><strong>SubmittedLengthValidator</strong> is a {@link Validator} that checks
038 * the number of characters in the submitted value of the
039 * associated component.
040 */
041
042 @Validator(id = SubmittedValueLengthValidator.VALIDATOR_ID)
043 public class SubmittedValueLengthValidator extends LengthValidator {
044 public static final String VALIDATOR_ID = "org.apache.myfaces.tobago.SubmittedValueLength";
045 private Integer minimum;
046 private Integer maximum;
047
048 public SubmittedValueLengthValidator() {
049 }
050
051 public SubmittedValueLengthValidator(int maximum) {
052 setMaximum(maximum);
053 }
054
055 public SubmittedValueLengthValidator(int maximum, int minimum) {
056 setMaximum(maximum);
057 setMinimum(minimum);
058 }
059
060 public int getMinimum() {
061 return minimum != null ? minimum : 0;
062 }
063
064 public void setMinimum(int minimum) {
065 if (minimum > 0) {
066 this.minimum = minimum;
067 }
068 }
069
070 public int getMaximum() {
071 return maximum != null ? maximum : 0;
072 }
073
074 public void setMaximum(int maximum) {
075 if (maximum > 0) {
076 this.maximum = maximum;
077 }
078 }
079
080 public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) throws ValidatorException {
081 if (value != null && uiComponent instanceof EditableValueHolder) {
082 String submittedValue = ((EditableValueHolder) uiComponent).getSubmittedValue().toString();
083 if (maximum != null && submittedValue.length() > maximum) {
084 Object[] args = {maximum, uiComponent.getId()};
085 FacesMessage facesMessage =
086 MessageFactory.createFacesMessage(facesContext, MAXIMUM_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, args);
087 throw new ValidatorException(facesMessage);
088 }
089 if (minimum != null && submittedValue.length() < minimum) {
090 Object[] args = {minimum, uiComponent.getId()};
091 FacesMessage facesMessage =
092 MessageFactory.createFacesMessage(facesContext, MINIMUM_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, args);
093 throw new ValidatorException(facesMessage);
094 }
095 }
096 }
097
098 public Object saveState(FacesContext context) {
099 Object[] values = new Object[2];
100 values[0] = maximum;
101 values[1] = minimum;
102 return values;
103
104 }
105
106
107 public void restoreState(FacesContext context, Object state) {
108 Object[] values = (Object[]) state;
109 maximum = (Integer) values[0];
110 minimum = (Integer) values[1];
111 }
112
113
114 public boolean equals(Object o) {
115 if (this == o) {
116 return true;
117 }
118 if (o == null || getClass() != o.getClass()) {
119 return false;
120 }
121 if (!super.equals(o)) {
122 return false;
123 }
124
125 SubmittedValueLengthValidator validator = (SubmittedValueLengthValidator) o;
126
127 if (maximum != null ? !maximum.equals(validator.maximum) : validator.maximum != null) {
128 return false;
129 }
130 if (minimum != null ? !minimum.equals(validator.minimum) : validator.minimum != null) {
131 return false;
132 }
133
134 return true;
135 }
136
137 public int hashCode() {
138 int result;
139 result = (minimum != null ? minimum.hashCode() : 0);
140 result = 31 * result + (maximum != null ? maximum.hashCode() : 0);
141 return result;
142 }
143 }