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.util.MessageFactory; 021 import org.apache.myfaces.tobago.apt.annotation.Validator; 022 023 import javax.faces.validator.LengthValidator; 024 import javax.faces.validator.ValidatorException; 025 import javax.faces.context.FacesContext; 026 import javax.faces.component.UIComponent; 027 import javax.faces.component.EditableValueHolder; 028 import javax.faces.application.FacesMessage; 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 private Integer minimum; 044 private Integer maximum; 045 046 public SubmittedValueLengthValidator() { 047 } 048 049 public SubmittedValueLengthValidator(int maximum) { 050 setMaximum(maximum); 051 } 052 053 public SubmittedValueLengthValidator(int maximum, int minimum) { 054 setMaximum(maximum); 055 setMinimum(minimum); 056 } 057 058 public int getMinimum() { 059 return minimum != null ? minimum : 0; 060 } 061 062 public void setMinimum(int minimum) { 063 if (minimum > 0) { 064 this.minimum = minimum; 065 } 066 } 067 068 public int getMaximum() { 069 return maximum != null ? maximum : 0; 070 } 071 072 public void setMaximum(int maximum) { 073 if (maximum > 0) { 074 this.maximum = maximum; 075 } 076 } 077 078 public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) throws ValidatorException { 079 if (value != null && uiComponent instanceof EditableValueHolder) { 080 String submittedValue = ((EditableValueHolder) uiComponent).getSubmittedValue().toString(); 081 if (maximum != null && submittedValue.length() > maximum) { 082 Object[] args = {maximum, uiComponent.getId()}; 083 FacesMessage facesMessage = 084 MessageFactory.createFacesMessage(facesContext, MAXIMUM_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, args); 085 throw new ValidatorException(facesMessage); 086 } 087 if (minimum != null && submittedValue.length() < minimum) { 088 Object[] args = {minimum, uiComponent.getId()}; 089 FacesMessage facesMessage = 090 MessageFactory.createFacesMessage(facesContext, MINIMUM_MESSAGE_ID, FacesMessage.SEVERITY_ERROR, args); 091 throw new ValidatorException(facesMessage); 092 } 093 } 094 } 095 096 public Object saveState(FacesContext context) { 097 Object[] values = new Object[2]; 098 values[0] = maximum; 099 values[1] = minimum; 100 return values; 101 102 } 103 104 105 public void restoreState(FacesContext context, Object state) { 106 Object[] values = (Object[]) state; 107 maximum = (Integer) values[0]; 108 minimum = (Integer) values[1]; 109 } 110 111 112 public boolean equals(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 SubmittedValueLengthValidator validator = (SubmittedValueLengthValidator) o; 124 125 if (maximum != null ? !maximum.equals(validator.maximum) : validator.maximum != null) { 126 return false; 127 } 128 if (minimum != null ? !minimum.equals(validator.minimum) : validator.minimum != null) { 129 return false; 130 } 131 132 return true; 133 } 134 135 public int hashCode() { 136 int result; 137 result = (minimum != null ? minimum.hashCode() : 0); 138 result = 31 * result + (maximum != null ? maximum.hashCode() : 0); 139 return result; 140 } 141 }