KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jpath > expression > FilterOperator


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.lang.jpath.expression;
18
19 import javax.servlet.jsp.PageContext JavaDoc;
20
21 import org.apache.taglibs.standard.lang.jpath.adapter.ConversionException;
22 import org.apache.taglibs.standard.lang.jpath.adapter.Convert;
23 import org.apache.taglibs.standard.lang.jpath.adapter.IterationContext;
24 import org.apache.taglibs.standard.lang.jpath.adapter.JSPList;
25
26 /**
27  * The FilterOperator class
28  *
29  *
30  * @author <a HREF='mailto:scott.hasse@isthmusgroup.com'>Scott Hasse</a>
31  * @version
32  */

33 public class FilterOperator extends SimpleNode implements Introspectable {
34
35     /**
36      * Used to create an instance of the FilterOperator class
37      *
38      *
39      * @param id
40      *
41      */

42     public FilterOperator(int id) {
43         super(id);
44     }
45
46     /**
47      * Used to create an instance of the FilterOperator class
48      *
49      *
50      * @param p
51      * @param id
52      *
53      */

54     public FilterOperator(Parser p, int id) {
55         super(p, id);
56     }
57
58     /**
59      * Provides a method to print a normalized version of the original
60      * expression. The normalized version has standardized spacing and
61      * parenthesis, and can be used to compare expressions formatted
62      * in different ways to see if they are actually the same expression.
63      *
64      *
65      * @return The normalized version of the original expression
66      *
67      */

68     public String JavaDoc toNormalizedString() {
69
70         String JavaDoc normalized = "";
71
72         normalized = jjtGetChild(0).toNormalizedString()
73                 + jjtGetChild(1).toNormalizedString();
74
75         return normalized;
76     }
77
78     /**
79      * This method evaluates this node of the expression and all child nodes.
80      * It returns the result of the
81      * evaluation as an <tt>Object</tt>. If any problems are encountered
82      * during the evaluation, an <tt>EvaluationException</tt> is thrown.
83      *
84      *
85      * @param pageContext the current JSP PageContext
86      *
87      * @param icontext the Iteration Context of the expression. If there is
88      * no interation context, this should be null.
89      *
90      * @return the result of the expression evaluation as an object
91      *
92      * @throws EvaluationException if a problem is encountered during the
93      * evaluation
94      */

95     public Object JavaDoc evaluate(PageContext JavaDoc pageContext, IterationContext icontext)
96             throws EvaluationException {
97
98         Object JavaDoc result;
99
100         try {
101             JSPList leftSide =
102                 Convert.toJSPList(jjtGetChild(0).evaluate(pageContext,
103                     icontext));
104             Predicate predicate = (Predicate) jjtGetChild(1);
105             boolean oneItem = leftSide.applyPredicate(pageContext, predicate);
106
107             if (oneItem) {
108                 if (leftSide.getLast() == 1) {
109                     result = leftSide.next();
110                 } else {
111                     result = null;
112                 }
113             } else {
114                 result = leftSide;
115             }
116         } catch (ConversionException ce) {
117             throw new EvaluationException(this, ce.getMessage());
118         }
119
120         return result;
121     }
122
123     /**
124      * The evaluate method
125      *
126      *
127      * @param pageContext
128      * @param icontext
129      * @param scope
130      *
131      * @return
132      *
133      * @throws EvaluationException
134      *
135      */

136     public Object JavaDoc evaluate(
137             PageContext JavaDoc pageContext, IterationContext icontext, int scope)
138                 throws EvaluationException {
139
140         Object JavaDoc result;
141
142         try {
143             JSPList leftSide =
144                 Convert.toJSPList(((Introspectable) jjtGetChild(0))
145                     .evaluate(pageContext, icontext, scope));
146             Predicate predicate = (Predicate) jjtGetChild(1);
147             boolean oneItem = leftSide.applyPredicate(pageContext, predicate);
148
149             if (oneItem) {
150                 if (leftSide.getLast() == 1) {
151                     result = leftSide.next();
152                 } else {
153                     result = null;
154                 }
155             } else {
156                 result = leftSide;
157             }
158         } catch (ConversionException ce) {
159             throw new EvaluationException(this, ce.getMessage());
160         }
161
162         return result;
163     }
164
165     /**
166      * The evaluate method
167      *
168      *
169      * @param pageContext
170      * @param icontext
171      * @param parent
172      *
173      * @return
174      *
175      * @throws EvaluationException
176      *
177      */

178     public Object JavaDoc evaluate(
179             PageContext JavaDoc pageContext, IterationContext icontext, Object JavaDoc parent)
180                 throws EvaluationException {
181
182         Object JavaDoc result;
183
184         try {
185             JSPList leftSide =
186                 Convert.toJSPList(((Introspectable) jjtGetChild(0))
187                     .evaluate(pageContext, icontext, parent));
188             Predicate predicate = (Predicate) jjtGetChild(1);
189             boolean oneItem = leftSide.applyPredicate(pageContext, predicate);
190
191             if (oneItem) {
192                 if (leftSide.getLast() == 1) {
193                     result = leftSide.next();
194                 } else {
195                     result = null;
196                 }
197             } else {
198                 result = leftSide;
199             }
200         } catch (ConversionException ce) {
201             throw new EvaluationException(this, ce.getMessage());
202         }
203
204         return result;
205     }
206 }
207
Popular Tags