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