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