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.Collections; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.List; |
28 | |
import java.util.Map; |
29 | |
|
30 | |
import javax.el.ELContext; |
31 | |
import javax.el.ELResolver; |
32 | |
|
33 | |
import org.apache.tiles.request.Request; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | 9 | public class ScopeELResolver extends ELResolver { |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
private static final int SUFFIX_LENGTH = 5; |
47 | |
|
48 | |
|
49 | |
@Override |
50 | |
public Class<?> getCommonPropertyType(ELContext context, Object base) { |
51 | |
|
52 | 2 | if (base != null) { |
53 | 1 | return null; |
54 | |
} |
55 | |
|
56 | 1 | return Map.class; |
57 | |
} |
58 | |
|
59 | |
|
60 | |
@Override |
61 | |
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, |
62 | |
Object base) { |
63 | 2 | if (base != null) { |
64 | 1 | List<FeatureDescriptor> retValue = Collections.emptyList(); |
65 | 1 | return retValue.iterator(); |
66 | |
} |
67 | |
|
68 | 1 | List<FeatureDescriptor> list = new ArrayList<FeatureDescriptor>(); |
69 | |
|
70 | 1 | Request request = (Request) context |
71 | |
.getContext(Request.class); |
72 | 1 | for (String scope : request.getAvailableScopes()) { |
73 | 2 | FeatureDescriptor descriptor = new FeatureDescriptor(); |
74 | 2 | descriptor.setDisplayName(scope + "Scope"); |
75 | 2 | descriptor.setExpert(false); |
76 | 2 | descriptor.setHidden(false); |
77 | 2 | descriptor.setName(scope + "Scope"); |
78 | 2 | descriptor.setPreferred(true); |
79 | 2 | descriptor.setShortDescription(""); |
80 | 2 | descriptor.setValue("type", Map.class); |
81 | 2 | descriptor.setValue("resolvableAtDesignTime", Boolean.FALSE); |
82 | 2 | list.add(descriptor); |
83 | 2 | } |
84 | |
|
85 | 1 | return list.iterator(); |
86 | |
} |
87 | |
|
88 | |
|
89 | |
@Override |
90 | |
public Class<?> getType(ELContext context, Object base, Object property) { |
91 | 4 | if (base != null || property == null || !(property instanceof String) |
92 | |
|| !((String) property).endsWith("Scope")) { |
93 | 1 | return null; |
94 | |
} |
95 | |
|
96 | 3 | return Map.class; |
97 | |
} |
98 | |
|
99 | |
|
100 | |
@Override |
101 | |
public Object getValue(ELContext context, Object base, Object property) { |
102 | 26 | if (base != null) { |
103 | 9 | return null; |
104 | |
} |
105 | |
|
106 | 17 | Object retValue = null; |
107 | 17 | String propertyString = (String) property; |
108 | 17 | if (property instanceof String && propertyString.endsWith("Scope")) { |
109 | 9 | Request request = (Request) context |
110 | |
.getContext(Request.class); |
111 | 9 | retValue = request.getContext(propertyString.substring(0, |
112 | |
propertyString.length() - SUFFIX_LENGTH)); |
113 | |
} |
114 | |
|
115 | 17 | if (retValue != null) { |
116 | 9 | context.setPropertyResolved(true); |
117 | |
} |
118 | |
|
119 | 17 | return retValue; |
120 | |
} |
121 | |
|
122 | |
|
123 | |
@Override |
124 | |
public boolean isReadOnly(ELContext context, Object base, Object property) { |
125 | 2 | if (context == null) { |
126 | 1 | throw new NullPointerException(); |
127 | |
} |
128 | |
|
129 | 1 | return true; |
130 | |
} |
131 | |
|
132 | |
|
133 | |
@Override |
134 | |
public void setValue(ELContext context, Object base, Object property, |
135 | |
Object value) { |
136 | |
|
137 | 1 | } |
138 | |
} |