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 java.util.logging.Level;
22  import java.util.logging.Logger;
23  
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   * 
33   * @author Jacob Hookom
34   * @version $Id$
35   */
36  final class ComponentRule extends MetaRule
37  {
38  
39      final class LiteralAttributeMetadata extends Metadata
40      {
41          private final String _name;
42          private final String _value;
43  
44          public LiteralAttributeMetadata(String name, String value)
45          {
46              _name = name;
47              _value = value;
48          }
49  
50          public void applyMetadata(FaceletContext ctx, Object instance)
51          {
52              ((UIComponent) instance).getAttributes().put(_name, _value);
53          }
54      }
55  
56      final static class ValueExpressionMetadata extends Metadata
57      {
58          private final String _name;
59  
60          private final TagAttribute _attr;
61  
62          private final Class<?> _type;
63  
64          public ValueExpressionMetadata(String name, Class<?> type, TagAttribute attr)
65          {
66              _name = name;
67              _attr = attr;
68              _type = type;
69          }
70  
71          public void applyMetadata(FaceletContext ctx, Object instance)
72          {
73              ((UIComponent) instance).setValueExpression(_name, _attr.getValueExpression(ctx, _type));
74          }
75      }
76  
77      //private final static Logger log = Logger.getLogger("facelets.tag.component");
78      private final static Logger log = Logger.getLogger(ComponentRule.class.getName());
79  
80      public final static ComponentRule INSTANCE = new ComponentRule();
81  
82      public ComponentRule()
83      {
84          super();
85      }
86  
87      public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta)
88      {
89          if (meta.isTargetInstanceOf(UIComponent.class))
90          {
91              // if component and dynamic, then must set expression
92              if (!attribute.isLiteral())
93              {
94                  Class<?> type = meta.getPropertyType(name);
95                  if (type == null)
96                  {
97                      type = Object.class;
98                  }
99                  
100                 return new ValueExpressionMetadata(name, type, attribute);
101             }
102             else if (meta.getWriteMethod(name) == null)
103             {
104 
105                 // this was an attribute literal, but not property
106                 warnAttr(attribute, meta.getTargetClass(), name);
107 
108                 return new LiteralAttributeMetadata(name, attribute.getValue());
109             }
110         }
111         return null;
112     }
113 
114     private static void warnAttr(TagAttribute attr, Class<?> type, String n)
115     {
116         if (log.isLoggable(Level.FINER))
117         {
118             log.finer(attr + " Property '" + n + "' is not on type: " + type.getName());
119         }
120     }
121 
122 }