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.application.FacesMessage;
25 import javax.faces.component.StateHolder;
26 import javax.faces.component.UIComponent;
27 import javax.faces.context.FacesContext;
28 import javax.faces.validator.Validator;
29 import javax.faces.validator.ValidatorException;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class DelegateValidator implements Validator, StateHolder
50 {
51
52 private ValueExpression _validatorId;
53 private ValueExpression _binding;
54 private String _validatorIdString = null;
55
56 public DelegateValidator()
57 {
58
59 }
60
61 public DelegateValidator(ValueExpression id, ValueExpression binding, String converterIdString)
62 {
63 super();
64 _validatorId = id;
65 _binding = binding;
66 _validatorIdString = converterIdString;
67 }
68
69 public boolean isTransient()
70 {
71 return false;
72 }
73
74 public void restoreState(FacesContext facesContext, Object state)
75 {
76 Object[] values = (Object[]) state;
77 _validatorId = (ValueExpression) values[0];
78 _binding = (ValueExpression) values[1];
79 _validatorIdString = (String) values[2];
80 }
81
82 public Object saveState(FacesContext facesContext)
83 {
84 Object[] values = new Object[3];
85 values[0] = _validatorId;
86 values[1] = _binding;
87 values[2] = _validatorIdString;
88 return values;
89 }
90
91 public void setTransient(boolean arg0)
92 {
93
94 }
95
96 private Validator _getDelegate()
97 {
98 return _createValidator();
99 }
100
101 private Validator _createValidator()
102 {
103 FacesContext facesContext = FacesContext.getCurrentInstance();
104 ELContext elContext = facesContext.getELContext();
105 if (null != _binding)
106 {
107 Object validator;
108 try
109 {
110 validator = _binding.getValue(elContext);
111 }
112 catch (Exception e)
113 {
114 throw new ValidatorException(new FacesMessage("Error while creating the Validator"), e);
115 }
116 if (validator instanceof Validator)
117 {
118 return (Validator) validator;
119 }
120 }
121 Application application = facesContext.getApplication();
122 Validator validator = null;
123 try
124 {
125
126 if (null != _validatorIdString)
127 {
128 validator = application.createValidator(_validatorIdString);
129 }
130 else if (null != _validatorId)
131 {
132 String validatorId = (String) _validatorId.getValue(elContext);
133 validator = application.createValidator(validatorId);
134 }
135 }
136 catch (Exception e)
137 {
138 throw new ValidatorException(new FacesMessage("Error while creating the Validator"), e);
139 }
140
141 if (null != validator)
142 {
143 if (null != _binding)
144 {
145 _binding.setValue(elContext, validator);
146 }
147 return validator;
148 }
149 throw new ValidatorException(new FacesMessage("validatorId and/or binding must be specified"));
150 }
151
152 public void validate(FacesContext facesContext, UIComponent component, Object value)
153 throws ValidatorException
154 {
155 _getDelegate().validate(facesContext, component, value);
156 }
157
158 }