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: ChildIterator.java 468655 2006-10-28 07:12:06Z minchau $
20 */
21package org.apache.xpath.axes;
22
23import org.apache.xml.dtm.DTM;
24import org.apache.xml.dtm.DTMFilter;
25import org.apache.xpath.XPathContext;
26import org.apache.xpath.compiler.Compiler;
27
28/**
29 * This class implements an optimized iterator for
30 * "node()" patterns, that is, any children of the
31 * context node.
32 * @see org.apache.xpath.axes.LocPathIterator
33 * @xsl.usage advanced
34 */
35public class ChildIterator extends LocPathIterator
36{
37    static final long serialVersionUID = -6935428015142993583L;
38
39  /**
40   * Create a ChildIterator object.
41   *
42   * @param compiler A reference to the Compiler that contains the op map.
43   * @param opPos The position within the op map, which contains the
44   * location path expression for this itterator.
45   * @param analysis Analysis bits of the entire pattern.
46   *
47   * @throws javax.xml.transform.TransformerException
48   */
49  ChildIterator(Compiler compiler, int opPos, int analysis)
50          throws javax.xml.transform.TransformerException
51  {
52    super(compiler, opPos, analysis, false);
53
54    // This iterator matches all kinds of nodes
55    initNodeTest(DTMFilter.SHOW_ALL);
56  }
57
58  /**
59   * Return the first node out of the nodeset, if this expression is
60   * a nodeset expression.  This is the default implementation for
61   * nodesets.
62   * <p>WARNING: Do not mutate this class from this function!</p>
63   * @param xctxt The XPath runtime context.
64   * @return the first node out of the nodeset, or DTM.NULL.
65   */
66  public int asNode(XPathContext xctxt)
67    throws javax.xml.transform.TransformerException
68  {
69    int current = xctxt.getCurrentNode();
70
71    DTM dtm = xctxt.getDTM(current);
72
73    return dtm.getFirstChild(current);
74  }
75
76  /**
77   *  Returns the next node in the set and advances the position of the
78   * iterator in the set. After a NodeIterator is created, the first call
79   * to nextNode() returns the first node in the set.
80   *
81   * @return  The next <code>Node</code> in the set being iterated over, or
82   *   <code>null</code> if there are no more members in that set.
83   */
84  public int nextNode()
85  {
86  	if(m_foundLast)
87  		return DTM.NULL;
88
89    int next;
90
91    m_lastFetched = next = (DTM.NULL == m_lastFetched)
92                           ? m_cdtm.getFirstChild(m_context)
93                           : m_cdtm.getNextSibling(m_lastFetched);
94
95    // m_lastFetched = next;
96    if (DTM.NULL != next)
97    {
98      m_pos++;
99      return next;
100    }
101    else
102    {
103      m_foundLast = true;
104
105      return DTM.NULL;
106    }
107  }
108
109  /**
110   * Returns the axis being iterated, if it is known.
111   *
112   * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
113   * types.
114   */
115  public int getAxis()
116  {
117    return org.apache.xml.dtm.Axis.CHILD;
118  }
119
120
121}
122