KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > BeanPropertyTagRule


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;
16
17 import java.lang.reflect.InvocationTargetException JavaDoc;
18 import java.lang.reflect.Method JavaDoc;
19
20 import com.sun.facelets.FaceletContext;
21
22 /**
23  *
24  * @author Jacob Hookom
25  * @version $Id: BeanPropertyTagRule.java,v 1.2 2005/08/24 04:38:46 jhook Exp $
26  */

27 final class BeanPropertyTagRule extends MetaRule {
28     
29     final static class LiteralPropertyMetadata extends Metadata {
30         
31         private final Method JavaDoc method;
32
33         private final TagAttribute attribute;
34
35         private Object JavaDoc[] value;
36
37         public LiteralPropertyMetadata(Method JavaDoc method, TagAttribute attribute) {
38             this.method = method;
39             this.attribute = attribute;
40         }
41
42         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
43             if (value == null) {
44                 String JavaDoc str = this.attribute.getValue();
45                 value = new Object JavaDoc[] { ctx.getExpressionFactory().coerceToType(str,
46                         method.getParameterTypes()[0]) };
47             }
48             try {
49                 method.invoke(instance, this.value);
50             } catch (InvocationTargetException JavaDoc e) {
51                 throw new TagAttributeException(this.attribute, e.getCause());
52             } catch (Exception JavaDoc e) {
53                 throw new TagAttributeException(this.attribute, e);
54             }
55         }
56
57     }
58     
59     final static class DynamicPropertyMetadata extends Metadata {
60
61         private final Method JavaDoc method;
62
63         private final TagAttribute attribute;
64
65         private final Class JavaDoc type;
66
67         public DynamicPropertyMetadata(Method JavaDoc method, TagAttribute attribute) {
68             this.method = method;
69             this.type = method.getParameterTypes()[0];
70             this.attribute = attribute;
71         }
72
73         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
74             try {
75                 this.method.invoke(instance, new Object JavaDoc[] { this.attribute
76                         .getObject(ctx, this.type) });
77             } catch (InvocationTargetException JavaDoc e) {
78                 throw new TagAttributeException(this.attribute, e.getCause());
79             } catch (Exception JavaDoc e) {
80                 throw new TagAttributeException(this.attribute, e);
81             }
82         }
83     }
84     
85     public final static BeanPropertyTagRule Instance = new BeanPropertyTagRule();
86
87     public Metadata applyRule(String JavaDoc name, TagAttribute attribute,
88             MetadataTarget meta) {
89         Method JavaDoc m = meta.getWriteMethod(name);
90
91         // if the property is writable
92
if (m != null) {
93             if (attribute.isLiteral()) {
94                 return new LiteralPropertyMetadata(m, attribute);
95             } else {
96                 return new DynamicPropertyMetadata(m, attribute);
97             }
98         }
99
100         return null;
101     }
102
103 }
104
Popular Tags