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.taglib.core;
20  
21  import javax.servlet.jsp.tagext.TagSupport;
22  import javax.servlet.jsp.JspException;
23  import javax.faces.webapp.UIComponentClassicTagBase;
24  import javax.faces.webapp.UIComponentELTag;
25  import javax.faces.component.UIComponent;
26  import javax.faces.context.FacesContext;
27  import javax.el.ValueExpression;
28  import javax.el.ELContext;
29  
30  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspAttribute;
31  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspTag;
32  
33  /**
34   * This tag associates an attribute with the nearest parent
35   * UIComponent.
36   * <p>
37   * When the value is not an EL expression, this tag has the same effect
38   * as calling component.getAttributes.put(name, value). When the attribute
39   * name specified matches a standard property of the component, that
40   * property is set. However it is also valid to assign attributes
41   * to components using any arbitrary name; the component itself won't
42   * make any use of these but other objects such as custom renderers,
43   * validators or action listeners can later retrieve the attribute
44   * from the component by name.
45   * </p>
46   * <p>
47   * When the value is an EL expression, this tag has the same effect
48   * as calling component.setValueBinding. A call to method
49   * component.getAttributes().get(name) will then cause that
50   * expression to be evaluated and the result of the expression is
51   * returned, not the original EL expression string.
52   * </p>
53   * <p>
54   * See the javadoc for UIComponent.getAttributes for more details.
55   * </p>
56   * <p>
57   * Unless otherwise specified, all attributes accept static values
58   * or EL expressions.
59   * </p>
60   * 
61   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
62   * @author Bruno Aranda (JSR-252)
63   * @version $Revision: 693358 $ $Date: 2008-09-08 22:54:29 -0500 (Mon, 08 Sep 2008) $
64   */
65  @JSFJspTag(
66          name="f:attribute",
67          bodyContent="empty")
68  public class AttributeTag
69          extends TagSupport
70  {
71      private static final long serialVersionUID = 31476300171678632L;
72      private ValueExpression _nameExpression;
73      private ValueExpression _valueExpression;
74  
75      /**
76       * The name of the attribute.
77       * 
78       * @param nameExpression
79       */
80      @JSFJspAttribute(
81              rtexprvalue=true,
82              className="java.lang.String")
83      public void setName(ValueExpression nameExpression)
84      {
85          _nameExpression = nameExpression;
86      }
87  
88      /**
89       * The attribute's value.
90       * 
91       * @param valueExpression
92       */
93      @JSFJspAttribute(
94              rtexprvalue=true,
95              className="java.lang.Object")
96      public void setValue(ValueExpression valueExpression)
97      {
98          _valueExpression = valueExpression;
99      }
100 
101 
102     public int doStartTag() throws JspException
103     {
104         UIComponentClassicTagBase componentTag = UIComponentELTag.getParentUIComponentClassicTagBase(pageContext);
105         if (componentTag == null)
106         {
107             throw new JspException("no parent UIComponentTag found");
108         }
109         UIComponent component = componentTag.getComponentInstance();
110         if (component == null)
111         {
112             throw new JspException("parent UIComponentTag has no UIComponent");
113         }
114 
115         FacesContext facesContext = FacesContext.getCurrentInstance();
116         ELContext elContext = facesContext.getELContext();
117 
118         String name = null;
119         Object value = null;
120         boolean isLiteral = false;
121 
122         if (_nameExpression != null)
123         {
124              name = (String) _nameExpression.getValue(elContext);
125         }
126 
127         if (_valueExpression != null)
128         {
129             isLiteral = _valueExpression.isLiteralText();
130             value = _valueExpression.getValue(elContext);
131         }
132 
133         if (name != null)
134         {
135             if (component.getAttributes().get(name) == null)
136             {
137                 if (isLiteral)
138                 {
139                    component.getAttributes().put(name, value);
140                 }
141                 else
142                 {
143                     component.setValueExpression(name, _valueExpression);
144                 }
145             }
146         }
147 
148         return SKIP_BODY;
149     }
150 
151     /**
152      * @deprecated
153      */
154     public void release()
155     {
156         super.release();
157         _nameExpression = null;
158         _valueExpression = null;
159     }
160 
161 }