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.jsf;
20  
21  import javax.faces.component.UIComponent;
22  import javax.faces.view.facelets.FaceletContext;
23  import javax.faces.view.facelets.MetaRule;
24  import javax.faces.view.facelets.Metadata;
25  import javax.faces.view.facelets.MetadataTarget;
26  import javax.faces.view.facelets.TagAttribute;
27  import org.apache.myfaces.view.facelets.PassthroughRule;
28  
29  /**
30   *
31   * @author Leonardo Uribe
32   */
33  final class PassthroughRuleImpl extends MetaRule implements PassthroughRule
34  {
35  
36      final static class LiteralAttributeMetadata extends Metadata
37      {
38          private final String _name;
39          private final String _value;
40  
41          public LiteralAttributeMetadata(String name, String value)
42          {
43              _name = name;
44              _value = value;
45          }
46  
47          public void applyMetadata(FaceletContext ctx, Object instance)
48          {
49              ((UIComponent) instance).getPassThroughAttributes().put(_name, _value);
50          }
51      }
52  
53      final static class ValueExpressionMetadata extends Metadata
54      {
55          private final String _name;
56  
57          private final TagAttribute _attr;
58  
59          private final Class<?> _type;
60  
61          public ValueExpressionMetadata(String name, Class<?> type, TagAttribute attr)
62          {
63              _name = name;
64              _attr = attr;
65              _type = type;
66          }
67  
68          public void applyMetadata(FaceletContext ctx, Object instance)
69          {
70              ((UIComponent) instance).getPassThroughAttributes().put(
71                  _name, _attr.getValueExpression(ctx, _type));
72          }
73      }
74  
75      public final static PassthroughRuleImpl INSTANCE = new PassthroughRuleImpl();
76  
77      public PassthroughRuleImpl()
78      {
79          super();
80      }
81  
82      public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta)
83      {
84          if (meta.isTargetInstanceOf(UIComponent.class))
85          {
86              // if component and dynamic, then must set expression
87              if (!attribute.isLiteral())
88              {
89                  Class<?> type = meta.getPropertyType(name);
90                  if (type == null)
91                  {
92                      type = Object.class;
93                  }
94                  // if "class" is used, set type as Object (do not confuse with getClass()). 
95                  if ("class".equalsIgnoreCase(name))
96                  {
97                      type = Object.class;
98                  }
99                  
100                 return new ValueExpressionMetadata(name, type, attribute);
101             }
102             else
103             {
104                 return new LiteralAttributeMetadata(name, attribute.getValue());
105             }
106         }
107         return null;
108     }
109 }