KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xpath > internal > axes > OneStepIterator


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

19 package com.sun.org.apache.xpath.internal.axes;
20
21 import com.sun.org.apache.xml.internal.dtm.DTM;
22 import com.sun.org.apache.xml.internal.dtm.DTMAxisIterator;
23 import com.sun.org.apache.xml.internal.dtm.DTMFilter;
24 import com.sun.org.apache.xml.internal.dtm.DTMIterator;
25 import com.sun.org.apache.xpath.internal.Expression;
26 import com.sun.org.apache.xpath.internal.XPathContext;
27 import com.sun.org.apache.xpath.internal.compiler.Compiler;
28
29 /**
30  * This class implements a general iterator for
31  * those LocationSteps with only one step, and perhaps a predicate.
32  * @see com.sun.org.apache.xpath.internal.axes#LocPathIterator
33  * @xsl.usage advanced
34  */

35 public class OneStepIterator extends ChildTestIterator
36 {
37   /** The traversal axis from where the nodes will be filtered. */
38   protected int m_axis = -1;
39
40   /** The DTM inner traversal class, that corresponds to the super axis. */
41   protected DTMAxisIterator m_iterator;
42
43   /**
44    * Create a OneStepIterator object.
45    *
46    * @param compiler A reference to the Compiler that contains the op map.
47    * @param opPos The position within the op map, which contains the
48    * location path expression for this itterator.
49    *
50    * @throws javax.xml.transform.TransformerException
51    */

52   OneStepIterator(Compiler JavaDoc compiler, int opPos, int analysis)
53           throws javax.xml.transform.TransformerException JavaDoc
54   {
55     super(compiler, opPos, analysis);
56     int firstStepPos = compiler.getFirstChildPos(opPos);
57     
58     m_axis = WalkerFactory.getAxisFromStep(compiler, firstStepPos);
59     
60   }
61   
62   
63   /**
64    * Create a OneStepIterator object.
65    *
66    * @param iterator The DTM iterator which this iterator will use.
67    * @param axis One of Axis.Child, etc., or -1 if the axis is unknown.
68    *
69    * @throws javax.xml.transform.TransformerException
70    */

71   public OneStepIterator(DTMAxisIterator iterator, int axis)
72           throws javax.xml.transform.TransformerException JavaDoc
73   {
74     super(null);
75     
76     m_iterator = iterator;
77     m_axis = axis;
78     int whatToShow = DTMFilter.SHOW_ALL;
79     initNodeTest(whatToShow);
80   }
81   
82   /**
83    * Initialize the context values for this expression
84    * after it is cloned.
85    *
86    * @param execContext The XPath runtime context for this
87    * transformation.
88    */

89   public void setRoot(int context, Object JavaDoc environment)
90   {
91     super.setRoot(context, environment);
92     if(m_axis > -1)
93       m_iterator = m_cdtm.getAxisIterator(m_axis);
94     m_iterator.setStartNode(m_context);
95   }
96
97   /**
98    * Detaches the iterator from the set which it iterated over, releasing
99    * any computational resources and placing the iterator in the INVALID
100    * state. After<code>detach</code> has been invoked, calls to
101    * <code>nextNode</code> or<code>previousNode</code> will raise the
102    * exception INVALID_STATE_ERR.
103    */

104   public void detach()
105   {
106     if(m_allowDetach)
107     {
108       if(m_axis > -1)
109         m_iterator = null;
110       
111       // Always call the superclass detach last!
112
super.detach();
113     }
114   }
115   
116   /**
117    * Get the next node via getFirstAttribute && getNextAttribute.
118    */

119   protected int getNextNode()
120   {
121     return m_lastFetched = m_iterator.next();
122   }
123   
124   /**
125    * Get a cloned iterator.
126    *
127    * @return A new iterator that can be used without mutating this one.
128    *
129    * @throws CloneNotSupportedException
130    */

131   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
132   {
133     // Do not access the location path itterator during this operation!
134

135     OneStepIterator clone = (OneStepIterator) super.clone();
136
137     if(m_iterator != null)
138     {
139       clone.m_iterator = m_iterator.cloneIterator();
140     }
141     return clone;
142   }
143   
144   /**
145    * Get a cloned Iterator that is reset to the beginning
146    * of the query.
147    *
148    * @return A cloned NodeIterator set of the start of the query.
149    *
150    * @throws CloneNotSupportedException
151    */

152   public DTMIterator cloneWithReset() throws CloneNotSupportedException JavaDoc
153   {
154
155     OneStepIterator clone = (OneStepIterator) super.cloneWithReset();
156     clone.m_iterator = m_iterator;
157
158     return clone;
159   }
160
161
162
163   /**
164    * Tells if this is a reverse axes. Overrides AxesWalker#isReverseAxes.
165    *
166    * @return true for this class.
167    */

168   public boolean isReverseAxes()
169   {
170     return m_iterator.isReverse();
171   }
172
173   /**
174    * Get the current sub-context position. In order to do the
175    * reverse axes count, for the moment this re-searches the axes
176    * up to the predicate. An optimization on this is to cache
177    * the nodes searched, but, for the moment, this case is probably
178    * rare enough that the added complexity isn't worth it.
179    *
180    * @param predicateIndex The predicate index of the proximity position.
181    *
182    * @return The pridicate index, or -1.
183    */

184   protected int getProximityPosition(int predicateIndex)
185   {
186     if(!isReverseAxes())
187       return super.getProximityPosition(predicateIndex);
188       
189     // A negative predicate index seems to occur with
190
// (preceding-sibling::*|following-sibling::*)/ancestor::*[position()]/*[position()]
191
// -sb
192
if(predicateIndex < 0)
193       return -1;
194       
195     if (m_proximityPositions[predicateIndex] <= 0)
196     {
197       XPathContext xctxt = getXPathContext();
198       try
199       {
200         OneStepIterator clone = (OneStepIterator) this.clone();
201         
202         int root = getRoot();
203         xctxt.pushCurrentNode(root);
204         clone.setRoot(root, xctxt);
205
206         // clone.setPredicateCount(predicateIndex);
207
clone.m_predCount = predicateIndex;
208
209         // Count 'em all
210
int count = 1;
211         int next;
212
213         while (DTM.NULL != (next = clone.nextNode()))
214         {
215           count++;
216         }
217
218         m_proximityPositions[predicateIndex] += count;
219       }
220       catch (CloneNotSupportedException JavaDoc cnse)
221       {
222
223         // can't happen
224
}
225       finally
226       {
227         xctxt.popCurrentNode();
228       }
229     }
230
231     return m_proximityPositions[predicateIndex];
232   }
233
234   /**
235    * The number of nodes in the list. The range of valid child node indices
236    * is 0 to <code>length-1</code> inclusive.
237    *
238    * @return The number of nodes in the list, always greater or equal to zero.
239    */

240   public int getLength()
241   {
242     if(!isReverseAxes())
243       return super.getLength();
244       
245     // Tell if this is being called from within a predicate.
246
boolean isPredicateTest = (this == m_execContext.getSubContextList());
247
248     // And get how many total predicates are part of this step.
249
int predCount = getPredicateCount();
250    
251     // If we have already calculated the length, and the current predicate
252
// is the first predicate, then return the length. We don't cache
253
// the anything but the length of the list to the first predicate.
254
if (-1 != m_length && isPredicateTest && m_predicateIndex < 1)
255        return m_length;
256
257     int count = 0;
258     
259     XPathContext xctxt = getXPathContext();
260     try
261     {
262       OneStepIterator clone = (OneStepIterator) this.cloneWithReset();
263       
264       int root = getRoot();
265       xctxt.pushCurrentNode(root);
266       clone.setRoot(root, xctxt);
267  
268       clone.m_predCount = m_predicateIndex;
269
270       int next;
271
272       while (DTM.NULL != (next = clone.nextNode()))
273       {
274         count++;
275       }
276     }
277     catch (CloneNotSupportedException JavaDoc cnse)
278     {
279        // can't happen
280
}
281     finally
282     {
283       xctxt.popCurrentNode();
284     }
285     if (isPredicateTest && m_predicateIndex < 1)
286       m_length = count;
287       
288     return count;
289   }
290
291   /**
292    * Count backwards one proximity position.
293    *
294    * @param i The predicate index.
295    */

296   protected void countProximityPosition(int i)
297   {
298     if(!isReverseAxes())
299       super.countProximityPosition(i);
300     else if (i < m_proximityPositions.length)
301       m_proximityPositions[i]--;
302   }
303   
304   /**
305    * Reset the iterator.
306    */

307   public void reset()
308   {
309
310     super.reset();
311     if(null != m_iterator)
312       m_iterator.reset();
313   }
314   
315   /**
316    * Returns the axis being iterated, if it is known.
317    *
318    * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
319    * types.
320    */

321   public int getAxis()
322   {
323     return m_axis;
324   }
325   
326   /**
327    * @see Expression#deepEquals(Expression)
328    */

329   public boolean deepEquals(Expression expr)
330   {
331     if(!super.deepEquals(expr))
332         return false;
333         
334     if(m_axis != ((OneStepIterator)expr).m_axis)
335         return false;
336         
337     return true;
338   }
339
340   
341 }
342
Popular Tags