19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one
39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file
49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information
59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file
69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the  "License");
79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License.
89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at
99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *     http://www.apache.org/licenses/LICENSE-2.0
119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson *
129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software
139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and
169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License.
179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/*
199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: UnionChildIterator.java 468655 2006-10-28 07:12:06Z minchau $
209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xpath.axes;
229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTMIterator;
249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.XPathContext;
259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.objects.XObject;
269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.patterns.NodeTest;
279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/**
299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This class defines a simplified type of union iterator that only
309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * tests along the child axes.  If the conditions are right, it is
319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * much faster than using a UnionPathIterator.
329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */
339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class UnionChildIterator extends ChildTestIterator
349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson{
359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    static final long serialVersionUID = 3500298482193003495L;
369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Even though these may hold full LocPathIterators, this array does
389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * not have to be cloned, since only the node test and predicate
399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * portion are used, and these only need static information.  However,
409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * also note that index predicates can not be used!
419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  private PredicatedNodeTest[] m_nodeTests = null;
439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Constructor for UnionChildIterator
469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public UnionChildIterator()
489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    super(null);
509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Add a node test to the union list.
549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *
559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param test reference to a NodeTest, which will be added
569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * directly to the list of node tests (in other words, it will
579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * not be cloned).  The parent of this test will be set to
589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * this object.
599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public void addNodeTest(PredicatedNodeTest test)
619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // Increase array size by only 1 at a time.  Fix this
649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    // if it looks to be a problem.
659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if (null == m_nodeTests)
669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      m_nodeTests = new PredicatedNodeTest[1];
689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      m_nodeTests[0] = test;
699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    else
719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      PredicatedNodeTest[] tests = m_nodeTests;
739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      int len = m_nodeTests.length;
749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      m_nodeTests = new PredicatedNodeTest[len + 1];
769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      System.arraycopy(tests, 0, m_nodeTests, 0, len);
789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      m_nodeTests[len] = test;
809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    test.exprSetParent(this);
829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * This function is used to fixup variables from QNames to stack frame
869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * indexes at stylesheet build time.
879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param vars List of QNames that correspond to variables.  This list
889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * should be searched backwards for the first qualified name that
899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * corresponds to the variable reference qname.  The position of the
909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * QName in the vector from the start of the vector will be its position
919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * in the stack frame (but variables above the globalsTop value will need
929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * to be offset to the current stack frame).
939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public void fixupVariables(java.util.Vector vars, int globalsSize)
959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    super.fixupVariables(vars, globalsSize);
979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    if (m_nodeTests != null) {
989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      for (int i = 0; i < m_nodeTests.length; i++) {
999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        m_nodeTests[i].fixupVariables(vars, globalsSize);
1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      }
1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  /**
1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * Test whether a specified node is visible in the logical view of a
1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * TreeWalker or NodeIterator. This function will be called by the
1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * implementation of TreeWalker and NodeIterator; it is not intended to
1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * be called directly from user code.
1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @param n  The node to check to see if it passes the filter or not.
1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   * @return  a constant to determine whether the node is accepted,
1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   *   rejected, or skipped, as defined  above .
1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson   */
1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  public short acceptNode(int n)
1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  {
1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    XPathContext xctxt = getXPathContext();
1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    try
1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      xctxt.pushCurrentNode(n);
1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      for (int i = 0; i < m_nodeTests.length; i++)
1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      {
1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        PredicatedNodeTest pnt = m_nodeTests[i];
1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        XObject score = pnt.execute(xctxt, n);
1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        if (score != NodeTest.SCORE_NONE)
1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        {
1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          // Note that we are assuming there are no positional predicates!
1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          if (pnt.getPredicateCount() > 0)
1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          {
1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            if (pnt.executePredicates(n, xctxt))
1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson              return DTMIterator.FILTER_ACCEPT;
1309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          }
1319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson          else
1329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson            return DTMIterator.FILTER_ACCEPT;
1339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson        }
1359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      }
1369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    catch (javax.xml.transform.TransformerException se)
1389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
1399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      // TODO: Fix this.
1419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      throw new RuntimeException(se.getMessage());
1429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    finally
1449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    {
1459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson      xctxt.popCurrentNode();
1469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    }
1479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson    return DTMIterator.FILTER_SKIP;
1489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson  }
1499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson
1509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson}
151