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 package org.jaxen.expr;
65
66 import java.util.ArrayList;
67 import java.util.List;
68 import org.jaxen.Context;
69 import org.jaxen.JaxenException;
70
71 /***
72 * @deprecated this class will become non-public in the future;
73 * use the interface instead
74 */
75 public class DefaultFilterExpr extends DefaultExpr implements FilterExpr, Predicated
76 {
77 private Expr expr;
78 private PredicateSet predicates;
79
80 public DefaultFilterExpr(PredicateSet predicateSet)
81 {
82 this.predicates = predicateSet;
83 }
84
85 public DefaultFilterExpr(Expr expr, PredicateSet predicateSet)
86 {
87 this.expr = expr;
88 this.predicates = predicateSet;
89 }
90
91 public void addPredicate(Predicate predicate)
92 {
93 this.predicates.addPredicate( predicate );
94 }
95
96 public List getPredicates()
97 {
98 return this.predicates.getPredicates();
99 }
100
101 public PredicateSet getPredicateSet()
102 {
103 return this.predicates;
104 }
105
106 public Expr getExpr()
107 {
108 return this.expr;
109 }
110
111 public String toString()
112 {
113 return "[(DefaultFilterExpr): expr: " + expr + " predicates: " + predicates + " ]";
114 }
115
116 public String getText()
117 {
118 String text = "";
119 if ( this.expr != null )
120 {
121 text = this.expr.getText();
122 }
123 text += predicates.getText();
124 return text;
125 }
126
127 public Expr simplify()
128 {
129 this.predicates.simplify();
130
131 if ( this.expr != null )
132 {
133 this.expr = this.expr.simplify();
134 }
135
136 if ( this.predicates.getPredicates().size() == 0 )
137 {
138 return getExpr();
139 }
140
141 return this;
142 }
143
144 /*** Returns true if the current filter matches at least one of the context nodes
145 */
146 public boolean asBoolean(Context context) throws JaxenException
147 {
148 Object results = null;
149 if ( expr != null )
150 {
151 results = expr.evaluate( context );
152 }
153 else
154 {
155 List nodeSet = context.getNodeSet();
156 ArrayList list = new ArrayList(nodeSet.size());
157 list.addAll( nodeSet );
158 results = list;
159 }
160
161 if ( results instanceof Boolean )
162 {
163 Boolean b = (Boolean) results;
164 return b.booleanValue();
165 }
166 if ( results instanceof List )
167 {
168 return getPredicateSet().evaluateAsBoolean(
169 (List) results, context.getContextSupport()
170 );
171 }
172
173 return false;
174 }
175
176 public Object evaluate(Context context) throws JaxenException
177 {
178 Object results = getExpr().evaluate( context );
179
180 if ( results instanceof List )
181 {
182 List newresults = getPredicateSet().evaluatePredicates( (List) results,
183 context.getContextSupport() );
184 results = newresults;
185 }
186
187 return results;
188 }
189 public void accept(Visitor visitor)
190 {
191 visitor.visit(this);
192 }
193 }