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 package org.jaxen.expr;
52
53 import java.io.Serializable;
54 import java.util.ArrayList;
55 import java.util.Iterator;
56 import java.util.List;
57 import org.jaxen.Context;
58 import org.jaxen.ContextSupport;
59 import org.jaxen.JaxenException;
60 import org.jaxen.function.BooleanFunction;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75 public class PredicateSet implements Serializable
76 {
77
78 private static final long serialVersionUID = -7166491740228977853L;
79
80 private List<Predicate> predicates;
81
82
83
84
85 public PredicateSet()
86 {
87 this.predicates = new ArrayList<Predicate>();
88 }
89
90
91
92
93
94
95 public void addPredicate(Predicate predicate)
96 {
97 this.predicates.add( predicate );
98 }
99
100
101
102
103
104
105
106 public List getPredicates()
107 {
108 return this.predicates;
109 }
110
111
112
113
114 public void simplify()
115 {
116 Iterator<Predicate> predIter = this.predicates.iterator();
117 Predicate eachPred = null;
118
119 while ( predIter.hasNext() )
120 {
121 eachPred = predIter.next();
122 eachPred.simplify();
123 }
124 }
125
126
127
128
129
130
131 public String getText()
132 {
133 StringBuffer buf = new StringBuffer();
134
135 Iterator<Predicate> predIter = this.predicates.iterator();
136 Predicate eachPred = null;
137
138 while ( predIter.hasNext() )
139 {
140 eachPred = (Predicate) predIter.next();
141 buf.append( eachPred.getText() );
142 }
143
144 return buf.toString();
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158 protected boolean evaluateAsBoolean(List contextNodeSet,
159 ContextSupport support) throws JaxenException
160 {
161 return anyMatchingNode( contextNodeSet, support );
162 }
163
164 private boolean anyMatchingNode(List contextNodeSet, ContextSupport support)
165 throws JaxenException {
166
167 if (predicates.isEmpty()) {
168 return false;
169 }
170 Iterator<Predicate> predIter = predicates.iterator();
171
172
173 List nodes2Filter = contextNodeSet;
174
175 while(predIter.hasNext()) {
176 final int nodes2FilterSize = nodes2Filter.size();
177
178 Context predContext = new Context(support);
179 List<Object> tempList = new ArrayList<Object>(1);
180 predContext.setNodeSet(tempList);
181
182
183 for (int i = 0; i < nodes2FilterSize; ++i) {
184 Object contextNode = nodes2Filter.get(i);
185 tempList.clear();
186 tempList.add(contextNode);
187 predContext.setNodeSet(tempList);
188
189 predContext.setPosition(i + 1);
190 predContext.setSize(nodes2FilterSize);
191 Object predResult = ((Predicate)predIter.next()).evaluate(predContext);
192 if (predResult instanceof Number) {
193
194
195 int proximity = ((Number) predResult).intValue();
196 if (proximity == (i + 1)) {
197 return true;
198 }
199 }
200 else {
201 Boolean includes =
202 BooleanFunction.evaluate(predResult,
203 predContext.getNavigator());
204 if (includes.booleanValue()) {
205 return true;
206 }
207 }
208 }
209 }
210
211 return false;
212 }
213
214
215
216
217
218
219
220
221
222
223
224
225
226 protected List evaluatePredicates(List contextNodeSet, ContextSupport support)
227 throws JaxenException {
228
229 if (predicates.size() == 0) {
230 return contextNodeSet;
231 }
232 Iterator<Predicate> predIter = predicates.iterator();
233
234
235 List nodes2Filter = contextNodeSet;
236
237 while(predIter.hasNext()) {
238 nodes2Filter =
239 applyPredicate((Predicate)predIter.next(), nodes2Filter, support);
240 }
241
242 return nodes2Filter;
243 }
244
245 public List applyPredicate(Predicate predicate, List nodes2Filter, ContextSupport support)
246 throws JaxenException {
247 final int nodes2FilterSize = nodes2Filter.size();
248 List<Object> filteredNodes = new ArrayList<Object>(nodes2FilterSize);
249
250 Context predContext = new Context(support);
251 List<Object> tempList = new ArrayList<Object>(1);
252 predContext.setNodeSet(tempList);
253
254
255 for (int i = 0; i < nodes2FilterSize; ++i) {
256 Object contextNode = nodes2Filter.get(i);
257 tempList.clear();
258 tempList.add(contextNode);
259 predContext.setNodeSet(tempList);
260
261 predContext.setPosition(i + 1);
262 predContext.setSize(nodes2FilterSize);
263 Object predResult = predicate.evaluate(predContext);
264 if (predResult instanceof Number) {
265
266
267 int proximity = ((Number) predResult).intValue();
268 if (proximity == (i + 1)) {
269 filteredNodes.add(contextNode);
270 }
271 }
272 else {
273 Boolean includes =
274 BooleanFunction.evaluate(predResult,
275 predContext.getNavigator());
276 if (includes.booleanValue()) {
277 filteredNodes.add(contextNode);
278 }
279 }
280 }
281 return filteredNodes;
282 }
283
284 }