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 | 0 | public final class BeanPropertyTagRule extends MetaRule |
37 | |
{ |
38 | 0 | public final static BeanPropertyTagRule INSTANCE = new BeanPropertyTagRule(); |
39 | |
|
40 | |
public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta) |
41 | |
{ |
42 | 0 | Method m = meta.getWriteMethod(name); |
43 | |
|
44 | |
|
45 | 0 | if (m != null) |
46 | |
{ |
47 | 0 | if (attribute.isLiteral()) |
48 | |
{ |
49 | 0 | return new LiteralPropertyMetadata(m, attribute); |
50 | |
} |
51 | |
else |
52 | |
{ |
53 | 0 | return new DynamicPropertyMetadata(m, attribute); |
54 | |
} |
55 | |
} |
56 | |
|
57 | 0 | 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 | 0 | { |
71 | 0 | this.method = method; |
72 | 0 | this.attribute = attribute; |
73 | 0 | } |
74 | |
|
75 | |
public void applyMetadata(FaceletContext ctx, Object instance) |
76 | |
{ |
77 | 0 | if (value == null) |
78 | |
{ |
79 | 0 | String str = this.attribute.getValue(); |
80 | 0 | value = new Object[] { ctx.getExpressionFactory().coerceToType(str, method.getParameterTypes()[0]) }; |
81 | |
} |
82 | |
try |
83 | |
{ |
84 | 0 | method.invoke(instance, this.value); |
85 | |
} |
86 | 0 | catch (InvocationTargetException e) |
87 | |
{ |
88 | 0 | throw new TagAttributeException(this.attribute, e.getCause()); |
89 | |
} |
90 | 0 | catch (Exception e) |
91 | |
{ |
92 | 0 | throw new TagAttributeException(this.attribute, e); |
93 | 0 | } |
94 | 0 | } |
95 | |
|
96 | |
} |
97 | |
|
98 | 0 | 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 | 0 | { |
109 | 0 | this.method = method; |
110 | 0 | this.type = method.getParameterTypes()[0]; |
111 | 0 | this.attribute = attribute; |
112 | 0 | } |
113 | |
|
114 | |
public void applyMetadata(FaceletContext ctx, Object instance) |
115 | |
{ |
116 | |
try |
117 | |
{ |
118 | 0 | method.invoke(instance, new Object[] { attribute.getObject(ctx, type) }); |
119 | |
} |
120 | 0 | catch (InvocationTargetException e) |
121 | |
{ |
122 | 0 | throw new TagAttributeException(attribute, e.getCause()); |
123 | |
} |
124 | 0 | catch (Exception e) |
125 | |
{ |
126 | 0 | throw new TagAttributeException(attribute, e); |
127 | 0 | } |
128 | 0 | } |
129 | |
} |
130 | |
|
131 | |
} |