Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
PredicateSet |
|
| 2.25;2.25 |
1 | /* |
|
2 | * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/expr/PredicateSet.java,v 1.19 2005/06/28 13:44:44 elharo Exp $ |
|
3 | * $Revision: 1.19 $ |
|
4 | * $Date: 2005/06/28 13:44:44 $ |
|
5 | * |
|
6 | * ==================================================================== |
|
7 | * |
|
8 | * Copyright (C) 2000-2002 bob mcwhirter & James Strachan. |
|
9 | * All rights reserved. |
|
10 | * |
|
11 | * Redistribution and use in source and binary forms, with or without |
|
12 | * modification, are permitted provided that the following conditions |
|
13 | * are met: |
|
14 | * |
|
15 | * 1. Redistributions of source code must retain the above copyright |
|
16 | * notice, this list of conditions, and the following disclaimer. |
|
17 | * |
|
18 | * 2. Redistributions in binary form must reproduce the above copyright |
|
19 | * notice, this list of conditions, and the disclaimer that follows |
|
20 | * these conditions in the documentation and/or other materials |
|
21 | * provided with the distribution. |
|
22 | * |
|
23 | * 3. The name "Jaxen" must not be used to endorse or promote products |
|
24 | * derived from this software without prior written permission. For |
|
25 | * written permission, please contact license@jaxen.org. |
|
26 | * |
|
27 | * 4. Products derived from this software may not be called "Jaxen", nor |
|
28 | * may "Jaxen" appear in their name, without prior written permission |
|
29 | * from the Jaxen Project Management (pm@jaxen.org). |
|
30 | * |
|
31 | * In addition, we request (but do not require) that you include in the |
|
32 | * end-user documentation provided with the redistribution and/or in the |
|
33 | * software itself an acknowledgement equivalent to the following: |
|
34 | * "This product includes software developed by the |
|
35 | * Jaxen Project <http://www.jaxen.org/>." |
|
36 | * Alternatively, the acknowledgment may be graphical using the logos |
|
37 | * available at http://www.jaxen.org/ |
|
38 | * |
|
39 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
|
40 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
41 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|
42 | * DISCLAIMED. IN NO EVENT SHALL THE Jaxen AUTHORS OR THE PROJECT |
|
43 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
45 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
|
46 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|
47 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|
48 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
|
49 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
50 | * SUCH DAMAGE. |
|
51 | * |
|
52 | * ==================================================================== |
|
53 | * This software consists of voluntary contributions made by many |
|
54 | * individuals on behalf of the Jaxen Project and was originally |
|
55 | * created by bob mcwhirter <bob@werken.com> and |
|
56 | * James Strachan <jstrachan@apache.org>. For more information on the |
|
57 | * Jaxen Project, please see <http://www.jaxen.org/>. |
|
58 | * |
|
59 | * $Id: PredicateSet.java,v 1.19 2005/06/28 13:44:44 elharo Exp $ |
|
60 | */ |
|
61 | ||
62 | ||
63 | ||
64 | package org.jaxen.expr; |
|
65 | ||
66 | import java.io.Serializable; |
|
67 | import java.util.ArrayList; |
|
68 | import java.util.Collections; |
|
69 | import java.util.Iterator; |
|
70 | import java.util.List; |
|
71 | import org.jaxen.Context; |
|
72 | import org.jaxen.ContextSupport; |
|
73 | import org.jaxen.JaxenException; |
|
74 | import org.jaxen.function.BooleanFunction; |
|
75 | ||
76 | public class PredicateSet implements Serializable |
|
77 | { |
|
78 | private List predicates; |
|
79 | ||
80 | public PredicateSet() |
|
81 | 29502 | { |
82 | 29502 | this.predicates = Collections.EMPTY_LIST; |
83 | 29502 | } |
84 | ||
85 | public void addPredicate(Predicate predicate) |
|
86 | { |
|
87 | 2346 | if ( this.predicates == Collections.EMPTY_LIST ) |
88 | { |
|
89 | 2268 | this.predicates = new ArrayList(); |
90 | } |
|
91 | ||
92 | 2346 | this.predicates.add( predicate ); |
93 | 2346 | } |
94 | ||
95 | public List getPredicates() |
|
96 | { |
|
97 | 13560 | return this.predicates; |
98 | } |
|
99 | ||
100 | public void simplify() |
|
101 | { |
|
102 | 29010 | Iterator predIter = this.predicates.iterator(); |
103 | 29010 | Predicate eachPred = null; |
104 | ||
105 | 31302 | while ( predIter.hasNext() ) |
106 | { |
|
107 | 2292 | eachPred = (Predicate) predIter.next(); |
108 | 2292 | eachPred.simplify(); |
109 | } |
|
110 | 29010 | } |
111 | ||
112 | public String getText() |
|
113 | { |
|
114 | 72 | StringBuffer buf = new StringBuffer(); |
115 | ||
116 | 72 | Iterator predIter = this.predicates.iterator(); |
117 | 72 | Predicate eachPred = null; |
118 | ||
119 | 72 | while ( predIter.hasNext() ) |
120 | { |
|
121 | 0 | eachPred = (Predicate) predIter.next(); |
122 | 0 | buf.append( eachPred.getText() ); |
123 | } |
|
124 | ||
125 | 72 | return buf.toString(); |
126 | } |
|
127 | ||
128 | // FIXME: Note - this could be *MUCH* more efficient |
|
129 | // currently this creates many redundant collections and should halt |
|
130 | // evaluation on the first matching item. |
|
131 | protected boolean evaluateAsBoolean(List contextNodeSet, |
|
132 | ContextSupport support) throws JaxenException |
|
133 | { |
|
134 | 0 | List result = evaluatePredicates( contextNodeSet, support ); |
135 | ||
136 | 0 | return ! result.isEmpty(); |
137 | } |
|
138 | ||
139 | protected List evaluatePredicates(List contextNodeSet, ContextSupport support) |
|
140 | throws JaxenException { |
|
141 | // Easy way out (necessary) |
|
142 | 54288 | if (predicates.size() == 0) { |
143 | 38118 | return contextNodeSet; |
144 | } |
|
145 | 16170 | Iterator predIter = predicates.iterator(); |
146 | ||
147 | // initial list to filter |
|
148 | 16170 | List nodes2Filter = contextNodeSet; |
149 | // apply all predicates |
|
150 | 32406 | while(predIter.hasNext()) { |
151 | 16248 | nodes2Filter = |
152 | applyPredicate((Predicate)predIter.next(), nodes2Filter, support); |
|
153 | } |
|
154 | ||
155 | 16158 | return nodes2Filter; |
156 | } |
|
157 | ||
158 | public List applyPredicate(Predicate predicate, List nodes2Filter, ContextSupport support) |
|
159 | throws JaxenException { |
|
160 | 16248 | final int nodes2FilterSize = nodes2Filter.size(); |
161 | 16248 | List filteredNodes = new ArrayList(nodes2FilterSize); |
162 | // Set up a dummy context with a list to hold each node |
|
163 | 16248 | Context predContext = new Context(support); |
164 | 16248 | List tempList = new ArrayList(1); |
165 | 16248 | predContext.setNodeSet(tempList); |
166 | // loop through the current nodes to filter and add to the |
|
167 | // filtered nodes list if the predicate succeeds |
|
168 | 39780 | for (int i = 0; i < nodes2FilterSize; ++i) { |
169 | 23544 | Object contextNode = nodes2Filter.get(i); |
170 | 23544 | tempList.clear(); |
171 | 23544 | tempList.add(contextNode); |
172 | 23544 | predContext.setNodeSet(tempList); |
173 | // ???? |
|
174 | 23544 | predContext.setPosition(i + 1); |
175 | 23544 | predContext.setSize(nodes2FilterSize); |
176 | 23544 | Object predResult = predicate.evaluate(predContext); |
177 | 23532 | if (predResult instanceof Number) { |
178 | // Here we assume nodes are in forward or reverse order |
|
179 | // as appropriate for axis |
|
180 | 4482 | int proximity = ((Number) predResult).intValue(); |
181 | 4482 | if (proximity == (i + 1)) { |
182 | 942 | filteredNodes.add(contextNode); |
183 | } |
|
184 | } |
|
185 | else { |
|
186 | 19050 | Boolean includes = |
187 | BooleanFunction.evaluate(predResult, |
|
188 | predContext.getNavigator()); |
|
189 | 19050 | if (includes.booleanValue()) { |
190 | 2922 | filteredNodes.add(contextNode); |
191 | } |
|
192 | } |
|
193 | } |
|
194 | 16236 | return filteredNodes; |
195 | } |
|
196 | } |