01: /*
02: * Copyright 2004-2006 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.myfaces.taglib.core;
17:
18: import javax.el.ELContext;
19: import javax.el.ValueExpression;
20: import javax.faces.context.FacesContext;
21: import javax.faces.validator.Validator;
22: import javax.servlet.jsp.JspException;
23:
24: /**
25: * This is the base Tag for all ValidatorTags which got a minimum and a maimum attribute.
26: *
27: * @author Andreas Berger (latest modification by $Author: baranda $)
28: * @version $Revision: 467963 $ $Date: 2006-10-26 13:33:25 +0200 (Do, 26 Okt 2006) $
29: * @since 1.2
30: */
31: public abstract class GenericMinMaxValidatorTag<T> extends ValidatorTag {
32: protected ValueExpression _minimum;
33: protected ValueExpression _maximum;
34: protected T _min = null;
35: protected T _max = null;
36:
37: public void setMinimum(ValueExpression minimum) {
38: _minimum = minimum;
39: }
40:
41: public void setMaximum(ValueExpression maximum) {
42: _maximum = maximum;
43: }
44:
45: public void release() {
46: _minimum = null;
47: _maximum = null;
48: _min = null;
49: _max = null;
50: }
51:
52: /**
53: * This method returns the Validator, you have to cast it to the correct type
54: * and apply the min and max values.
55: *
56: * @return
57: * @throws JspException
58: */
59: protected Validator createValidator() throws JspException {
60: if (null == _minimum && null == _maximum) {
61: throw new JspException(
62: "a minimum and / or a maximum have to be specified");
63: }
64: ELContext elContext = FacesContext.getCurrentInstance()
65: .getELContext();
66: if (null != _minimum) {
67: _min = getValue(_minimum.getValue(elContext));
68: }
69: if (null != _maximum) {
70: _max = getValue(_maximum.getValue(elContext));
71: }
72: if (null != _minimum && null != _maximum) {
73: if (!isMinLTMax()) {
74: throw new JspException(
75: "maximum limit must be greater than the minimum limit");
76: }
77: }
78: return super .createValidator();
79: }
80:
81: /**
82: * @return true if min is lower than max
83: */
84: protected abstract boolean isMinLTMax();
85:
86: /**
87: * Wrapper method.
88: * @param value
89: * @return
90: */
91: protected abstract T getValue(Object value);
92: }
|