1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.tag;
20
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23
24 import javax.faces.view.facelets.FaceletContext;
25 import javax.faces.view.facelets.MetaRule;
26 import javax.faces.view.facelets.Metadata;
27 import javax.faces.view.facelets.MetadataTarget;
28 import javax.faces.view.facelets.TagAttribute;
29 import javax.faces.view.facelets.TagAttributeException;
30
31
32
33
34
35
36 public final class BeanPropertyTagRule extends MetaRule
37 {
38 public final static BeanPropertyTagRule INSTANCE = new BeanPropertyTagRule();
39
40 public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta)
41 {
42 Method m = meta.getWriteMethod(name);
43
44
45 if (m != null)
46 {
47 if (attribute.isLiteral())
48 {
49 return new LiteralPropertyMetadata(m, attribute);
50 }
51 else
52 {
53 return new DynamicPropertyMetadata(m, attribute);
54 }
55 }
56
57 return null;
58 }
59
60 final static class LiteralPropertyMetadata extends Metadata
61 {
62
63 private final Method method;
64
65 private final TagAttribute attribute;
66
67 private Object[] value;
68
69 public LiteralPropertyMetadata(Method method, TagAttribute attribute)
70 {
71 this.method = method;
72 this.attribute = attribute;
73 }
74
75 public void applyMetadata(FaceletContext ctx, Object instance)
76 {
77 if (value == null)
78 {
79 String str = this.attribute.getValue();
80 value = new Object[] { ctx.getExpressionFactory().coerceToType(str, method.getParameterTypes()[0]) };
81 }
82 try
83 {
84 method.invoke(instance, this.value);
85 }
86 catch (InvocationTargetException e)
87 {
88 throw new TagAttributeException(this.attribute, e.getCause());
89 }
90 catch (Exception e)
91 {
92 throw new TagAttributeException(this.attribute, e);
93 }
94 }
95
96 }
97
98 final static class DynamicPropertyMetadata extends Metadata
99 {
100
101 private final Method method;
102
103 private final TagAttribute attribute;
104
105 private final Class<?> type;
106
107 public DynamicPropertyMetadata(Method method, TagAttribute attribute)
108 {
109 this.method = method;
110 this.type = method.getParameterTypes()[0];
111 this.attribute = attribute;
112 }
113
114 public void applyMetadata(FaceletContext ctx, Object instance)
115 {
116 try
117 {
118 method.invoke(instance, new Object[] { attribute.getObject(ctx, type) });
119 }
120 catch (InvocationTargetException e)
121 {
122 throw new TagAttributeException(attribute, e.getCause());
123 }
124 catch (Exception e)
125 {
126 throw new TagAttributeException(attribute, e);
127 }
128 }
129 }
130
131 }