Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DefaultFilterExpr |
|
| 1.9090909090909092;1.909 |
1 | /* | |
2 | * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/expr/DefaultFilterExpr.java,v 1.19 2006/11/30 18:49:26 elharo Exp $ | |
3 | * $Revision: 1.19 $ | |
4 | * $Date: 2006/11/30 18:49:26 $ | |
5 | * | |
6 | * ==================================================================== | |
7 | * | |
8 | * Copyright 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 are | |
13 | * met: | |
14 | * | |
15 | * * Redistributions of source code must retain the above copyright | |
16 | * notice, this list of conditions and the following disclaimer. | |
17 | * | |
18 | * * Redistributions in binary form must reproduce the above copyright | |
19 | * notice, this list of conditions and the following disclaimer in the | |
20 | * documentation and/or other materials provided with the distribution. | |
21 | * | |
22 | * * Neither the name of the Jaxen Project nor the names of its | |
23 | * contributors may be used to endorse or promote products derived | |
24 | * from this software without specific prior written permission. | |
25 | * | |
26 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS | |
27 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
28 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | |
29 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | |
30 | * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
31 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
32 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
33 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
34 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
35 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
36 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
37 | * | |
38 | * ==================================================================== | |
39 | * This software consists of voluntary contributions made by many | |
40 | * individuals on behalf of the Jaxen Project and was originally | |
41 | * created by bob mcwhirter <bob@werken.com> and | |
42 | * James Strachan <jstrachan@apache.org>. For more information on the | |
43 | * Jaxen Project, please see <http://www.jaxen.org/>. | |
44 | * | |
45 | * $Id: DefaultFilterExpr.java,v 1.19 2006/11/30 18:49:26 elharo Exp $ | |
46 | */ | |
47 | ||
48 | ||
49 | ||
50 | package org.jaxen.expr; | |
51 | ||
52 | import java.util.ArrayList; | |
53 | import java.util.List; | |
54 | import org.jaxen.Context; | |
55 | import org.jaxen.JaxenException; | |
56 | ||
57 | /** | |
58 | * @deprecated this class will become non-public in the future; | |
59 | * use the interface instead | |
60 | */ | |
61 | public class DefaultFilterExpr extends DefaultExpr implements FilterExpr, Predicated | |
62 | { | |
63 | /** | |
64 | * | |
65 | */ | |
66 | private static final long serialVersionUID = -549640659288005735L; | |
67 | private Expr expr; | |
68 | private PredicateSet predicates; | |
69 | ||
70 | public DefaultFilterExpr(PredicateSet predicateSet) | |
71 | 10 | { |
72 | 10 | this.predicates = predicateSet; |
73 | 10 | } |
74 | ||
75 | public DefaultFilterExpr(Expr expr, PredicateSet predicateSet) | |
76 | 9460 | { |
77 | 9460 | this.expr = expr; |
78 | 9460 | this.predicates = predicateSet; |
79 | 9460 | } |
80 | ||
81 | public void addPredicate(Predicate predicate) | |
82 | { | |
83 | 12 | this.predicates.addPredicate( predicate ); |
84 | 12 | } |
85 | ||
86 | public List getPredicates() | |
87 | { | |
88 | 0 | return this.predicates.getPredicates(); |
89 | } | |
90 | ||
91 | public PredicateSet getPredicateSet() | |
92 | { | |
93 | 2 | return this.predicates; |
94 | } | |
95 | ||
96 | public Expr getExpr() | |
97 | { | |
98 | 9434 | return this.expr; |
99 | } | |
100 | ||
101 | public String toString() | |
102 | { | |
103 | 0 | return "[(DefaultFilterExpr): expr: " + expr + " predicates: " + predicates + " ]"; |
104 | } | |
105 | ||
106 | public String getText() | |
107 | { | |
108 | 0 | String text = ""; |
109 | 0 | if ( this.expr != null ) |
110 | { | |
111 | 0 | text = this.expr.getText(); |
112 | } | |
113 | 0 | text += predicates.getText(); |
114 | 0 | return text; |
115 | } | |
116 | ||
117 | public Expr simplify() | |
118 | { | |
119 | 9434 | this.predicates.simplify(); |
120 | ||
121 | 9434 | if ( this.expr != null ) |
122 | { | |
123 | 9434 | this.expr = this.expr.simplify(); |
124 | } | |
125 | ||
126 | 9434 | if ( this.predicates.getPredicates().size() == 0 ) |
127 | { | |
128 | 9432 | return getExpr(); |
129 | } | |
130 | ||
131 | 2 | return this; |
132 | } | |
133 | ||
134 | /** Returns true if the current filter matches at least one of the context nodes | |
135 | */ | |
136 | public boolean asBoolean(Context context) throws JaxenException | |
137 | { | |
138 | 0 | Object results = null; |
139 | 0 | if ( expr != null ) |
140 | { | |
141 | 0 | results = expr.evaluate( context ); |
142 | 0 | } |
143 | else | |
144 | { | |
145 | 0 | List nodeSet = context.getNodeSet(); |
146 | 0 | ArrayList list = new ArrayList(nodeSet.size()); |
147 | 0 | list.addAll( nodeSet ); |
148 | 0 | results = list; |
149 | } | |
150 | ||
151 | 0 | if ( results instanceof Boolean ) |
152 | { | |
153 | 0 | Boolean b = (Boolean) results; |
154 | 0 | return b.booleanValue(); |
155 | } | |
156 | 0 | if ( results instanceof List ) |
157 | { | |
158 | 0 | return getPredicateSet().evaluateAsBoolean( |
159 | (List) results, context.getContextSupport() | |
160 | ); | |
161 | } | |
162 | ||
163 | 0 | return false; |
164 | } | |
165 | ||
166 | public Object evaluate(Context context) throws JaxenException | |
167 | { | |
168 | 2 | Object results = getExpr().evaluate( context ); |
169 | ||
170 | 2 | if ( results instanceof List ) |
171 | { | |
172 | 2 | List newresults = getPredicateSet().evaluatePredicates( (List) results, |
173 | context.getContextSupport() ); | |
174 | 2 | results = newresults; |
175 | } | |
176 | ||
177 | 2 | return results; |
178 | } | |
179 | ||
180 | } |