1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.el.unified.resolver.implicitobject; |
20 | |
|
21 | |
import javax.el.*; |
22 | |
import java.beans.FeatureDescriptor; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
public class ImplicitObjectResolver extends ELResolver { |
34 | |
|
35 | |
private Map<String, ImplicitObject> implicitObjects; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public static ELResolver makeResolverForJSP() { |
42 | 0 | Map<String, ImplicitObject> forJSPList = new HashMap<String, ImplicitObject>(2); |
43 | 0 | ImplicitObject io1 = new FacesContextImplicitObject(); |
44 | 0 | forJSPList.put(io1.getName(), io1); |
45 | 0 | ImplicitObject io2 = new ViewImplicitObject(); |
46 | 0 | forJSPList.put(io2.getName(), io2); |
47 | 0 | return new ImplicitObjectResolver(forJSPList); |
48 | |
} |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public static ELResolver makeResolverForFaces() { |
55 | 0 | Map<String, ImplicitObject> forFacesList = new HashMap<String, ImplicitObject>(14); |
56 | 0 | ImplicitObject io1 = new ApplicationImplicitObject(); |
57 | 0 | forFacesList.put(io1.getName(), io1); |
58 | 0 | ImplicitObject io2 = new ApplicationScopeImplicitObject(); |
59 | 0 | forFacesList.put(io2.getName(), io2); |
60 | 0 | ImplicitObject io3 = new CookieImplicitObject(); |
61 | 0 | forFacesList.put(io3.getName(), io3); |
62 | 0 | ImplicitObject io4 = new FacesContextImplicitObject(); |
63 | 0 | forFacesList.put(io4.getName(), io4); |
64 | 0 | ImplicitObject io5 = new HeaderImplicitObject(); |
65 | 0 | forFacesList.put(io5.getName(), io5); |
66 | 0 | ImplicitObject io6 = new HeaderValuesImplicitObject(); |
67 | 0 | forFacesList.put(io6.getName(), io6); |
68 | 0 | ImplicitObject io7 = new InitParamImplicitObject(); |
69 | 0 | forFacesList.put(io7.getName(), io7); |
70 | 0 | ImplicitObject io8 = new ParamImplicitObject(); |
71 | 0 | forFacesList.put(io8.getName(), io8); |
72 | 0 | ImplicitObject io9 = new ParamValuesImplicitObject(); |
73 | 0 | forFacesList.put(io9.getName(), io9); |
74 | 0 | ImplicitObject io10 = new RequestImplicitObject(); |
75 | 0 | forFacesList.put(io10.getName(), io10); |
76 | 0 | ImplicitObject io11 = new RequestScopeImplicitObject(); |
77 | 0 | forFacesList.put(io11.getName(), io11); |
78 | 0 | ImplicitObject io12 = new SessionImplicitObject(); |
79 | 0 | forFacesList.put(io12.getName(), io12); |
80 | 0 | ImplicitObject io13 = new SessionScopeImplicitObject(); |
81 | 0 | forFacesList.put(io13.getName(), io13); |
82 | 0 | ImplicitObject io14 = new ViewImplicitObject(); |
83 | 0 | forFacesList.put(io14.getName(), io14); |
84 | 0 | return new ImplicitObjectResolver(forFacesList); |
85 | |
} |
86 | |
|
87 | |
|
88 | |
private ImplicitObjectResolver() { |
89 | 0 | super(); |
90 | 0 | this.implicitObjects = new HashMap<String, ImplicitObject>(); |
91 | 0 | } |
92 | |
|
93 | |
|
94 | |
private ImplicitObjectResolver(Map<String, ImplicitObject> implicitObjects) { |
95 | 0 | this(); |
96 | 0 | this.implicitObjects = implicitObjects; |
97 | 0 | } |
98 | |
|
99 | |
public void setValue(ELContext context, Object base, Object property, Object value) |
100 | |
throws NullPointerException, PropertyNotFoundException, PropertyNotWritableException, ELException { |
101 | |
|
102 | 0 | if (base != null) return; |
103 | 0 | if (property == null) throw new PropertyNotFoundException(); |
104 | 0 | if (!(property instanceof String)) return; |
105 | |
|
106 | 0 | String strProperty = castAndIntern(property); |
107 | |
|
108 | 0 | if (implicitObjects.containsKey(strProperty)) { |
109 | 0 | throw new PropertyNotWritableException(); |
110 | |
} |
111 | 0 | } |
112 | |
|
113 | |
public boolean isReadOnly(ELContext context, Object base, Object property) |
114 | |
throws NullPointerException, PropertyNotFoundException, ELException { |
115 | |
|
116 | 0 | if (base != null) return false; |
117 | 0 | if (property == null) throw new PropertyNotFoundException(); |
118 | 0 | if (!(property instanceof String)) return false; |
119 | |
|
120 | 0 | String strProperty = castAndIntern(property); |
121 | |
|
122 | 0 | if (implicitObjects.containsKey(strProperty)) { |
123 | 0 | context.setPropertyResolved(true); |
124 | 0 | return true; |
125 | |
} |
126 | |
|
127 | 0 | return false; |
128 | |
} |
129 | |
|
130 | |
public Object getValue(ELContext context, Object base, Object property) |
131 | |
throws NullPointerException, PropertyNotFoundException, ELException { |
132 | |
|
133 | 0 | if (base != null) return null; |
134 | 0 | if (property == null) throw new PropertyNotFoundException(); |
135 | 0 | if (!(property instanceof String)) return null; |
136 | |
|
137 | 0 | String strProperty = castAndIntern(property); |
138 | |
|
139 | 0 | ImplicitObject obj = implicitObjects.get(strProperty); |
140 | 0 | if (obj != null) { |
141 | 0 | context.setPropertyResolved(true); |
142 | 0 | return obj.getValue(context); |
143 | |
} |
144 | |
|
145 | 0 | return null; |
146 | |
} |
147 | |
|
148 | |
public Class<?> getType(ELContext context, Object base, Object property) |
149 | |
throws NullPointerException, PropertyNotFoundException, ELException { |
150 | |
|
151 | 0 | if (base != null) return null; |
152 | 0 | if (property == null) throw new PropertyNotFoundException(); |
153 | 0 | if (!(property instanceof String)) return null; |
154 | |
|
155 | 0 | String strProperty = castAndIntern(property); |
156 | |
|
157 | 0 | if (implicitObjects.containsKey(strProperty)) { |
158 | 0 | context.setPropertyResolved(true); |
159 | |
} |
160 | |
|
161 | 0 | return null; |
162 | |
} |
163 | |
|
164 | |
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) { |
165 | 0 | if (base != null) return null; |
166 | |
|
167 | 0 | ArrayList<FeatureDescriptor> descriptors = new ArrayList<FeatureDescriptor>(implicitObjects.size()); |
168 | |
|
169 | 0 | for (ImplicitObject obj: implicitObjects.values()) { |
170 | 0 | descriptors.add(obj.getDescriptor()); |
171 | |
} |
172 | |
|
173 | 0 | return descriptors.iterator(); |
174 | |
} |
175 | |
|
176 | |
public Class<?> getCommonPropertyType(ELContext context, Object base) { |
177 | 0 | if (base != null) return null; |
178 | |
|
179 | 0 | return String.class; |
180 | |
} |
181 | |
|
182 | |
protected String castAndIntern(Object o) { |
183 | 0 | String s = (String)o; |
184 | 0 | return s.intern(); |
185 | |
} |
186 | |
|
187 | |
} |