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 public DelegateValidator(ValueExpression id, ValueExpression binding, String converterIdString)
61 {
62 super();
63 _validatorId = id;
64 _binding = binding;
65 _validatorIdString = converterIdString;
66 }
67
68 public boolean isTransient()
69 {
70 return false;
71 }
72
73 public void restoreState(FacesContext facesContext, Object state)
74 {
75 Object[] values = (Object[]) state;
76 _validatorId = (ValueExpression) values[0];
77 _binding = (ValueExpression) values[1];
78 _validatorIdString = (String) values[2];
79 }
80
81 public Object saveState(FacesContext facesContext)
82 {
83 Object[] values = new Object[3];
84 values[0] = _validatorId;
85 values[1] = _binding;
86 values[2] = _validatorIdString;
87 return values;
88 }
89
90 public void setTransient(boolean arg0)
91 {
92
93 }
94
95 private Validator _getDelegate()
96 {
97 return _createValidator();
98 }
99
100 private Validator _createValidator()
101 {
102 FacesContext facesContext = FacesContext.getCurrentInstance();
103 ELContext elContext = facesContext.getELContext();
104 if (null != _binding)
105 {
106 Object validator;
107 try
108 {
109 validator = _binding.getValue(elContext);
110 } catch (Exception e)
111 {
112 throw new ValidatorException(new FacesMessage("Error while creating the Validator"), e);
113 }
114 if (validator instanceof Validator)
115 {
116 return (Validator) validator;
117 }
118 }
119 Application application = facesContext.getApplication();
120 Validator validator = null;
121 try
122 {
123
124 if (null != _validatorIdString)
125 {
126 validator = application.createValidator(_validatorIdString);
127 } else if (null != _validatorId)
128 {
129 String validatorId = (String) _validatorId.getValue(elContext);
130 validator = application.createValidator(validatorId);
131 }
132 } catch (Exception e)
133 {
134 throw new ValidatorException(new FacesMessage("Error while creating the Validator"), e);
135 }
136
137 if (null != validator)
138 {
139 if (null != _binding)
140 {
141 _binding.setValue(elContext, validator);
142 }
143 return validator;
144 }
145 throw new ValidatorException(new FacesMessage("validatorId and/or binding must be specified"));
146 }
147
148 public void validate(FacesContext facesContext, UIComponent component, Object value)
149 throws ValidatorException
150 {
151 _getDelegate().validate(facesContext, component, value);
152 }
153
154 }