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 java.util.Vector;
  59. import javax.xml.transform.TransformerException;
  60. import org.apache.xalan.templates.FuncKey;
  61. import org.apache.xml.dtm.Axis;
  62. import org.apache.xml.dtm.DTM;
  63. import org.apache.xml.dtm.DTMIterator;
  64. import org.apache.xml.utils.PrefixResolver;
  65. import org.apache.xpath.Expression;
  66. import org.apache.xpath.ExpressionOwner;
  67. import org.apache.xpath.VariableStack;
  68. import org.apache.xpath.XPathContext;
  69. import org.apache.xpath.XPathVisitor;
  70. import org.apache.xpath.compiler.Compiler;
  71. import org.apache.xpath.compiler.OpCodes;
  72. import org.apache.xpath.objects.XNodeSet;
  73. /**
  74. * Walker for the OP_VARIABLE, or OP_EXTFUNCTION, or OP_FUNCTION, or OP_GROUP,
  75. * op codes.
  76. * @see <a href="http://www.w3.org/TR/xpath#NT-FilterExpr">XPath FilterExpr descriptions</a>
  77. */
  78. public class FilterExprWalker extends AxesWalker
  79. {
  80. /**
  81. * Construct a FilterExprWalker using a LocPathIterator.
  82. *
  83. * @param locPathIterator non-null reference to the parent iterator.
  84. */
  85. public FilterExprWalker(WalkingIterator locPathIterator)
  86. {
  87. super(locPathIterator, Axis.FILTEREDLIST);
  88. }
  89. /**
  90. * Init a FilterExprWalker.
  91. *
  92. * @param compiler non-null reference to the Compiler that is constructing.
  93. * @param opPos positive opcode position for this step.
  94. * @param stepType The type of step.
  95. *
  96. * @throws javax.xml.transform.TransformerException
  97. */
  98. public void init(Compiler compiler, int opPos, int stepType)
  99. throws javax.xml.transform.TransformerException
  100. {
  101. super.init(compiler, opPos, stepType);
  102. // Smooth over an anomily in the opcode map...
  103. switch (stepType)
  104. {
  105. case OpCodes.OP_FUNCTION :
  106. case OpCodes.OP_EXTFUNCTION :
  107. m_mustHardReset = true;
  108. case OpCodes.OP_GROUP :
  109. case OpCodes.OP_VARIABLE :
  110. m_expr = compiler.compile(opPos);
  111. m_expr.exprSetParent(this);
  112. if((OpCodes.OP_FUNCTION == stepType) && (m_expr instanceof org.apache.xalan.templates.FuncKey))
  113. {
  114. // hack/temp workaround
  115. m_canDetachNodeset = false;
  116. }
  117. break;
  118. default :
  119. m_expr = compiler.compile(opPos + 2);
  120. m_expr.exprSetParent(this);
  121. }
  122. // if(m_expr instanceof WalkingIterator)
  123. // {
  124. // WalkingIterator wi = (WalkingIterator)m_expr;
  125. // if(wi.getFirstWalker() instanceof FilterExprWalker)
  126. // {
  127. // FilterExprWalker fw = (FilterExprWalker)wi.getFirstWalker();
  128. // if(null == fw.getNextWalker())
  129. // {
  130. // m_expr = fw.m_expr;
  131. // m_expr.exprSetParent(this);
  132. // }
  133. // }
  134. //
  135. // }
  136. }
  137. /**
  138. * Detaches the walker from the set which it iterated over, releasing
  139. * any computational resources and placing the iterator in the INVALID
  140. * state.
  141. */
  142. public void detach()
  143. {
  144. super.detach();
  145. m_exprObj.detach();
  146. m_exprObj = null;
  147. }
  148. /**
  149. * Set the root node of the TreeWalker.
  150. *
  151. * @param root non-null reference to the root, or starting point of
  152. * the query.
  153. */
  154. public void setRoot(int root)
  155. {
  156. super.setRoot(root);
  157. m_exprObj = FilterExprIteratorSimple.executeFilterExpr(root,
  158. m_lpi.getXPathContext(), m_lpi.getPrefixResolver(),
  159. m_lpi.getIsTopLevel(), m_lpi.m_stackFrame, m_expr);
  160. }
  161. /**
  162. * Get a cloned FilterExprWalker.
  163. *
  164. * @return A new FilterExprWalker that can be used without mutating this one.
  165. *
  166. * @throws CloneNotSupportedException
  167. */
  168. public Object clone() throws CloneNotSupportedException
  169. {
  170. FilterExprWalker clone = (FilterExprWalker) super.clone();
  171. // clone.m_expr = (Expression)((Expression)m_expr).clone();
  172. if (null != m_exprObj)
  173. clone.m_exprObj = (XNodeSet) m_exprObj.clone();
  174. return clone;
  175. }
  176. /**
  177. * This method needs to override AxesWalker.acceptNode because FilterExprWalkers
  178. * don't need to, and shouldn't, do a node test.
  179. * @param n The node to check to see if it passes the filter or not.
  180. * @return a constant to determine whether the node is accepted,
  181. * rejected, or skipped, as defined above .
  182. */
  183. public short acceptNode(int n)
  184. {
  185. try
  186. {
  187. if (getPredicateCount() > 0)
  188. {
  189. countProximityPosition(0);
  190. if (!executePredicates(n, m_lpi.getXPathContext()))
  191. return DTMIterator.FILTER_SKIP;
  192. }
  193. return DTMIterator.FILTER_ACCEPT;
  194. }
  195. catch (javax.xml.transform.TransformerException se)
  196. {
  197. throw new RuntimeException(se.getMessage());
  198. }
  199. }
  200. /**
  201. * Moves the <code>TreeWalker</code> to the next visible node in document
  202. * order relative to the current node, and returns the new node. If the
  203. * current node has no next node, or if the search for nextNode attempts
  204. * to step upward from the TreeWalker's root node, returns
  205. * <code>null</code> , and retains the current node.
  206. * @return The new node, or <code>null</code> if the current node has no
  207. * next node in the TreeWalker's logical view.
  208. */
  209. public int getNextNode()
  210. {
  211. if (null != m_exprObj)
  212. {
  213. int next = m_exprObj.nextNode();
  214. return next;
  215. }
  216. else
  217. return DTM.NULL;
  218. }
  219. /**
  220. * Get the index of the last node that can be itterated to.
  221. *
  222. *
  223. * @param xctxt XPath runtime context.
  224. *
  225. * @return the index of the last node that can be itterated to.
  226. */
  227. public int getLastPos(XPathContext xctxt)
  228. {
  229. return m_exprObj.getLength();
  230. }
  231. /** The contained expression. Should be non-null.
  232. * @serial */
  233. private Expression m_expr;
  234. /** The result of executing m_expr. Needs to be deep cloned on clone op. */
  235. transient private XNodeSet m_exprObj;
  236. private boolean m_mustHardReset = false;
  237. private boolean m_canDetachNodeset = true;
  238. /**
  239. * This function is used to fixup variables from QNames to stack frame
  240. * indexes at stylesheet build time.
  241. * @param vars List of QNames that correspond to variables. This list
  242. * should be searched backwards for the first qualified name that
  243. * corresponds to the variable reference qname. The position of the
  244. * QName in the vector from the start of the vector will be its position
  245. * in the stack frame (but variables above the globalsTop value will need
  246. * to be offset to the current stack frame).
  247. */
  248. public void fixupVariables(java.util.Vector vars, int globalsSize)
  249. {
  250. super.fixupVariables(vars, globalsSize);
  251. m_expr.fixupVariables(vars, globalsSize);
  252. }
  253. /**
  254. * Get the inner contained expression of this filter.
  255. */
  256. public Expression getInnerExpression()
  257. {
  258. return m_expr;
  259. }
  260. /**
  261. * Set the inner contained expression of this filter.
  262. */
  263. public void setInnerExpression(Expression expr)
  264. {
  265. expr.exprSetParent(this);
  266. m_expr = expr;
  267. }
  268. /**
  269. * Get the analysis bits for this walker, as defined in the WalkerFactory.
  270. * @return One of WalkerFactory#BIT_DESCENDANT, etc.
  271. */
  272. public int getAnalysisBits()
  273. {
  274. if (null != m_expr && m_expr instanceof PathComponent)
  275. {
  276. return ((PathComponent) m_expr).getAnalysisBits();
  277. }
  278. return WalkerFactory.BIT_FILTER;
  279. }
  280. /**
  281. * Returns true if all the nodes in the iteration well be returned in document
  282. * order.
  283. * Warning: This can only be called after setRoot has been called!
  284. *
  285. * @return true as a default.
  286. */
  287. public boolean isDocOrdered()
  288. {
  289. return m_exprObj.isDocOrdered();
  290. }
  291. /**
  292. * Returns the axis being iterated, if it is known.
  293. *
  294. * @return Axis.CHILD, etc., or -1 if the axis is not known or is of multiple
  295. * types.
  296. */
  297. public int getAxis()
  298. {
  299. return m_exprObj.getAxis();
  300. }
  301. class filterExprOwner implements ExpressionOwner
  302. {
  303. /**
  304. * @see ExpressionOwner#getExpression()
  305. */
  306. public Expression getExpression()
  307. {
  308. return m_expr;
  309. }
  310. /**
  311. * @see ExpressionOwner#setExpression(Expression)
  312. */
  313. public void setExpression(Expression exp)
  314. {
  315. exp.exprSetParent(FilterExprWalker.this);
  316. m_expr = exp;
  317. }
  318. }
  319. /**
  320. * This will traverse the heararchy, calling the visitor for
  321. * each member. If the called visitor method returns
  322. * false, the subtree should not be called.
  323. *
  324. * @param owner The owner of the visitor, where that path may be
  325. * rewritten if needed.
  326. * @param visitor The visitor whose appropriate method will be called.
  327. */
  328. public void callPredicateVisitors(XPathVisitor visitor)
  329. {
  330. m_expr.callVisitors(new filterExprOwner(), visitor);
  331. super.callPredicateVisitors(visitor);
  332. }
  333. /**
  334. * @see Expression#deepEquals(Expression)
  335. */
  336. public boolean deepEquals(Expression expr)
  337. {
  338. if (!super.deepEquals(expr))
  339. return false;
  340. FilterExprWalker walker = (FilterExprWalker)expr;
  341. if(!m_expr.deepEquals(walker.m_expr))
  342. return false;
  343. return true;
  344. }
  345. }