2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.shale.clay.component.chain;
22
23 import javax.faces.component.EditableValueHolder;
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.el.ValueBinding;
27 import javax.faces.validator.Validator;
28
29 import org.apache.commons.chain.Context;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.shale.clay.config.beans.AttributeBean;
33 import org.apache.shale.clay.config.beans.ComponentBean;
34
35 /***
36 * <p>
37 * Creates an object implementing the {@link Validator} interface and assigns it
38 * to the <code>parent</code> from the {@link ClayContext}.
39 * </p>
40 */
41 public class CreateValidatorCommand extends AbstractCommand {
42
43 /***
44 * <p>
45 * Common Logger utility.
46 * </p>
47 */
48 private static Log log;
49 static {
50 log = LogFactory.getLog(CreateValidatorCommand.class);
51 }
52
53 /***
54 * <p>
55 * Creates a faces validator object that is registered in the
56 * <strong>faces-config.xml</strong> file and assigns it to the parent.
57 * </p>
58 *
59 * @param context commons chains
60 * @return <code>true</code> if the chain is final
61 * @exception Exception propagated to the top chain
62 */
63 public boolean execute(Context context) throws Exception {
64
65 boolean isFinal = false;
66
67 ClayContext clayContext = (ClayContext) context;
68 if (clayContext == null) {
69 throw new NullPointerException(getMessages()
70 .getMessage("clay.null.clayContext"));
71 }
72
73 UIComponent child = (UIComponent) clayContext.getChild();
74 if (child == null) {
75 throw new NullPointerException(getMessages()
76 .getMessage("clay.null.childComponent"));
77 }
78
79 ComponentBean displayElement = clayContext.getDisplayElement();
80 if (displayElement == null) {
81 throw new NullPointerException(getMessages()
82 .getMessage("clay.null.componentBean"));
83 }
84
85 EditableValueHolder parent = (EditableValueHolder) clayContext
86 .getParent();
87 if (parent == null) {
88 throw new NullPointerException(getMessages()
89 .getMessage("clay.null.parentComponent"));
90 }
91
92 FacesContext facesContext = clayContext.getFacesContext();
93 if (facesContext == null) {
94 throw new NullPointerException(getMessages()
95 .getMessage("clay.null.facesContext"));
96 }
97
98 Validator validator = null;
99 try {
100 AttributeBean attr = displayElement.getAttribute("binding");
101 if (attr != null && isValueReference(attr.getValue())) {
102 clayContext.setAttribute(attr);
103 String expr = replaceMnemonic(clayContext);
104 ValueBinding vb = facesContext.getApplication()
105 .createValueBinding(expr);
106 validator = (Validator) vb.getValue(facesContext);
107
108 } else {
109 validator = facesContext.getApplication().createValidator(
110 displayElement.getComponentType());
111 }
112 } catch (Exception e) {
113 log.error(getMessages().getMessage("create.validator.error",
114 new Object[] { displayElement }), e);
115 throw e;
116 }
117 parent.addValidator(validator);
118
119
120 clayContext.setChild(validator);
121
122 return isFinal;
123 }
124
125 }