2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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          // create a new scoped symbol table
75          Map symbolTable = new Attributes();
76          // inherit the parents symbols
77          symbolTable.putAll(clayContext.getSymbols());
78          // override config (XML, HTML) symbols
79          symbolTable.putAll(displayElement.getSymbols());
80          // push to context
81          clayContext.setSymbols(symbolTable);
82  
83          // evaluate nested symbols; symbols having symbols as values
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         // Two new properties on the view root in JSF 1.2 beforePhaseListener and afterPhaseListener.
130         // These two require MethodExpression that is introduced in JSP 2.1.  We will have to wait
131         // until we can migrate to 1.2 before we can support these attributes.  For some reason,
132         // these are not wrappered like the ValueBinding is wrapperd by the ValueExpression.
133 
134         // assign any children of the display element to the parent
135         // we return "true" indicating the component already exists
136         // and the "createComponent" chain will stop and return back to the
137         // "addComponent" chain
138         clayContext.setChild(clayContext.getParent());
139 
140         return true;
141     }
142 
143 }