1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xpath.axes;
  58. import org.apache.xml.dtm.DTMIterator;
  59. import org.apache.xpath.XPathContext;
  60. import org.apache.xpath.objects.XObject;
  61. import org.apache.xpath.patterns.NodeTest;
  62. /**
  63. * This class defines a simplified type of union iterator that only
  64. * tests along the child axes. If the conditions are right, it is
  65. * much faster than using a UnionPathIterator.
  66. */
  67. public class UnionChildIterator extends ChildTestIterator
  68. {
  69. /**
  70. * Even though these may hold full LocPathIterators, this array does
  71. * not have to be cloned, since only the node test and predicate
  72. * portion are used, and these only need static information. However,
  73. * also note that index predicates can not be used!
  74. */
  75. private PredicatedNodeTest[] m_nodeTests = null;
  76. /**
  77. * Constructor for UnionChildIterator
  78. */
  79. public UnionChildIterator()
  80. {
  81. super(null);
  82. }
  83. /**
  84. * Add a node test to the union list.
  85. *
  86. * @param test reference to a NodeTest, which will be added
  87. * directly to the list of node tests (in other words, it will
  88. * not be cloned). The parent of this test will be set to
  89. * this object.
  90. */
  91. public void addNodeTest(PredicatedNodeTest test)
  92. {
  93. // Increase array size by only 1 at a time. Fix this
  94. // if it looks to be a problem.
  95. if (null == m_nodeTests)
  96. {
  97. m_nodeTests = new PredicatedNodeTest[1];
  98. m_nodeTests[0] = test;
  99. }
  100. else
  101. {
  102. PredicatedNodeTest[] tests = m_nodeTests;
  103. int len = m_nodeTests.length;
  104. m_nodeTests = new PredicatedNodeTest[len + 1];
  105. System.arraycopy(tests, 0, m_nodeTests, 0, len);
  106. m_nodeTests[len] = test;
  107. }
  108. test.exprSetParent(this);
  109. }
  110. /**
  111. * Test whether a specified node is visible in the logical view of a
  112. * TreeWalker or NodeIterator. This function will be called by the
  113. * implementation of TreeWalker and NodeIterator; it is not intended to
  114. * be called directly from user code.
  115. * @param n The node to check to see if it passes the filter or not.
  116. * @return a constant to determine whether the node is accepted,
  117. * rejected, or skipped, as defined above .
  118. */
  119. public short acceptNode(int n)
  120. {
  121. XPathContext xctxt = getXPathContext();
  122. try
  123. {
  124. xctxt.pushCurrentNode(n);
  125. for (int i = 0; i < m_nodeTests.length; i++)
  126. {
  127. PredicatedNodeTest pnt = m_nodeTests[i];
  128. XObject score = pnt.execute(xctxt, n);
  129. if (score != NodeTest.SCORE_NONE)
  130. {
  131. // Note that we are assuming there are no positional predicates!
  132. if (pnt.getPredicateCount() > 0)
  133. {
  134. if (pnt.executePredicates(n, xctxt))
  135. return DTMIterator.FILTER_ACCEPT;
  136. }
  137. else
  138. return DTMIterator.FILTER_ACCEPT;
  139. }
  140. }
  141. }
  142. catch (javax.xml.transform.TransformerException se)
  143. {
  144. // TODO: Fix this.
  145. throw new RuntimeException(se.getMessage());
  146. }
  147. finally
  148. {
  149. xctxt.popCurrentNode();
  150. }
  151. return DTMIterator.FILTER_SKIP;
  152. }
  153. }