001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: OneStepIteratorForward.java,v 1.12 2005/01/23 01:02:11 mcnamara Exp $
018: */
019: package org.apache.xpath.axes;
020:
021: import org.apache.xml.dtm.DTM;
022: import org.apache.xml.dtm.DTMFilter;
023: import org.apache.xpath.Expression;
024: import org.apache.xpath.compiler.Compiler;
025:
026: /**
027: * This class implements a general iterator for
028: * those LocationSteps with only one step, and perhaps a predicate,
029: * that only go forward (i.e. it can not be used with ancestors,
030: * preceding, etc.)
031: * @see org.apache.xpath.axes#ChildTestIterator
032: * @xsl.usage advanced
033: */
034: public class OneStepIteratorForward extends ChildTestIterator {
035: static final long serialVersionUID = -1576936606178190566L;
036: /** The traversal axis from where the nodes will be filtered. */
037: protected int m_axis = -1;
038:
039: /**
040: * Create a OneStepIterator 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: *
046: * @throws javax.xml.transform.TransformerException
047: */
048: OneStepIteratorForward(Compiler compiler, int opPos, int analysis)
049: throws javax.xml.transform.TransformerException {
050: super (compiler, opPos, analysis);
051: int firstStepPos = compiler.getFirstChildPos(opPos);
052:
053: m_axis = WalkerFactory.getAxisFromStep(compiler, firstStepPos);
054:
055: }
056:
057: /**
058: * Create a OneStepIterator object that will just traverse the self axes.
059: *
060: * @param axis One of the org.apache.xml.dtm.Axis integers.
061: *
062: * @throws javax.xml.transform.TransformerException
063: */
064: public OneStepIteratorForward(int axis) {
065: super (null);
066:
067: m_axis = axis;
068: int whatToShow = DTMFilter.SHOW_ALL;
069: initNodeTest(whatToShow);
070: }
071:
072: /**
073: * Initialize the context values for this expression
074: * after it is cloned.
075: *
076: * @param context The XPath runtime context for this
077: * transformation.
078: */
079: public void setRoot(int context, Object environment) {
080: super .setRoot(context, environment);
081: m_traverser = m_cdtm.getAxisTraverser(m_axis);
082:
083: }
084:
085: // /**
086: // * Return the first node out of the nodeset, if this expression is
087: // * a nodeset expression. This is the default implementation for
088: // * nodesets.
089: // * <p>WARNING: Do not mutate this class from this function!</p>
090: // * @param xctxt The XPath runtime context.
091: // * @return the first node out of the nodeset, or DTM.NULL.
092: // */
093: // public int asNode(XPathContext xctxt)
094: // throws javax.xml.transform.TransformerException
095: // {
096: // if(getPredicateCount() > 0)
097: // return super.asNode(xctxt);
098: //
099: // int current = xctxt.getCurrentNode();
100: //
101: // DTM dtm = xctxt.getDTM(current);
102: // DTMAxisTraverser traverser = dtm.getAxisTraverser(m_axis);
103: //
104: // String localName = getLocalName();
105: // String namespace = getNamespace();
106: // int what = m_whatToShow;
107: //
108: // // System.out.println("what: ");
109: // // NodeTest.debugWhatToShow(what);
110: // if(DTMFilter.SHOW_ALL == what
111: // || ((DTMFilter.SHOW_ELEMENT & what) == 0)
112: // || localName == NodeTest.WILD
113: // || namespace == NodeTest.WILD)
114: // {
115: // return traverser.first(current);
116: // }
117: // else
118: // {
119: // int type = getNodeTypeTest(what);
120: // int extendedType = dtm.getExpandedTypeID(namespace, localName, type);
121: // return traverser.first(current, extendedType);
122: // }
123: // }
124:
125: /**
126: * Get the next node via getFirstAttribute && getNextAttribute.
127: */
128: protected int getNextNode() {
129: m_lastFetched = (DTM.NULL == m_lastFetched) ? m_traverser
130: .first(m_context) : m_traverser.next(m_context,
131: m_lastFetched);
132: return m_lastFetched;
133: }
134:
135: /**
136: * Returns the axis being iterated, if it is known.
137: *
138: * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
139: * types.
140: */
141: public int getAxis() {
142: return m_axis;
143: }
144:
145: /**
146: * @see Expression#deepEquals(Expression)
147: */
148: public boolean deepEquals(Expression expr) {
149: if (!super .deepEquals(expr))
150: return false;
151:
152: if (m_axis != ((OneStepIteratorForward) expr).m_axis)
153: return false;
154:
155: return true;
156: }
157:
158: }
|