KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xpath > axes > FilterExprIterator


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  * $Id: FilterExprIterator.java,v 1.5 2004/02/17 04:32:08 minchau Exp $
18  */

19 package org.apache.xpath.axes;
20
21 import org.apache.xml.dtm.DTM;
22 import org.apache.xpath.Expression;
23 import org.apache.xpath.ExpressionOwner;
24 import org.apache.xpath.XPathVisitor;
25 import org.apache.xpath.objects.XNodeSet;
26
27 public class FilterExprIterator extends BasicTestIterator
28 {
29   /** The contained expression. Should be non-null.
30    * @serial */

31   private Expression m_expr;
32
33   /** The result of executing m_expr. Needs to be deep cloned on clone op. */
34   transient private XNodeSet m_exprObj;
35
36   private boolean m_mustHardReset = false;
37   private boolean m_canDetachNodeset = true;
38
39   /**
40    * Create a ChildTestIterator object.
41    *
42    * @param traverser Traverser that tells how the KeyIterator is to be handled.
43    *
44    * @throws javax.xml.transform.TransformerException
45    */

46   public FilterExprIterator()
47   {
48     super(null);
49   }
50   
51   /**
52    * Create a ChildTestIterator object.
53    *
54    * @param traverser Traverser that tells how the KeyIterator is to be handled.
55    *
56    * @throws javax.xml.transform.TransformerException
57    */

58   public FilterExprIterator(Expression expr)
59   {
60     super(null);
61     m_expr = expr;
62   }
63
64   /**
65    * Initialize the context values for this expression
66    * after it is cloned.
67    *
68    * @param execContext The XPath runtime context for this
69    * transformation.
70    */

71   public void setRoot(int context, Object JavaDoc environment)
72   {
73     super.setRoot(context, environment);
74     
75     m_exprObj = FilterExprIteratorSimple.executeFilterExpr(context,
76                       m_execContext, getPrefixResolver(),
77                       getIsTopLevel(), m_stackFrame, m_expr);
78    }
79
80
81   /**
82    * Get the next node via getNextXXX. Bottlenecked for derived class override.
83    * @return The next node on the axis, or DTM.NULL.
84    */

85   protected int getNextNode()
86   {
87     if (null != m_exprObj)
88     {
89       m_lastFetched = m_exprObj.nextNode();
90     }
91     else
92       m_lastFetched = DTM.NULL;
93
94     return m_lastFetched;
95   }
96   
97   /**
98    * Detaches the walker from the set which it iterated over, releasing
99    * any computational resources and placing the iterator in the INVALID
100    * state.
101    */

102   public void detach()
103   {
104     super.detach();
105     m_exprObj.detach();
106     m_exprObj = null;
107   }
108
109   /**
110    * This function is used to fixup variables from QNames to stack frame
111    * indexes at stylesheet build time.
112    * @param vars List of QNames that correspond to variables. This list
113    * should be searched backwards for the first qualified name that
114    * corresponds to the variable reference qname. The position of the
115    * QName in the vector from the start of the vector will be its position
116    * in the stack frame (but variables above the globalsTop value will need
117    * to be offset to the current stack frame).
118    */

119   public void fixupVariables(java.util.Vector JavaDoc vars, int globalsSize)
120   {
121     super.fixupVariables(vars, globalsSize);
122     m_expr.fixupVariables(vars, globalsSize);
123   }
124
125   /**
126    * Get the inner contained expression of this filter.
127    */

128   public Expression getInnerExpression()
129   {
130     return m_expr;
131   }
132
133   /**
134    * Set the inner contained expression of this filter.
135    */

136   public void setInnerExpression(Expression expr)
137   {
138     expr.exprSetParent(this);
139     m_expr = expr;
140   }
141
142   /**
143    * Get the analysis bits for this walker, as defined in the WalkerFactory.
144    * @return One of WalkerFactory#BIT_DESCENDANT, etc.
145    */

146   public int getAnalysisBits()
147   {
148     if (null != m_expr && m_expr instanceof PathComponent)
149     {
150       return ((PathComponent) m_expr).getAnalysisBits();
151     }
152     return WalkerFactory.BIT_FILTER;
153   }
154
155   /**
156    * Returns true if all the nodes in the iteration well be returned in document
157    * order.
158    * Warning: This can only be called after setRoot has been called!
159    *
160    * @return true as a default.
161    */

162   public boolean isDocOrdered()
163   {
164     return m_exprObj.isDocOrdered();
165   }
166
167   class filterExprOwner implements ExpressionOwner
168   {
169     /**
170     * @see ExpressionOwner#getExpression()
171     */

172     public Expression getExpression()
173     {
174       return m_expr;
175     }
176
177     /**
178      * @see ExpressionOwner#setExpression(Expression)
179      */

180     public void setExpression(Expression exp)
181     {
182       exp.exprSetParent(FilterExprIterator.this);
183       m_expr = exp;
184     }
185
186   }
187
188   /**
189    * This will traverse the heararchy, calling the visitor for
190    * each member. If the called visitor method returns
191    * false, the subtree should not be called.
192    *
193    * @param owner The owner of the visitor, where that path may be
194    * rewritten if needed.
195    * @param visitor The visitor whose appropriate method will be called.
196    */

197   public void callPredicateVisitors(XPathVisitor visitor)
198   {
199     m_expr.callVisitors(new filterExprOwner(), visitor);
200
201     super.callPredicateVisitors(visitor);
202   }
203
204   /**
205    * @see Expression#deepEquals(Expression)
206    */

207   public boolean deepEquals(Expression expr)
208   {
209     if (!super.deepEquals(expr))
210       return false;
211
212     FilterExprIterator fet = (FilterExprIterator) expr;
213     if (!m_expr.deepEquals(fet.m_expr))
214       return false;
215
216     return true;
217   }
218
219 }
220
Popular Tags