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: UnionChildIterator.java,v 1.6 2004/08/17 19:25:34 jycli Exp $
018: */
019: package org.apache.xpath.axes;
020:
021: import org.apache.xml.dtm.DTMIterator;
022: import org.apache.xpath.XPathContext;
023: import org.apache.xpath.objects.XObject;
024: import org.apache.xpath.patterns.NodeTest;
025:
026: /**
027: * This class defines a simplified type of union iterator that only
028: * tests along the child axes. If the conditions are right, it is
029: * much faster than using a UnionPathIterator.
030: */
031: public class UnionChildIterator extends ChildTestIterator {
032: static final long serialVersionUID = 3500298482193003495L;
033: /**
034: * Even though these may hold full LocPathIterators, this array does
035: * not have to be cloned, since only the node test and predicate
036: * portion are used, and these only need static information. However,
037: * also note that index predicates can not be used!
038: */
039: private PredicatedNodeTest[] m_nodeTests = null;
040:
041: /**
042: * Constructor for UnionChildIterator
043: */
044: public UnionChildIterator() {
045: super (null);
046: }
047:
048: /**
049: * Add a node test to the union list.
050: *
051: * @param test reference to a NodeTest, which will be added
052: * directly to the list of node tests (in other words, it will
053: * not be cloned). The parent of this test will be set to
054: * this object.
055: */
056: public void addNodeTest(PredicatedNodeTest test) {
057:
058: // Increase array size by only 1 at a time. Fix this
059: // if it looks to be a problem.
060: if (null == m_nodeTests) {
061: m_nodeTests = new PredicatedNodeTest[1];
062: m_nodeTests[0] = test;
063: } else {
064: PredicatedNodeTest[] tests = m_nodeTests;
065: int len = m_nodeTests.length;
066:
067: m_nodeTests = new PredicatedNodeTest[len + 1];
068:
069: System.arraycopy(tests, 0, m_nodeTests, 0, len);
070:
071: m_nodeTests[len] = test;
072: }
073: test.exprSetParent(this );
074: }
075:
076: /**
077: * This function is used to fixup variables from QNames to stack frame
078: * indexes at stylesheet build time.
079: * @param vars List of QNames that correspond to variables. This list
080: * should be searched backwards for the first qualified name that
081: * corresponds to the variable reference qname. The position of the
082: * QName in the vector from the start of the vector will be its position
083: * in the stack frame (but variables above the globalsTop value will need
084: * to be offset to the current stack frame).
085: */
086: public void fixupVariables(java.util.Vector vars, int globalsSize) {
087: super .fixupVariables(vars, globalsSize);
088: if (m_nodeTests != null) {
089: for (int i = 0; i < m_nodeTests.length; i++) {
090: m_nodeTests[i].fixupVariables(vars, globalsSize);
091: }
092: }
093: }
094:
095: /**
096: * Test whether a specified node is visible in the logical view of a
097: * TreeWalker or NodeIterator. This function will be called by the
098: * implementation of TreeWalker and NodeIterator; it is not intended to
099: * be called directly from user code.
100: * @param n The node to check to see if it passes the filter or not.
101: * @return a constant to determine whether the node is accepted,
102: * rejected, or skipped, as defined above .
103: */
104: public short acceptNode(int n) {
105: XPathContext xctxt = getXPathContext();
106: try {
107: xctxt.pushCurrentNode(n);
108: for (int i = 0; i < m_nodeTests.length; i++) {
109: PredicatedNodeTest pnt = m_nodeTests[i];
110: XObject score = pnt.execute(xctxt, n);
111: if (score != NodeTest.SCORE_NONE) {
112: // Note that we are assuming there are no positional predicates!
113: if (pnt.getPredicateCount() > 0) {
114: if (pnt.executePredicates(n, xctxt))
115: return DTMIterator.FILTER_ACCEPT;
116: } else
117: return DTMIterator.FILTER_ACCEPT;
118:
119: }
120: }
121: } catch (javax.xml.transform.TransformerException se) {
122:
123: // TODO: Fix this.
124: throw new RuntimeException(se.getMessage());
125: } finally {
126: xctxt.popCurrentNode();
127: }
128: return DTMIterator.FILTER_SKIP;
129: }
130:
131: }
|