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 package org.apache.shale.clay.component.chain;
19
20 import java.util.Locale;
21 import java.util.Map;
22 import java.util.StringTokenizer;
23
24 import javax.faces.context.FacesContext;
25
26 import org.apache.commons.chain.Context;
27 import org.apache.shale.clay.config.beans.AttributeBean;
28 import org.apache.shale.clay.config.beans.Attributes;
29 import org.apache.shale.clay.config.beans.ComponentBean;
30
31 /***
32 * <p>This Command assigns properties to an existing UIViewRoot.
33 * It can only update the renderKit and locale properties.
34 * There are two new properties on the view root in JSF 1.2,
35 * beforePhaseListener and afterPhaseListener. These two require
36 * MethodExpression that is introduced in JSP 2.1. We will have to wait
37 * until we can migrate to 1.2 before we can support these attributes. For some reason,
38 * these are not wrappered like the ValueBinding is wrapperd by the ValueExpression.
39 * </p>
40 */
41 public class AssignViewRootCommand extends AbstractCommand {
42
43 /***
44 * <p>If the target <code>componentType</code> is "javax.faces.ViewRoot",
45 * assign the property overrides is present.</p>
46 * @param context commons chains context
47 * @return <code>true</code> if the current display element is for the view root;
48 * Otherwise, return <code>false</code> to create/update a component.
49 * @exception Exception any error that might terminate processing
50 */
51 public boolean execute(Context context) throws Exception {
52 ClayContext clayContext = (ClayContext) context;
53 if (clayContext == null) {
54 throw new NullPointerException(getMessages()
55 .getMessage("clay.null.clayContext"));
56 }
57
58 ComponentBean displayElement = clayContext.getDisplayElement();
59 if (displayElement == null) {
60 throw new NullPointerException(getMessages()
61 .getMessage("clay.null.componentBean"));
62 }
63
64 if (!displayElement.getComponentType().equals("javax.faces.ViewRoot")) {
65 return false;
66 }
67
68 FacesContext facesContext = clayContext.getFacesContext();
69 if (facesContext == null) {
70 throw new NullPointerException(getMessages()
71 .getMessage("clay.null.facesContext"));
72 }
73
74
75 Map symbolTable = new Attributes();
76
77 symbolTable.putAll(clayContext.getSymbols());
78
79 symbolTable.putAll(displayElement.getSymbols());
80
81 clayContext.setSymbols(symbolTable);
82
83
84 realizeSymbols(clayContext);
85
86 AttributeBean attr = null;
87 attr = displayElement.getAttribute("renderKitId");
88 if (attr != null && attr.getValue() != null) {
89 clayContext.setAttribute(attr);
90 String expr = replaceMnemonic(clayContext);
91 if (expr != null) {
92 if (isValueReference(expr)) {
93 getTagUtils().setValueBinding(facesContext.getViewRoot(), "renderKitId", expr);
94 } else {
95 facesContext.getViewRoot().setRenderKitId(expr);
96 }
97 }
98 clayContext.setAttribute(null);
99 }
100 attr = displayElement.getAttribute("locale");
101 if (attr != null && attr.getValue() != null) {
102 clayContext.setAttribute(attr);
103 String expr = replaceMnemonic(clayContext);
104 if (expr != null) {
105 if (isValueReference(expr)) {
106 getTagUtils().setValueBinding(facesContext.getViewRoot(), "locale", expr);
107 } else {
108 final int language = 0;
109 final int country = 1;
110 StringTokenizer tokenizer = new StringTokenizer(expr, "-_");
111 String[] tokens = new String[2];
112 int i = 0;
113 while (tokenizer.hasMoreTokens()) {
114 tokens[i++] = tokenizer.nextToken();
115 }
116 Locale locale = null;
117 if (tokens[language] != null && tokens[country] != null) {
118 locale = new Locale(tokens[language], tokens[country]);
119 } else if (tokens[language] != null) {
120 locale = new Locale(tokens[language]);
121 }
122 if (locale != null) {
123 facesContext.getViewRoot().setLocale(locale);
124 }
125 }
126 }
127 clayContext.setAttribute(null);
128 }
129
130
131
132
133
134
135
136
137
138 clayContext.setChild(clayContext.getParent());
139
140 return true;
141 }
142
143 }