1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
package org.apache.tiles.el; |
22 | |
|
23 | |
import java.beans.FeatureDescriptor; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
|
29 | |
import javax.el.ELContext; |
30 | |
import javax.el.ELResolver; |
31 | |
|
32 | |
import org.apache.tiles.request.Request; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 12 | public class TilesContextBeanELResolver extends ELResolver { |
41 | |
|
42 | |
|
43 | |
@Override |
44 | |
public Class<?> getCommonPropertyType(ELContext context, Object base) { |
45 | |
|
46 | 2 | if (base != null) { |
47 | 1 | return null; |
48 | |
} |
49 | |
|
50 | 1 | return String.class; |
51 | |
} |
52 | |
|
53 | |
|
54 | |
@Override |
55 | |
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, |
56 | |
Object base) { |
57 | 1 | List<FeatureDescriptor> list = new ArrayList<FeatureDescriptor>(); |
58 | |
|
59 | 1 | Request request = (Request) context |
60 | |
.getContext(Request.class); |
61 | 1 | for (String scope : request.getAvailableScopes()) { |
62 | 3 | collectBeanInfo(request.getContext(scope), list); |
63 | 3 | } |
64 | 1 | return list.iterator(); |
65 | |
} |
66 | |
|
67 | |
|
68 | |
@Override |
69 | |
public Class<?> getType(ELContext context, Object base, Object property) { |
70 | 5 | if (base != null) { |
71 | 1 | return null; |
72 | |
} |
73 | |
|
74 | 4 | Object obj = findObjectByProperty(context, property); |
75 | 4 | if (obj != null) { |
76 | 3 | context.setPropertyResolved(true); |
77 | 3 | return obj.getClass(); |
78 | |
} |
79 | 1 | return null; |
80 | |
} |
81 | |
|
82 | |
|
83 | |
@Override |
84 | |
public Object getValue(ELContext context, Object base, Object property) { |
85 | 20 | if (base != null) { |
86 | 9 | return null; |
87 | |
} |
88 | |
|
89 | 11 | Object retValue = findObjectByProperty(context, property); |
90 | |
|
91 | 11 | if (retValue != null) { |
92 | 11 | context.setPropertyResolved(true); |
93 | |
} |
94 | |
|
95 | 11 | return retValue; |
96 | |
} |
97 | |
|
98 | |
|
99 | |
@Override |
100 | |
public boolean isReadOnly(ELContext context, Object base, Object property) { |
101 | 2 | if (context == null) { |
102 | 1 | throw new NullPointerException(); |
103 | |
} |
104 | |
|
105 | 1 | return true; |
106 | |
} |
107 | |
|
108 | |
|
109 | |
@Override |
110 | |
public void setValue(ELContext context, Object base, Object property, |
111 | |
Object value) { |
112 | |
|
113 | 1 | } |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
protected void collectBeanInfo(Map<String, ? extends Object> map, |
123 | |
List<FeatureDescriptor> list) { |
124 | 7 | if (map == null || map.isEmpty()) { |
125 | 1 | return; |
126 | |
} |
127 | |
|
128 | 6 | for (Map.Entry<String, ? extends Object> entry : map.entrySet()) { |
129 | 6 | FeatureDescriptor descriptor = new FeatureDescriptor(); |
130 | 6 | descriptor.setDisplayName(entry.getKey()); |
131 | 6 | descriptor.setExpert(false); |
132 | 6 | descriptor.setHidden(false); |
133 | 6 | descriptor.setName(entry.getKey()); |
134 | 6 | descriptor.setPreferred(true); |
135 | 6 | descriptor.setShortDescription(""); |
136 | 6 | descriptor.setValue("type", String.class); |
137 | 6 | descriptor.setValue("resolvableAtDesignTime", Boolean.FALSE); |
138 | 6 | list.add(descriptor); |
139 | 6 | } |
140 | 6 | } |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
protected Object findObjectByProperty(ELContext context, Object property) { |
151 | 18 | Object retValue = null; |
152 | |
|
153 | 18 | Request request = (Request) context |
154 | |
.getContext(Request.class); |
155 | |
|
156 | 18 | String prop = property.toString(); |
157 | |
|
158 | 18 | String[] scopes = request.getAvailableScopes().toArray(new String[0]); |
159 | 18 | int i = 0; |
160 | |
do { |
161 | 35 | retValue = getObject(request.getContext(scopes[i]), prop); |
162 | 35 | i++; |
163 | 35 | } while (retValue == null && i < scopes.length); |
164 | |
|
165 | 18 | return retValue; |
166 | |
} |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
protected Object getObject(Map<String, ? extends Object> map, |
177 | |
String property) { |
178 | 38 | Object retValue = null; |
179 | 38 | if (map != null) { |
180 | 37 | retValue = map.get(property); |
181 | |
} |
182 | 38 | return retValue; |
183 | |
} |
184 | |
} |