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.operations;
  58. import java.util.Vector;
  59. import javax.xml.transform.TransformerException;
  60. import org.apache.xpath.Expression;
  61. import org.apache.xpath.ExpressionOwner;
  62. import org.apache.xpath.XPathContext;
  63. import org.apache.xpath.XPathVisitor;
  64. import org.apache.xpath.objects.XObject;
  65. /**
  66. * The baseclass for a binary operation.
  67. */
  68. public class Operation extends Expression implements ExpressionOwner
  69. {
  70. /** The left operand expression.
  71. * @serial */
  72. protected Expression m_left;
  73. /** The right operand expression.
  74. * @serial */
  75. protected Expression m_right;
  76. /**
  77. * This function is used to fixup variables from QNames to stack frame
  78. * indexes at stylesheet build time.
  79. * @param vars List of QNames that correspond to variables. This list
  80. * should be searched backwards for the first qualified name that
  81. * corresponds to the variable reference qname. The position of the
  82. * QName in the vector from the start of the vector will be its position
  83. * in the stack frame (but variables above the globalsTop value will need
  84. * to be offset to the current stack frame).
  85. */
  86. public void fixupVariables(java.util.Vector vars, int globalsSize)
  87. {
  88. m_left.fixupVariables(vars, globalsSize);
  89. m_right.fixupVariables(vars, globalsSize);
  90. }
  91. /**
  92. * Tell if this expression or it's subexpressions can traverse outside
  93. * the current subtree.
  94. *
  95. * @return true if traversal outside the context node's subtree can occur.
  96. */
  97. public boolean canTraverseOutsideSubtree()
  98. {
  99. if (null != m_left && m_left.canTraverseOutsideSubtree())
  100. return true;
  101. if (null != m_right && m_right.canTraverseOutsideSubtree())
  102. return true;
  103. return false;
  104. }
  105. /**
  106. * Set the left and right operand expressions for this operation.
  107. *
  108. *
  109. * @param l The left expression operand.
  110. * @param r The right expression operand.
  111. */
  112. public void setLeftRight(Expression l, Expression r)
  113. {
  114. m_left = l;
  115. m_right = r;
  116. l.exprSetParent(this);
  117. r.exprSetParent(this);
  118. }
  119. /**
  120. * Execute a binary operation by calling execute on each of the operands,
  121. * and then calling the operate method on the derived class.
  122. *
  123. *
  124. * @param xctxt The runtime execution context.
  125. *
  126. * @return The XObject result of the operation.
  127. *
  128. * @throws javax.xml.transform.TransformerException
  129. */
  130. public XObject execute(XPathContext xctxt)
  131. throws javax.xml.transform.TransformerException
  132. {
  133. XObject left = m_left.execute(xctxt, true);
  134. XObject right = m_right.execute(xctxt, true);
  135. XObject result = operate(left, right);
  136. left.detach();
  137. right.detach();
  138. return result;
  139. }
  140. /**
  141. * Apply the operation to two operands, and return the result.
  142. *
  143. *
  144. * @param left non-null reference to the evaluated left operand.
  145. * @param right non-null reference to the evaluated right operand.
  146. *
  147. * @return non-null reference to the XObject that represents the result of the operation.
  148. *
  149. * @throws javax.xml.transform.TransformerException
  150. */
  151. public XObject operate(XObject left, XObject right)
  152. throws javax.xml.transform.TransformerException
  153. {
  154. return null; // no-op
  155. }
  156. /** @return the left operand of binary operation, as an Expression.
  157. */
  158. public Expression getLeftOperand(){
  159. return m_left;
  160. }
  161. /** @return the right operand of binary operation, as an Expression.
  162. */
  163. public Expression getRightOperand(){
  164. return m_right;
  165. }
  166. class LeftExprOwner implements ExpressionOwner
  167. {
  168. /**
  169. * @see ExpressionOwner#getExpression()
  170. */
  171. public Expression getExpression()
  172. {
  173. return m_left;
  174. }
  175. /**
  176. * @see ExpressionOwner#setExpression(Expression)
  177. */
  178. public void setExpression(Expression exp)
  179. {
  180. exp.exprSetParent(Operation.this);
  181. m_left = exp;
  182. }
  183. }
  184. /**
  185. * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
  186. */
  187. public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
  188. {
  189. if(visitor.visitBinaryOperation(owner, this))
  190. {
  191. m_left.callVisitors(new LeftExprOwner(), visitor);
  192. m_right.callVisitors(this, visitor);
  193. }
  194. }
  195. /**
  196. * @see ExpressionOwner#getExpression()
  197. */
  198. public Expression getExpression()
  199. {
  200. return m_right;
  201. }
  202. /**
  203. * @see ExpressionOwner#setExpression(Expression)
  204. */
  205. public void setExpression(Expression exp)
  206. {
  207. exp.exprSetParent(this);
  208. m_right = exp;
  209. }
  210. /**
  211. * @see Expression#deepEquals(Expression)
  212. */
  213. public boolean deepEquals(Expression expr)
  214. {
  215. if(!isSameClass(expr))
  216. return false;
  217. if(!m_left.deepEquals(((Operation)expr).m_left))
  218. return false;
  219. if(!m_right.deepEquals(((Operation)expr).m_right))
  220. return false;
  221. return true;
  222. }
  223. }