-
Notifications
You must be signed in to change notification settings - Fork 1
/
XspQuery.java
290 lines (249 loc) · 7.84 KB
/
XspQuery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package org.openntf.xsp.extlib.query;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.visit.VisitCallback;
import javax.faces.component.visit.VisitContext;
import javax.faces.component.visit.VisitHint;
import javax.faces.component.visit.VisitResult;
import javax.faces.context.FacesContext;
import com.ibm.xsp.context.FacesContextEx;
public class XspQuery extends ArrayList<UIComponent> {
private static final long serialVersionUID = 1L;
protected static Logger _logger;
static {
_logger = Logger.getLogger(XspQuery.class.getName());
}
private List<QueryFilter> filters;
private List<VisitCallback> callbacks;
private ICacheContainer cacheContainer;
public XspQuery() {
}
public XspQuery addCallback(VisitCallback callback) {
getCallbacks().add(callback);
return this;
}
public XspQuery addContains(String propertyName, Object content) {
getFilters().add(new ContainsFilter(propertyName, content));
return this;
}
public XspQuery addEndsWith(String propertyName, String content) {
getFilters().add(new EndsWithFilter(propertyName, content));
return this;
}
public XspQuery addEquals(String propertyName, Object content) {
getFilters().add(new EqualsFilter(propertyName, content));
return this;
}
public XspQuery addClientIdFilter(String propertyName, Object content) {
getFilters().add(new ClientIdEqualsFilter(propertyName, content));
return this;
}
public XspQuery addInstanceOf(Class<? extends UIComponent> klass) {
getFilters().add(new InstanceFilter(klass));
return this;
}
public XspQuery addStartsWith(String propertyName, String content) {
getFilters().add(new StartsWithFilter(propertyName, content));
return this;
}
public UIComponent byId(String id) {
return byId(id, null);
}
public UIComponent byId(String id, UIComponent root) {
UIComponent result = null;
addEquals("id", id);
List<UIComponent> matches = locate(root);
if (matches.size() > 0) {
_logger.log(Level.FINER, "Found a component match by id: " + id);
result = matches.get(0);
} else {
_logger.log(Level.FINER, "Unable to find a component match by id: "
+ id);
}
return result;
}
public UIComponent byClientId(String id) {
return byClientId(id, null);
}
public UIComponent byClientId(String id, UIComponent root) {
_logger.finer("Component requested by client id " + id);
UIComponent result = null;
if (getCacheContainer() != null) {
_logger
.finer("Cache container exists, requesting cached component");
result = getCacheContainer().get(id);
} else {
_logger.warning("This org.openntf.xsp.extlib.query has a null cache container");
}
if (result == null) {
_logger.log(Level.FINER, "Did not find result in cache.");
if (root == null) {
root = FacesContext.getCurrentInstance().getViewRoot();
}
addClientIdFilter("clientId", id);
List<UIComponent> matches = locate(root);
if (matches.size() > 0) {
result = matches.get(0);
}
} else {
_logger.log(Level.FINER, "Found result in cache.");
}
return result;
}
protected List<VisitCallback> getCallbacks() {
if (this.callbacks == null) {
this.callbacks = new ArrayList<VisitCallback>();
}
return this.callbacks;
}
protected List<QueryFilter> getFilters() {
if (this.filters == null) {
this.filters = new ArrayList<QueryFilter>();
}
return this.filters;
}
public List<UIComponent> locate() {
return locate(null);
}
public List<UIComponent> locate(UIComponent root) {
if (root == null) {
root = FacesContext.getCurrentInstance().getViewRoot();
}
safeVisitTree(new QueryVisitContext(), new QueryVisitCallback(), root);
return this;
}
public UIInput locateField(String id) {
return locateField(id, null);
}
public UIInput locateField(String id, UIComponent root) {
UIInput result = null;
addEquals("id", id);
List<UIInput> inputs = locateInputs(root);
if (inputs.size() > 0) {
result = inputs.get(0);
}
return result;
}
public List<UIInput> locateInputs() {
return locateInputs(null);
}
public List<UIInput> locateInputs(UIComponent root) {
List<UIInput> inputs = new ArrayList<UIInput>();
addInstanceOf(UIInput.class);
List<UIComponent> components = locate(null);
for (UIComponent component : components) {
inputs.add((UIInput) component);
}
return inputs;
}
public static boolean safeVisitTree(VisitContext context,
VisitCallback callback, UIComponent component) {
if (!(isVisitable(context, component))) {
return false;
}
VisitResult res = context.invokeVisitCallback(component, callback);
if (res == VisitResult.COMPLETE) {
return true;
}
if ((res == VisitResult.ACCEPT)
&& (((component.getChildCount() > 0) || (component
.getFacetCount() > 0)))) {
for (Iterator it = component.getFacetsAndChildren(); it.hasNext();) {
UIComponent c = (UIComponent) it.next();
if (safeVisitTree(context, callback, c)) {
return true;
}
}
}
return false;
}
private static boolean isVisitable(VisitContext context,
UIComponent component) {
Collection hints = context.getHints();
if ((hints.contains(VisitHint.SKIP_TRANSIENT))
&& (component.isTransient())) {
return false;
}
return ((!(hints.contains(VisitHint.SKIP_UNRENDERED))) || (component
.isRendered()));
}
public XspQuery setCacheContainer(ICacheContainer cacheContainer) {
this.cacheContainer = cacheContainer;
return this;
}
protected ICacheContainer getCacheContainer() {
return this.cacheContainer;
}
private class QueryVisitCallback implements VisitCallback {
public VisitResult visit(VisitContext context, UIComponent component) {
refreshCache(context, component);
boolean valid = true;
for (QueryFilter filter : getFilters()) {
if (!filter.matches(component, FacesContext
.getCurrentInstance())) {
valid = false;
break;
}
}
if (valid) {
List<VisitCallback> forEach = getCallbacks();
for (VisitCallback each : forEach) {
each.visit(context, component);
}
add(component);
/*
* List<VisitCallback> forEach = getCallbacks(); for
* (VisitCallback each : forEach) { VisitResult thisResult =
* each.visit(context, component); switch
* (thisResult.compareTo(VisitResult.ACCEPT)) { case 1: //
* REJECT return thisResult; case 2: // COMPLETE add(component);
* return thisResult; } add(component); }
*/
}
return VisitResult.ACCEPT;
}
private void refreshCache(VisitContext context, UIComponent component) {
ICacheContainer cache = getCacheContainer();
if (!(cache == null)) {
String clientId = component.getClientId(context
.getFacesContext());
if (!(cache.containsKey(clientId))) {
cache.put(clientId, component);
}
}
}
}
private class QueryVisitContext extends VisitContext {
@Override
public FacesContext getFacesContext() {
return FacesContextEx.getCurrentInstance();
}
@Override
public Set<VisitHint> getHints() {
Set<VisitHint> hints = new HashSet<VisitHint>();
hints.add(VisitHint.SKIP_TRANSIENT);
return hints;
}
@Override
public Collection<String> getIdsToVisit() {
return null;
}
@Override
public Collection<String> getSubtreeIdsToVisit(UIComponent component) {
return null;
}
@Override
public VisitResult invokeVisitCallback(UIComponent component,
VisitCallback callback) {
return callback.visit(this, component);
}
}
}