View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.view.facelets.tag.ui;
20  
21  import java.io.IOException;
22  
23  import javax.el.ELException;
24  import javax.el.ValueExpression;
25  import javax.faces.FacesException;
26  import javax.faces.component.UIComponent;
27  import javax.faces.view.facelets.FaceletContext;
28  import javax.faces.view.facelets.FaceletException;
29  import javax.faces.view.facelets.TagAttribute;
30  import javax.faces.view.facelets.TagConfig;
31  import javax.faces.view.facelets.TagHandler;
32  
33  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute;
34  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
35  import org.apache.myfaces.view.facelets.AbstractFaceletContext;
36  import org.apache.myfaces.view.facelets.ELExpressionCacheMode;
37  
38  /**
39   * @author Jacob Hookom
40   * @version $Id: ParamHandler.java 1187700 2011-10-22 12:19:37Z bommel $
41   */
42  @JSFFaceletTag(name="ui:param")
43  public class ParamHandler extends TagHandler
44  {
45  
46      /**
47       * The name of the variable to pass to the included Facelet.
48       */
49      @JSFFaceletAttribute(
50              className="javax.el.ValueExpression",
51              deferredValueType="java.lang.String",
52              required=true)
53      private final TagAttribute name;
54  
55      /**
56       * The literal or EL expression value to assign to the named variable.
57       */
58      @JSFFaceletAttribute(
59              className="javax.el.ValueExpression",
60              deferredValueType="java.lang.String",
61              required=true)
62      private final TagAttribute value;
63  
64      /**
65       * @param config
66       */
67      public ParamHandler(TagConfig config)
68      {
69          super(config);
70          this.name = this.getRequiredAttribute("name");
71          this.value = this.getRequiredAttribute("value");
72      }
73  
74      /*
75       * (non-Javadoc)
76       * 
77       * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext, javax.faces.component.UIComponent)
78       */
79      public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
80              ELException
81      {
82          String nameStr = getName(ctx);
83          ValueExpression valueVE = getValue(ctx);
84          //ctx.getVariableMapper().setVariable(nameStr, valueVE);
85          apply(ctx, parent, nameStr, valueVE);
86      }
87      
88      public void apply(FaceletContext ctx, UIComponent parent, String nameStr, ValueExpression valueVE) throws IOException, FacesException, FaceletException,
89              ELException
90      {
91          //((AbstractFaceletContext) ctx).getTemplateContext().getAttributes().put(nameStr, valueVE);
92          AbstractFaceletContext actx = ((AbstractFaceletContext) ctx); 
93          actx.getTemplateContext().setParameter(nameStr, valueVE);
94          
95          if (actx.getTemplateContext().isAllowCacheELExpressions())
96          {
97              if (ELExpressionCacheMode.strict.equals(actx.getELExpressionCacheMode()) ||
98                  ELExpressionCacheMode.allowCset.equals(actx.getELExpressionCacheMode()))
99              {
100                 actx.getTemplateContext().setAllowCacheELExpressions(false);
101             }
102         }
103     }
104     
105     public String getName(FaceletContext ctx)
106     {
107         return this.name.getValue(ctx);
108     }
109     
110     public ValueExpression getValue(FaceletContext ctx)
111     {
112         return this.value.getValueExpression(ctx, Object.class);
113     }
114 }