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
30
31
32
33
34
35
36 public class ValidatorTag extends ValidatorELTag
37 {
38 private ValueExpression _validatorId;
39 private ValueExpression _binding;
40 private String _validatorIdString = null;
41
42 public void setValidatorId(ValueExpression validatorId)
43 {
44 _validatorId = validatorId;
45 }
46
47 public void setBinding(ValueExpression binding)
48 {
49 _binding = binding;
50 }
51
52
53
54
55
56
57 public void setValidatorIdString(String validatorIdString)
58 {
59 _validatorIdString = validatorIdString;
60 }
61
62 @Override
63 public void release()
64 {
65 super.release();
66 _validatorId = null;
67 _binding = null;
68 _validatorIdString = null;
69 }
70
71 @Override
72 protected Validator createValidator() throws javax.servlet.jsp.JspException
73 {
74 FacesContext facesContext = FacesContext.getCurrentInstance();
75 ELContext elContext = facesContext.getELContext();
76 if (null != _binding)
77 {
78 Object validator;
79 try
80 {
81 validator = _binding.getValue(elContext);
82 }
83 catch (Exception e)
84 {
85 throw new JspException("Error while creating the Validator", e);
86 }
87 if (validator instanceof Validator)
88 {
89 return (Validator)validator;
90 }
91 }
92 Application application = facesContext.getApplication();
93 Validator validator = null;
94 try
95 {
96
97 if (null != _validatorIdString)
98 {
99 validator = application.createValidator(_validatorIdString);
100 }
101 else if (null != _validatorId)
102 {
103 String validatorId = (String)_validatorId.getValue(elContext);
104 validator = application.createValidator(validatorId);
105 }
106 }
107 catch (Exception e)
108 {
109 throw new JspException("Error while creating the Validator", e);
110 }
111
112 if (null != validator)
113 {
114 if (null != _binding)
115 {
116 _binding.setValue(elContext, validator);
117 }
118 return validator;
119 }
120 throw new JspException("validatorId and/or binding must be specified");
121 }
122
123 }