001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 /*
019 * $Id: ChildIterator.java 468655 2006-10-28 07:12:06Z minchau $
020 */
021 package org.apache.xpath.axes;
022
023 import org.apache.xml.dtm.DTM;
024 import org.apache.xml.dtm.DTMFilter;
025 import org.apache.xpath.XPathContext;
026 import org.apache.xpath.compiler.Compiler;
027
028 /**
029 * This class implements an optimized iterator for
030 * "node()" patterns, that is, any children of the
031 * context node.
032 * @see org.apache.xpath.axes.LocPathIterator
033 * @xsl.usage advanced
034 */
035 public class ChildIterator extends LocPathIterator
036 {
037 static final long serialVersionUID = -6935428015142993583L;
038
039 /**
040 * Create a ChildIterator object.
041 *
042 * @param compiler A reference to the Compiler that contains the op map.
043 * @param opPos The position within the op map, which contains the
044 * location path expression for this itterator.
045 * @param analysis Analysis bits of the entire pattern.
046 *
047 * @throws javax.xml.transform.TransformerException
048 */
049 ChildIterator(Compiler compiler, int opPos, int analysis)
050 throws javax.xml.transform.TransformerException
051 {
052 super(compiler, opPos, analysis, false);
053
054 // This iterator matches all kinds of nodes
055 initNodeTest(DTMFilter.SHOW_ALL);
056 }
057
058 /**
059 * Return the first node out of the nodeset, if this expression is
060 * a nodeset expression. This is the default implementation for
061 * nodesets.
062 * <p>WARNING: Do not mutate this class from this function!</p>
063 * @param xctxt The XPath runtime context.
064 * @return the first node out of the nodeset, or DTM.NULL.
065 */
066 public int asNode(XPathContext xctxt)
067 throws javax.xml.transform.TransformerException
068 {
069 int current = xctxt.getCurrentNode();
070
071 DTM dtm = xctxt.getDTM(current);
072
073 return dtm.getFirstChild(current);
074 }
075
076 /**
077 * Returns the next node in the set and advances the position of the
078 * iterator in the set. After a NodeIterator is created, the first call
079 * to nextNode() returns the first node in the set.
080 *
081 * @return The next <code>Node</code> in the set being iterated over, or
082 * <code>null</code> if there are no more members in that set.
083 */
084 public int nextNode()
085 {
086 if(m_foundLast)
087 return DTM.NULL;
088
089 int next;
090
091 m_lastFetched = next = (DTM.NULL == m_lastFetched)
092 ? m_cdtm.getFirstChild(m_context)
093 : m_cdtm.getNextSibling(m_lastFetched);
094
095 // m_lastFetched = next;
096 if (DTM.NULL != next)
097 {
098 m_pos++;
099 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 }