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 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 {
72 this.predicates = predicateSet;
73 }
74
75 public DefaultFilterExpr(Expr expr, PredicateSet predicateSet)
76 {
77 this.expr = expr;
78 this.predicates = predicateSet;
79 }
80
81 public void addPredicate(Predicate predicate)
82 {
83 this.predicates.addPredicate( predicate );
84 }
85
86 public List getPredicates()
87 {
88 return this.predicates.getPredicates();
89 }
90
91 public PredicateSet getPredicateSet()
92 {
93 return this.predicates;
94 }
95
96 public Expr getExpr()
97 {
98 return this.expr;
99 }
100
101 public String toString()
102 {
103 return "[(DefaultFilterExpr): expr: " + expr + " predicates: " + predicates + " ]";
104 }
105
106 public String getText()
107 {
108 String text = "";
109 if ( this.expr != null )
110 {
111 text = this.expr.getText();
112 }
113 text += predicates.getText();
114 return text;
115 }
116
117 public Expr simplify()
118 {
119 this.predicates.simplify();
120
121 if ( this.expr != null )
122 {
123 this.expr = this.expr.simplify();
124 }
125
126 if ( this.predicates.getPredicates().size() == 0 )
127 {
128 return getExpr();
129 }
130
131 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 Object results = null;
139 if ( expr != null )
140 {
141 results = expr.evaluate( context );
142 }
143 else
144 {
145 List nodeSet = context.getNodeSet();
146 ArrayList list = new ArrayList(nodeSet.size());
147 list.addAll( nodeSet );
148 results = list;
149 }
150
151 if ( results instanceof Boolean )
152 {
153 Boolean b = (Boolean) results;
154 return b.booleanValue();
155 }
156 if ( results instanceof List )
157 {
158 return getPredicateSet().evaluateAsBoolean(
159 (List) results, context.getContextSupport()
160 );
161 }
162
163 return false;
164 }
165
166 public Object evaluate(Context context) throws JaxenException
167 {
168 Object results = getExpr().evaluate( context );
169
170 if ( results instanceof List )
171 {
172 List newresults = getPredicateSet().evaluatePredicates( (List) results,
173 context.getContextSupport() );
174 results = newresults;
175 }
176
177 return results;
178 }
179
180 }