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.ognl; |
22 | |
|
23 | |
import java.util.Map; |
24 | |
|
25 | |
import ognl.OgnlContext; |
26 | |
import ognl.PropertyAccessor; |
27 | |
|
28 | |
import org.apache.tiles.request.Request; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 8 | public class AnyScopePropertyAccessor implements PropertyAccessor { |
36 | |
|
37 | |
@Override |
38 | |
public Object getProperty(@SuppressWarnings("rawtypes") Map context, Object target, Object name) { |
39 | 11 | Request request = (Request) target; |
40 | 11 | String attributeName = (String) name; |
41 | 11 | for (String scopeName : request.getAvailableScopes()) { |
42 | 19 | Map<String, Object> scope = request.getContext(scopeName); |
43 | 19 | if (scope.containsKey(attributeName)) { |
44 | 10 | return scope.get(attributeName); |
45 | |
} |
46 | 9 | } |
47 | 1 | return null; |
48 | |
} |
49 | |
|
50 | |
@Override |
51 | |
public String getSourceAccessor(OgnlContext context, Object target, |
52 | |
Object index) { |
53 | 3 | Request request = (Request) target; |
54 | 3 | String attributeName = (String) index; |
55 | 3 | for (String scopeName : request.getAvailableScopes()) { |
56 | 5 | Map<String, Object> scope = request.getContext(scopeName); |
57 | 5 | if (scope.containsKey(attributeName)) { |
58 | 2 | return ".getContext(\"" + scopeName + "\").get(index)"; |
59 | |
} |
60 | 3 | } |
61 | 1 | return null; |
62 | |
} |
63 | |
|
64 | |
@Override |
65 | |
public String getSourceSetter(OgnlContext context, Object target, |
66 | |
Object index) { |
67 | 3 | Request request = (Request) target; |
68 | 3 | String attributeName = (String) index; |
69 | 3 | String[] availableScopes = request.getAvailableScopes().toArray(new String[0]); |
70 | 6 | for (String scopeName : availableScopes) { |
71 | 5 | Map<String, Object> scope = request.getContext(scopeName); |
72 | 5 | if (scope.containsKey(attributeName)) { |
73 | 2 | return ".getContext(\"" + scopeName + "\").put(index, target)"; |
74 | |
} |
75 | |
} |
76 | 1 | return ".getContext(\"" + availableScopes[0] + "\").put(index, target)"; |
77 | |
} |
78 | |
|
79 | |
@Override |
80 | |
public void setProperty(@SuppressWarnings("rawtypes") Map context, Object target, Object name, |
81 | |
Object value) { |
82 | 3 | Request request = (Request) target; |
83 | 3 | String attributeName = (String) name; |
84 | 3 | String[] availableScopes = request.getAvailableScopes().toArray(new String[0]); |
85 | 6 | for (String scopeName : availableScopes) { |
86 | 5 | Map<String, Object> scope = request.getContext(scopeName); |
87 | 5 | if (scope.containsKey(attributeName)) { |
88 | 2 | scope.put(attributeName, value); |
89 | 2 | return; |
90 | |
} |
91 | |
} |
92 | 1 | if (availableScopes.length > 0) { |
93 | 1 | request.getContext(availableScopes[0]).put(attributeName, value); |
94 | |
} |
95 | 1 | } |
96 | |
|
97 | |
} |