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.xpath.XPathContext;
  59. import org.apache.xml.utils.PrefixResolver;
  60. import org.apache.xpath.compiler.Compiler;
  61. import org.apache.xml.dtm.DTM;
  62. import org.apache.xml.dtm.DTMIterator;
  63. import org.apache.xml.dtm.Axis;
  64. /**
  65. * <meta name="usage" content="internal"/>
  66. * This class iterates over set of nodes that needs to be sorted.
  67. */
  68. public class WalkingIteratorSorted extends WalkingIterator
  69. {
  70. // /** True if the nodes will be found in document order */
  71. // protected boolean m_inNaturalOrder = false;
  72. /** True if the nodes will be found in document order, and this can
  73. * be determined statically. */
  74. protected boolean m_inNaturalOrderStatic = false;
  75. /**
  76. * Create a WalkingIteratorSorted object.
  77. *
  78. * @param nscontext The namespace context for this iterator,
  79. * should be OK if null.
  80. */
  81. public WalkingIteratorSorted(PrefixResolver nscontext)
  82. {
  83. super(nscontext);
  84. }
  85. /**
  86. * Create a WalkingIterator iterator, including creation
  87. * of step walkers from the opcode list, and call back
  88. * into the Compiler to create predicate expressions.
  89. *
  90. * @param compiler The Compiler which is creating
  91. * this expression.
  92. * @param opPos The position of this iterator in the
  93. * opcode list from the compiler.
  94. * @param shouldLoadWalkers True if walkers should be
  95. * loaded, or false if this is a derived iterator and
  96. * it doesn't wish to load child walkers.
  97. *
  98. * @throws javax.xml.transform.TransformerException
  99. */
  100. WalkingIteratorSorted(
  101. Compiler compiler, int opPos, int analysis, boolean shouldLoadWalkers)
  102. throws javax.xml.transform.TransformerException
  103. {
  104. super(compiler, opPos, analysis, shouldLoadWalkers);
  105. }
  106. /**
  107. * Returns true if all the nodes in the iteration well be returned in document
  108. * order.
  109. *
  110. * @return true as a default.
  111. */
  112. public boolean isDocOrdered()
  113. {
  114. return m_inNaturalOrderStatic;
  115. }
  116. /**
  117. * Tell if the nodeset can be walked in doc order, via static analysis.
  118. *
  119. *
  120. * @return true if the nodeset can be walked in doc order, without sorting.
  121. */
  122. boolean canBeWalkedInNaturalDocOrderStatic()
  123. {
  124. if (null != m_firstWalker)
  125. {
  126. AxesWalker walker = m_firstWalker;
  127. int prevAxis = -1;
  128. boolean prevIsSimpleDownAxis = true;
  129. for(int i = 0; null != walker; i++)
  130. {
  131. int axis = walker.getAxis();
  132. if(walker.isDocOrdered())
  133. {
  134. boolean isSimpleDownAxis = ((axis == Axis.CHILD)
  135. || (axis == Axis.SELF)
  136. || (axis == Axis.ROOT));
  137. // Catching the filtered list here is only OK because
  138. // FilterExprWalker#isDocOrdered() did the right thing.
  139. if(isSimpleDownAxis || (axis == -1))
  140. walker = walker.getNextWalker();
  141. else
  142. {
  143. boolean isLastWalker = (null == walker.getNextWalker());
  144. if(isLastWalker)
  145. {
  146. if(walker.isDocOrdered() && (axis == Axis.DESCENDANT ||
  147. axis == Axis.DESCENDANTORSELF || axis == Axis.DESCENDANTSFROMROOT
  148. || axis == Axis.DESCENDANTSORSELFFROMROOT) || (axis == Axis.ATTRIBUTE))
  149. return true;
  150. }
  151. return false;
  152. }
  153. }
  154. else
  155. return false;
  156. }
  157. return true;
  158. }
  159. return false;
  160. }
  161. // /**
  162. // * NEEDSDOC Method canBeWalkedInNaturalDocOrder
  163. // *
  164. // *
  165. // * NEEDSDOC (canBeWalkedInNaturalDocOrder) @return
  166. // */
  167. // boolean canBeWalkedInNaturalDocOrder()
  168. // {
  169. //
  170. // if (null != m_firstWalker)
  171. // {
  172. // AxesWalker walker = m_firstWalker;
  173. // int prevAxis = -1;
  174. // boolean prevIsSimpleDownAxis = true;
  175. //
  176. // for(int i = 0; null != walker; i++)
  177. // {
  178. // int axis = walker.getAxis();
  179. //
  180. // if(walker.isDocOrdered())
  181. // {
  182. // boolean isSimpleDownAxis = ((axis == Axis.CHILD)
  183. // || (axis == Axis.SELF)
  184. // || (axis == Axis.ROOT));
  185. // // Catching the filtered list here is only OK because
  186. // // FilterExprWalker#isDocOrdered() did the right thing.
  187. // if(isSimpleDownAxis || (axis == -1))
  188. // walker = walker.getNextWalker();
  189. // else
  190. // {
  191. // boolean isLastWalker = (null == walker.getNextWalker());
  192. // if(isLastWalker)
  193. // {
  194. // if(walker.isDocOrdered() && (axis == Axis.DESCENDANT ||
  195. // axis == Axis.DESCENDANTORSELF || axis == Axis.DESCENDANTSFROMROOT
  196. // || axis == Axis.DESCENDANTSORSELFFROMROOT) || (axis == Axis.ATTRIBUTE))
  197. // return true;
  198. // }
  199. // return false;
  200. // }
  201. // }
  202. // else
  203. // return false;
  204. // }
  205. // return true;
  206. // }
  207. // return false;
  208. // }
  209. /**
  210. * This function is used to perform some extra analysis of the iterator.
  211. *
  212. * @param vars List of QNames that correspond to variables. This list
  213. * should be searched backwards for the first qualified name that
  214. * corresponds to the variable reference qname. The position of the
  215. * QName in the vector from the start of the vector will be its position
  216. * in the stack frame (but variables above the globalsTop value will need
  217. * to be offset to the current stack frame).
  218. */
  219. public void fixupVariables(java.util.Vector vars, int globalsSize)
  220. {
  221. super.fixupVariables(vars, globalsSize);
  222. int analysis = getAnalysisBits();
  223. if(WalkerFactory.isNaturalDocOrder(analysis))
  224. {
  225. m_inNaturalOrderStatic = true;
  226. }
  227. else
  228. {
  229. m_inNaturalOrderStatic = false;
  230. // System.out.println("Setting natural doc order to false: "+
  231. // WalkerFactory.getAnalysisString(analysis));
  232. }
  233. }
  234. }