KickJava   Java API By Example, From Geeks To Geeks.

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


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: BasicTestIterator.java,v 1.6 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.xml.dtm.DTMFilter;
23 import org.apache.xml.dtm.DTMIterator;
24 import org.apache.xml.utils.PrefixResolver;
25 import org.apache.xpath.compiler.Compiler;
26
27 /**
28  * Base for iterators that handle predicates. Does the basic next
29  * node logic, so all the derived iterator has to do is get the
30  * next node.
31  */

32 public abstract class BasicTestIterator extends LocPathIterator
33 {
34   /**
35    * Create a LocPathIterator object.
36    *
37    * @param nscontext The namespace context for this iterator,
38    * should be OK if null.
39    */

40   protected BasicTestIterator()
41   {
42   }
43
44
45   /**
46    * Create a LocPathIterator object.
47    *
48    * @param nscontext The namespace context for this iterator,
49    * should be OK if null.
50    */

51   protected BasicTestIterator(PrefixResolver nscontext)
52   {
53
54     super(nscontext);
55   }
56
57   /**
58    * Create a LocPathIterator object, including creation
59    * of step walkers from the opcode list, and call back
60    * into the Compiler to create predicate expressions.
61    *
62    * @param compiler The Compiler which is creating
63    * this expression.
64    * @param opPos The position of this iterator in the
65    * opcode list from the compiler.
66    *
67    * @throws javax.xml.transform.TransformerException
68    */

69   protected BasicTestIterator(Compiler JavaDoc compiler, int opPos, int analysis)
70           throws javax.xml.transform.TransformerException JavaDoc
71   {
72     super(compiler, opPos, analysis, false);
73     
74     int firstStepPos = compiler.getFirstChildPos(opPos);
75     int whatToShow = compiler.getWhatToShow(firstStepPos);
76
77     if ((0 == (whatToShow
78                & (DTMFilter.SHOW_ATTRIBUTE
79                | DTMFilter.SHOW_NAMESPACE
80                | DTMFilter.SHOW_ELEMENT
81                | DTMFilter.SHOW_PROCESSING_INSTRUCTION)))
82                || (whatToShow == DTMFilter.SHOW_ALL))
83       initNodeTest(whatToShow);
84     else
85     {
86       initNodeTest(whatToShow, compiler.getStepNS(firstStepPos),
87                               compiler.getStepLocalName(firstStepPos));
88     }
89     initPredicateInfo(compiler, firstStepPos);
90   }
91
92   /**
93    * Create a LocPathIterator object, including creation
94    * of step walkers from the opcode list, and call back
95    * into the Compiler to create predicate expressions.
96    *
97    * @param compiler The Compiler which is creating
98    * this expression.
99    * @param opPos The position of this iterator in the
100    * opcode list from the compiler.
101    * @param shouldLoadWalkers True if walkers should be
102    * loaded, or false if this is a derived iterator and
103    * it doesn't wish to load child walkers.
104    *
105    * @throws javax.xml.transform.TransformerException
106    */

107   protected BasicTestIterator(
108           Compiler JavaDoc compiler, int opPos, int analysis, boolean shouldLoadWalkers)
109             throws javax.xml.transform.TransformerException JavaDoc
110   {
111     super(compiler, opPos, analysis, shouldLoadWalkers);
112   }
113
114     
115   /**
116    * Get the next node via getNextXXX. Bottlenecked for derived class override.
117    * @return The next node on the axis, or DTM.NULL.
118    */

119   protected abstract int getNextNode();
120
121   /**
122    * Returns the next node in the set and advances the position of the
123    * iterator in the set. After a NodeIterator is created, the first call
124    * to nextNode() returns the first node in the set.
125    *
126    * @return The next <code>Node</code> in the set being iterated over, or
127    * <code>null</code> if there are no more members in that set.
128    */

129   public int nextNode()
130   {
131     if(m_foundLast)
132     {
133         m_lastFetched = DTM.NULL;
134         return DTM.NULL;
135     }
136         
137     if(DTM.NULL == m_lastFetched)
138     {
139       resetProximityPositions();
140     }
141
142     int next;
143     
144     org.apache.xpath.VariableStack vars;
145     int savedStart;
146     if (-1 != m_stackFrame)
147     {
148       vars = m_execContext.getVarStack();
149
150       // These three statements need to be combined into one operation.
151
savedStart = vars.getStackFrame();
152
153       vars.setStackFrame(m_stackFrame);
154     }
155     else
156     {
157       // Yuck. Just to shut up the compiler!
158
vars = null;
159       savedStart = 0;
160     }
161     
162     try
163     {
164       do
165       {
166         next = getNextNode();
167   
168         if (DTM.NULL != next)
169         {
170           if(DTMIterator.FILTER_ACCEPT == acceptNode(next))
171             break;
172           else
173             continue;
174         }
175         else
176           break;
177       }
178       while (next != DTM.NULL);
179   
180       if (DTM.NULL != next)
181       {
182         m_pos++;
183         return next;
184       }
185       else
186       {
187         m_foundLast = true;
188   
189         return DTM.NULL;
190       }
191     }
192     finally
193     {
194       if (-1 != m_stackFrame)
195       {
196         // These two statements need to be combined into one operation.
197
vars.setStackFrame(savedStart);
198       }
199     }
200   }
201   
202   /**
203    * Get a cloned Iterator that is reset to the beginning
204    * of the query.
205    *
206    * @return A cloned NodeIterator set of the start of the query.
207    *
208    * @throws CloneNotSupportedException
209    */

210   public DTMIterator cloneWithReset() throws CloneNotSupportedException JavaDoc
211   {
212
213     ChildTestIterator clone = (ChildTestIterator) super.cloneWithReset();
214
215     clone.resetProximityPositions();
216
217     return clone;
218   }
219
220
221 }
222
223
Popular Tags