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.composite;
20  
21  import java.beans.PropertyDescriptor;
22  
23  import javax.el.ValueExpression;
24  import javax.faces.component.UIComponent;
25  import javax.faces.view.facelets.FaceletContext;
26  import javax.faces.view.facelets.MetaRule;
27  import javax.faces.view.facelets.Metadata;
28  import javax.faces.view.facelets.MetadataTarget;
29  import javax.faces.view.facelets.TagAttribute;
30  
31  /**
32   * Rule used to wire ValueExpressions retrieved by ViewDeclarationLanguage.retargetMethodExpressions 
33   * 
34   * @author Leonardo Uribe (latest modification by $Author$)
35   * @version $Revision$ $Date$
36   */
37  final class RetargetMethodExpressionRule extends MetaRule
38  {
39      final static class RetargetValueExpressionMapper extends Metadata
40      {
41          private final TagAttribute _attr;
42          
43          private final String _name;
44  
45          public RetargetValueExpressionMapper(TagAttribute attr, String name)
46          {
47              this._attr = attr;
48              this._name = name;
49          }
50  
51          public void applyMetadata(FaceletContext ctx, Object instance)
52          {
53              ValueExpression expr = _attr.getValueExpression(ctx, Object.class);
54              ((UIComponent) instance).getAttributes().put(_name, expr);
55          }
56      }
57  
58      public final static RetargetMethodExpressionRule INSTANCE = new RetargetMethodExpressionRule();
59  
60      public RetargetMethodExpressionRule()
61      {
62          super();
63      }
64  
65      public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta)
66      {
67          // ViewDeclarationLanguage.retargetMethodExpressions only works when a method-signature 
68          // is defined, so we just have to apply this rule on that case. 
69          if ("action".equals(name) || 
70              "actionListener".equals(name) ||
71              "validator".equals(name) ||
72              "valueChangeListener".equals(name))
73          {
74              return new RetargetValueExpressionMapper(attribute, name);
75          }
76          else
77          {
78              PropertyDescriptor propertyDescriptor = meta.getProperty(name);
79              //Type takes precedence over method-signature
80              if (propertyDescriptor != null && 
81                      propertyDescriptor.getValue("type") == null)
82              {
83                  ValueExpression methodSignatureExpression = 
84                      (ValueExpression) propertyDescriptor.getValue("method-signature");
85                  
86                  if (methodSignatureExpression != null)
87                  {
88                      return new RetargetValueExpressionMapper(attribute, name);
89                  }
90                  
91                  ValueExpression targetAttributeNameVE = 
92                      (ValueExpression)propertyDescriptor.getValue("targetAttributeName");
93                  if (targetAttributeNameVE != null)
94                  {
95                      return new RetargetValueExpressionMapper(attribute, name);
96                  }
97                  
98                  //if there is a targets declaration, is a retarget without doubt
99                  ValueExpression targets = 
100                     (ValueExpression)propertyDescriptor.getValue("targets");
101                 
102                 if (targets != null)
103                 {
104                     return new RetargetValueExpressionMapper(attribute, name);
105                 }
106             }
107         }
108         return null;
109     }
110 }