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.RegexValidator;
26 import javax.faces.validator.Validator;
27 import javax.faces.webapp.ValidatorELTag;
28 import javax.servlet.jsp.JspException;
29
30
31
32
33
34
35
36 public class ValidateRegexTag extends ValidatorELTag
37 {
38 private static final long serialVersionUID = 8363913774859484811L;
39
40 private ValueExpression _pattern;
41
42 private ValueExpression _binding;
43
44 @Override
45 protected Validator createValidator() throws JspException
46 {
47 FacesContext fc = FacesContext.getCurrentInstance();
48 ELContext elc = fc.getELContext();
49 if (_binding != null)
50 {
51 Object validator;
52 try
53 {
54 validator = _binding.getValue(elc);
55 }
56 catch (Exception e)
57 {
58 throw new JspException("Error while creating the Validator", e);
59 }
60 if (validator instanceof RegexValidator)
61 {
62 return (Validator)validator;
63 }
64 }
65 if (null != _pattern)
66 {
67 Application appl = fc.getApplication();
68 RegexValidator validator = (RegexValidator) appl.createValidator(RegexValidator.VALIDATOR_ID);
69 String pattern = (String)_pattern.getValue(elc);
70 validator.setPattern(pattern);
71
72 if (_binding != null)
73 {
74 _binding.setValue(elc, validator);
75 }
76
77 return validator;
78 }
79 else
80 {
81 throw new AssertionError("pattern may not be null");
82 }
83 }
84
85 public ValueExpression getBinding()
86 {
87 return _binding;
88 }
89
90 public void setBinding(ValueExpression binding)
91 {
92 _binding = binding;
93 }
94
95 public ValueExpression getPattern()
96 {
97 return _pattern;
98 }
99
100 public void setPattern(ValueExpression pattern)
101 {
102 _pattern = pattern;
103 }
104
105 @Override
106 public void release()
107 {
108 _pattern = null;
109 _binding = null;
110 }
111 }