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  import org.apache.myfaces.view.facelets.el.FaceletStateValueExpression;
38  import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
39  import org.apache.myfaces.view.facelets.tag.jsf.FaceletState;
40  
41  /**
42   * @author Jacob Hookom
43   * @version $Id$
44   */
45  @JSFFaceletTag(name="ui:param")
46  public class ParamHandler extends TagHandler
47  {
48  
49      /**
50       * The name of the variable to pass to the included Facelet.
51       */
52      @JSFFaceletAttribute(
53              className="javax.el.ValueExpression",
54              deferredValueType="java.lang.String",
55              required=true)
56      private final TagAttribute name;
57  
58      /**
59       * The literal or EL expression value to assign to the named variable.
60       */
61      @JSFFaceletAttribute(
62              className="javax.el.ValueExpression",
63              deferredValueType="java.lang.String",
64              required=true)
65      private final TagAttribute value;
66  
67      /**
68       * @param config
69       */
70      public ParamHandler(TagConfig config)
71      {
72          super(config);
73          this.name = this.getRequiredAttribute("name");
74          this.value = this.getRequiredAttribute("value");
75      }
76  
77      /*
78       * (non-Javadoc)
79       * 
80       * @see javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext,
81       *        javax.faces.component.UIComponent)
82       */
83      @Override
84      public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
85              ELException
86      {
87          String nameStr = getName(ctx);
88          ValueExpression valueVE = getValue(ctx);
89          //ctx.getVariableMapper().setVariable(nameStr, valueVE);
90          apply(ctx, parent, nameStr, valueVE);
91      }
92      
93      public void apply(FaceletContext ctx, UIComponent parent, String nameStr, ValueExpression valueVE)
94              throws IOException, FacesException, FaceletException, ELException
95      {
96          AbstractFaceletContext actx = ((AbstractFaceletContext) ctx);
97          actx.getTemplateContext().setParameter(nameStr, valueVE);
98          
99          if (actx.getTemplateContext().isAllowCacheELExpressions())
100         {
101             if (ELExpressionCacheMode.strict.equals(actx.getELExpressionCacheMode()) ||
102                 ELExpressionCacheMode.allowCset.equals(actx.getELExpressionCacheMode()))
103             {
104                 actx.getTemplateContext().setAllowCacheELExpressions(false);
105             }
106         }
107     }
108     
109     public void apply(FaceletContext ctx, UIComponent parent, String nameStr, ValueExpression valueVE,
110         String uniqueId)
111             throws IOException, FacesException, FaceletException, ELException
112     {
113         AbstractFaceletContext actx = ((AbstractFaceletContext) ctx);
114         if (ELExpressionCacheMode.alwaysRecompile.equals(actx.getELExpressionCacheMode()))
115         {
116             FaceletState faceletState = ComponentSupport.getFaceletState(ctx, parent, true);
117             faceletState.putBinding(uniqueId, nameStr, valueVE);
118             ValueExpression ve = new FaceletStateValueExpression(uniqueId, nameStr);
119             actx.getTemplateContext().setParameter(nameStr, ve);
120         }
121         else
122         {
123             actx.getTemplateContext().setParameter(nameStr, valueVE);
124         }
125         
126         if (actx.getTemplateContext().isAllowCacheELExpressions())
127         {
128             if (ELExpressionCacheMode.strict.equals(actx.getELExpressionCacheMode()) ||
129                 ELExpressionCacheMode.allowCset.equals(actx.getELExpressionCacheMode()))
130             {
131                 actx.getTemplateContext().setAllowCacheELExpressions(false);
132             }
133         }
134     }
135     
136     public String getName(FaceletContext ctx)
137     {
138         return this.name.getValue(ctx);
139     }
140     
141     public ValueExpression getValue(FaceletContext ctx)
142     {
143         return this.value.getValueExpression(ctx, Object.class);
144     }
145 }