1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.taglib.core;
20
21 import javax.el.ELContext;
22 import javax.el.ValueExpression;
23 import javax.faces.application.Application;
24 import javax.faces.context.FacesContext;
25 import javax.faces.validator.Validator;
26 import javax.faces.webapp.ValidatorELTag;
27 import javax.servlet.jsp.JspException;
28
29 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspAttribute;
30 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspTag;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 @JSFJspTag(name = "f:validator", bodyContent = "empty")
55 public class ValidatorImplTag extends ValidatorELTag
56 {
57 private ValueExpression _validatorId;
58 private ValueExpression _binding;
59 private String _validatorIdString = null;
60
61
62
63
64 @JSFJspAttribute(className="javax.el.ValueExpression",
65 deferredValueType="java.lang.String")
66 public void setValidatorId(ValueExpression validatorId)
67 {
68 _validatorId = validatorId;
69 }
70
71
72
73
74 @JSFJspAttribute(className="javax.el.ValueExpression",
75 deferredValueType="javax.faces.validator.Validator")
76 public void setBinding(ValueExpression binding)
77 {
78 _binding = binding;
79 }
80
81
82
83
84
85
86 public void setValidatorIdString(String validatorIdString)
87 {
88 _validatorIdString = validatorIdString;
89 }
90
91 @Override
92 public void release()
93 {
94 super.release();
95 _validatorId = null;
96 _binding = null;
97 _validatorIdString = null;
98 }
99
100 @Override
101 protected Validator createValidator() throws javax.servlet.jsp.JspException
102 {
103
104 if (_validatorIdString != null)
105 {
106 return this.createClassicValidator();
107 }
108 if (_validatorId != null && _validatorId.isLiteralText())
109 {
110 return this.createClassicValidator();
111 }
112
113 return new DelegateValidator(_validatorId, _binding, _validatorIdString);
114 }
115
116 protected Validator createClassicValidator() throws javax.servlet.jsp.JspException
117 {
118 FacesContext facesContext = FacesContext.getCurrentInstance();
119 ELContext elContext = facesContext.getELContext();
120 if (null != _binding)
121 {
122 Object validator;
123 try
124 {
125 validator = _binding.getValue(elContext);
126 }
127 catch (Exception e)
128 {
129 throw new JspException("Error while creating the Validator", e);
130 }
131 if (validator instanceof Validator)
132 {
133 return (Validator)validator;
134 }
135 }
136 Application application = facesContext.getApplication();
137 Validator validator = null;
138 try
139 {
140
141 if (null != _validatorIdString)
142 {
143 validator = application.createValidator(_validatorIdString);
144 }
145 else if (null != _validatorId)
146 {
147 String validatorId = (String)_validatorId.getValue(elContext);
148 validator = application.createValidator(validatorId);
149 }
150 }
151 catch (Exception e)
152 {
153 throw new JspException("Error while creating the Validator", e);
154 }
155
156 if (null != validator)
157 {
158 if (null != _binding)
159 {
160 _binding.setValue(elContext, validator);
161 }
162 return validator;
163 }
164 throw new JspException("validatorId and/or binding must be specified");
165 }
166
167 }