Source code: com/sun/facelets/tag/jsf/ComponentRule.java
1 /**
2 * Licensed under the Common Development and Distribution License,
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.sun.com/cddl/
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11 * implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15 package com.sun.facelets.tag.jsf;
16
17 import java.util.logging.Level;
18 import java.util.logging.Logger;
19
20 import javax.faces.component.UIComponent;
21 import javax.faces.component.UIComponentBase;
22
23 import com.sun.facelets.FaceletContext;
24 import com.sun.facelets.el.LegacyValueBinding;
25 import com.sun.facelets.tag.TagAttribute;
26 import com.sun.facelets.tag.Metadata;
27 import com.sun.facelets.tag.MetaRule;
28 import com.sun.facelets.tag.MetadataTarget;
29 import com.sun.facelets.util.FacesAPI;
30
31 /**
32 *
33 * @author Jacob Hookom
34 * @version $Id: ComponentRule.java,v 1.2 2005/08/24 04:38:50 jhook Exp $
35 */
36 final class ComponentRule extends MetaRule {
37
38 final class LiteralAttributeMetadata extends Metadata {
39
40 private final String name;
41 private final String value;
42
43 public LiteralAttributeMetadata(String name, String value) {
44 this.value = value;
45 this.name = name;
46 }
47
48 public void applyMetadata(FaceletContext ctx, Object instance) {
49 ((UIComponent) instance).getAttributes().put(this.name, this.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,
62 TagAttribute attr) {
63 this.name = name;
64 this.attr = attr;
65 this.type = type;
66 }
67
68 public void applyMetadata(FaceletContext ctx, Object instance) {
69 ((UIComponent) instance).setValueExpression(this.name, this.attr
70 .getValueExpression(ctx, this.type));
71 }
72
73 }
74
75 final static class ValueBindingMetadata extends Metadata {
76
77 private final String name;
78
79 private final TagAttribute attr;
80
81 private final Class type;
82
83 public ValueBindingMetadata(String name, Class type, TagAttribute attr) {
84 this.name = name;
85 this.attr = attr;
86 this.type = type;
87 }
88
89 public void applyMetadata(FaceletContext ctx, Object instance) {
90 ((UIComponent) instance).setValueBinding(this.name,
91 new LegacyValueBinding(this.attr.getValueExpression(ctx,
92 this.type)));
93 }
94
95 }
96
97 private final static Logger log = Logger
98 .getLogger("facelets.tag.component");
99
100 public final static ComponentRule Instance = new ComponentRule();
101
102 public ComponentRule() {
103 super();
104 }
105
106 public Metadata applyRule(String name, TagAttribute attribute,
107 MetadataTarget meta) {
108 if (meta.isTargetInstanceOf(UIComponent.class)) {
109
110 // if component and dynamic, then must set expression
111 if (!attribute.isLiteral()) {
112 Class type = meta.getPropertyType(name);
113 if (type == null) {
114 type = Object.class;
115 }
116 if (FacesAPI.getComponentVersion(meta.getTargetClass()) >= 12) {
117 return new ValueExpressionMetadata(name, type, attribute);
118 } else {
119 return new ValueBindingMetadata(name, type, attribute);
120 }
121 } else if (meta.getWriteMethod(name) == null) {
122
123 // this was an attribute literal, but not property
124 warnAttr(attribute, meta.getTargetClass(), name);
125
126 return new LiteralAttributeMetadata(name, attribute.getValue());
127 }
128 }
129 return null;
130 }
131
132 private static void warnAttr(TagAttribute attr, Class type, String n) {
133 if (log.isLoggable(Level.WARNING)) {
134 log.warning(attr + " Property '" + n + "' is not on type: "
135 + type.getName());
136 }
137 }
138
139 }