1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the  "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18/*
19 * $Id: ChildTestIterator.java 468655 2006-10-28 07:12:06Z minchau $
20 */
21package org.apache.xpath.axes;
22
23import org.apache.xml.dtm.Axis;
24import org.apache.xml.dtm.DTM;
25import org.apache.xml.dtm.DTMAxisTraverser;
26import org.apache.xml.dtm.DTMIterator;
27import org.apache.xpath.compiler.Compiler;
28
29/**
30 * This class implements an optimized iterator for
31 * children patterns that have a node test, and possibly a predicate.
32 * @see org.apache.xpath.axes.BasicTestIterator
33 * @xsl.usage advanced
34 */
35public class ChildTestIterator extends BasicTestIterator
36{
37    static final long serialVersionUID = -7936835957960705722L;
38  /** The traverser to use to navigate over the descendants. */
39  transient protected DTMAxisTraverser m_traverser;
40
41  /** The extended type ID, not set until setRoot. */
42//  protected int m_extendedTypeID;
43
44
45  /**
46   * Create a ChildTestIterator object.
47   *
48   * @param compiler A reference to the Compiler that contains the op map.
49   * @param opPos The position within the op map, which contains the
50   * location path expression for this itterator.
51   *
52   * @throws javax.xml.transform.TransformerException
53   */
54  ChildTestIterator(Compiler compiler, int opPos, int analysis)
55          throws javax.xml.transform.TransformerException
56  {
57    super(compiler, opPos, analysis);
58  }
59
60  /**
61   * Create a ChildTestIterator object.
62   *
63   * @param traverser Traverser that tells how the KeyIterator is to be handled.
64   *
65   * @throws javax.xml.transform.TransformerException
66   */
67  public ChildTestIterator(DTMAxisTraverser traverser)
68  {
69
70    super(null);
71
72    m_traverser = traverser;
73  }
74
75  /**
76   * Get the next node via getNextXXX.  Bottlenecked for derived class override.
77   * @return The next node on the axis, or DTM.NULL.
78   */
79  protected int getNextNode()
80  {
81    if(true /* 0 == m_extendedTypeID */)
82    {
83      m_lastFetched = (DTM.NULL == m_lastFetched)
84                   ? m_traverser.first(m_context)
85                   : m_traverser.next(m_context, m_lastFetched);
86    }
87//    else
88//    {
89//      m_lastFetched = (DTM.NULL == m_lastFetched)
90//                   ? m_traverser.first(m_context, m_extendedTypeID)
91//                   : m_traverser.next(m_context, m_lastFetched,
92//                                      m_extendedTypeID);
93//    }
94
95    return m_lastFetched;
96  }
97
98
99  /**
100   *  Get a cloned Iterator that is reset to the beginning
101   *  of the query.
102   *
103   *  @return A cloned NodeIterator set of the start of the query.
104   *
105   *  @throws CloneNotSupportedException
106   */
107  public DTMIterator cloneWithReset() throws CloneNotSupportedException
108  {
109
110    ChildTestIterator clone = (ChildTestIterator) super.cloneWithReset();
111    clone.m_traverser = m_traverser;
112
113    return clone;
114  }
115
116
117  /**
118   * Initialize the context values for this expression
119   * after it is cloned.
120   *
121   * @param context The XPath runtime context for this
122   * transformation.
123   */
124  public void setRoot(int context, Object environment)
125  {
126    super.setRoot(context, environment);
127    m_traverser = m_cdtm.getAxisTraverser(Axis.CHILD);
128
129//    String localName = getLocalName();
130//    String namespace = getNamespace();
131//    int what = m_whatToShow;
132//    // System.out.println("what: ");
133//    // NodeTest.debugWhatToShow(what);
134//    if(DTMFilter.SHOW_ALL == what ||
135//       ((DTMFilter.SHOW_ELEMENT & what) == 0)
136//       || localName == NodeTest.WILD
137//       || namespace == NodeTest.WILD)
138//    {
139//      m_extendedTypeID = 0;
140//    }
141//    else
142//    {
143//      int type = getNodeTypeTest(what);
144//      m_extendedTypeID = m_cdtm.getExpandedTypeID(namespace, localName, type);
145//    }
146
147  }
148
149  /**
150   * Returns the axis being iterated, if it is known.
151   *
152   * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
153   * types.
154   */
155  public int getAxis()
156  {
157    return org.apache.xml.dtm.Axis.CHILD;
158  }
159
160  /**
161   *  Detaches the iterator from the set which it iterated over, releasing
162   * any computational resources and placing the iterator in the INVALID
163   * state. After<code>detach</code> has been invoked, calls to
164   * <code>nextNode</code> or<code>previousNode</code> will raise the
165   * exception INVALID_STATE_ERR.
166   */
167  public void detach()
168  {
169    if(m_allowDetach)
170    {
171      m_traverser = null;
172
173      // Always call the superclass detach last!
174      super.detach();
175    }
176  }
177
178}
179