1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: Operation.java,v 1.12 2004/02/17 04:35:12 minchau Exp $
  18. */
  19. package com.sun.org.apache.xpath.internal.operations;
  20. import com.sun.org.apache.xpath.internal.Expression;
  21. import com.sun.org.apache.xpath.internal.ExpressionOwner;
  22. import com.sun.org.apache.xpath.internal.XPathContext;
  23. import com.sun.org.apache.xpath.internal.XPathVisitor;
  24. import com.sun.org.apache.xpath.internal.objects.XObject;
  25. /**
  26. * The baseclass for a binary operation.
  27. */
  28. public class Operation extends Expression implements ExpressionOwner
  29. {
  30. /** The left operand expression.
  31. * @serial */
  32. protected Expression m_left;
  33. /** The right operand expression.
  34. * @serial */
  35. protected Expression m_right;
  36. /**
  37. * This function is used to fixup variables from QNames to stack frame
  38. * indexes at stylesheet build time.
  39. * @param vars List of QNames that correspond to variables. This list
  40. * should be searched backwards for the first qualified name that
  41. * corresponds to the variable reference qname. The position of the
  42. * QName in the vector from the start of the vector will be its position
  43. * in the stack frame (but variables above the globalsTop value will need
  44. * to be offset to the current stack frame).
  45. */
  46. public void fixupVariables(java.util.Vector vars, int globalsSize)
  47. {
  48. m_left.fixupVariables(vars, globalsSize);
  49. m_right.fixupVariables(vars, globalsSize);
  50. }
  51. /**
  52. * Tell if this expression or it's subexpressions can traverse outside
  53. * the current subtree.
  54. *
  55. * @return true if traversal outside the context node's subtree can occur.
  56. */
  57. public boolean canTraverseOutsideSubtree()
  58. {
  59. if (null != m_left && m_left.canTraverseOutsideSubtree())
  60. return true;
  61. if (null != m_right && m_right.canTraverseOutsideSubtree())
  62. return true;
  63. return false;
  64. }
  65. /**
  66. * Set the left and right operand expressions for this operation.
  67. *
  68. *
  69. * @param l The left expression operand.
  70. * @param r The right expression operand.
  71. */
  72. public void setLeftRight(Expression l, Expression r)
  73. {
  74. m_left = l;
  75. m_right = r;
  76. l.exprSetParent(this);
  77. r.exprSetParent(this);
  78. }
  79. /**
  80. * Execute a binary operation by calling execute on each of the operands,
  81. * and then calling the operate method on the derived class.
  82. *
  83. *
  84. * @param xctxt The runtime execution context.
  85. *
  86. * @return The XObject result of the operation.
  87. *
  88. * @throws javax.xml.transform.TransformerException
  89. */
  90. public XObject execute(XPathContext xctxt)
  91. throws javax.xml.transform.TransformerException
  92. {
  93. XObject left = m_left.execute(xctxt, true);
  94. XObject right = m_right.execute(xctxt, true);
  95. XObject result = operate(left, right);
  96. left.detach();
  97. right.detach();
  98. return result;
  99. }
  100. /**
  101. * Apply the operation to two operands, and return the result.
  102. *
  103. *
  104. * @param left non-null reference to the evaluated left operand.
  105. * @param right non-null reference to the evaluated right operand.
  106. *
  107. * @return non-null reference to the XObject that represents the result of the operation.
  108. *
  109. * @throws javax.xml.transform.TransformerException
  110. */
  111. public XObject operate(XObject left, XObject right)
  112. throws javax.xml.transform.TransformerException
  113. {
  114. return null; // no-op
  115. }
  116. /** @return the left operand of binary operation, as an Expression.
  117. */
  118. public Expression getLeftOperand(){
  119. return m_left;
  120. }
  121. /** @return the right operand of binary operation, as an Expression.
  122. */
  123. public Expression getRightOperand(){
  124. return m_right;
  125. }
  126. class LeftExprOwner implements ExpressionOwner
  127. {
  128. /**
  129. * @see ExpressionOwner#getExpression()
  130. */
  131. public Expression getExpression()
  132. {
  133. return m_left;
  134. }
  135. /**
  136. * @see ExpressionOwner#setExpression(Expression)
  137. */
  138. public void setExpression(Expression exp)
  139. {
  140. exp.exprSetParent(Operation.this);
  141. m_left = exp;
  142. }
  143. }
  144. /**
  145. * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
  146. */
  147. public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
  148. {
  149. if(visitor.visitBinaryOperation(owner, this))
  150. {
  151. m_left.callVisitors(new LeftExprOwner(), visitor);
  152. m_right.callVisitors(this, visitor);
  153. }
  154. }
  155. /**
  156. * @see ExpressionOwner#getExpression()
  157. */
  158. public Expression getExpression()
  159. {
  160. return m_right;
  161. }
  162. /**
  163. * @see ExpressionOwner#setExpression(Expression)
  164. */
  165. public void setExpression(Expression exp)
  166. {
  167. exp.exprSetParent(this);
  168. m_right = exp;
  169. }
  170. /**
  171. * @see Expression#deepEquals(Expression)
  172. */
  173. public boolean deepEquals(Expression expr)
  174. {
  175. if(!isSameClass(expr))
  176. return false;
  177. if(!m_left.deepEquals(((Operation)expr).m_left))
  178. return false;
  179. if(!m_right.deepEquals(((Operation)expr).m_right))
  180. return false;
  181. return true;
  182. }
  183. }