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