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 * Created by IntelliJ IDEA. 032 * User: bommel 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 }