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: UnaryOperation.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 unary operation base class.
  27. */
  28. public abstract class UnaryOperation extends Expression implements ExpressionOwner
  29. {
  30. /** The operand for the operation.
  31. * @serial */
  32. protected Expression m_right;
  33. /**
  34. * This function is used to fixup variables from QNames to stack frame
  35. * indexes at stylesheet build time.
  36. * @param vars List of QNames that correspond to variables. This list
  37. * should be searched backwards for the first qualified name that
  38. * corresponds to the variable reference qname. The position of the
  39. * QName in the vector from the start of the vector will be its position
  40. * in the stack frame (but variables above the globalsTop value will need
  41. * to be offset to the current stack frame).
  42. */
  43. public void fixupVariables(java.util.Vector vars, int globalsSize)
  44. {
  45. m_right.fixupVariables(vars, globalsSize);
  46. }
  47. /**
  48. * Tell if this expression or it's subexpressions can traverse outside
  49. * the current subtree.
  50. *
  51. * @return true if traversal outside the context node's subtree can occur.
  52. */
  53. public boolean canTraverseOutsideSubtree()
  54. {
  55. if (null != m_right && m_right.canTraverseOutsideSubtree())
  56. return true;
  57. return false;
  58. }
  59. /**
  60. * Set the expression operand for the operation.
  61. *
  62. *
  63. * @param r The expression operand to which the unary operation will be
  64. * applied.
  65. */
  66. public void setRight(Expression r)
  67. {
  68. m_right = r;
  69. r.exprSetParent(this);
  70. }
  71. /**
  72. * Execute the operand and apply the unary operation to the result.
  73. *
  74. *
  75. * @param xctxt The runtime execution context.
  76. *
  77. * @return An XObject that represents the result of applying the unary
  78. * operation to the evaluated operand.
  79. *
  80. * @throws javax.xml.transform.TransformerException
  81. */
  82. public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  83. {
  84. return operate(m_right.execute(xctxt));
  85. }
  86. /**
  87. * Apply the operation to two operands, and return the result.
  88. *
  89. *
  90. * @param right non-null reference to the evaluated right operand.
  91. *
  92. * @return non-null reference to the XObject that represents the result of the operation.
  93. *
  94. * @throws javax.xml.transform.TransformerException
  95. */
  96. public abstract XObject operate(XObject right)
  97. throws javax.xml.transform.TransformerException;
  98. /** @return the operand of unary operation, as an Expression.
  99. */
  100. public Expression getOperand(){
  101. return m_right;
  102. }
  103. /**
  104. * @see XPathVisitable#callVisitors(XPathVisitor)
  105. */
  106. public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
  107. {
  108. if(visitor.visitUnaryOperation(owner, this))
  109. {
  110. m_right.callVisitors(this, visitor);
  111. }
  112. }
  113. /**
  114. * @see ExpressionOwner#getExpression()
  115. */
  116. public Expression getExpression()
  117. {
  118. return m_right;
  119. }
  120. /**
  121. * @see ExpressionOwner#setExpression(Expression)
  122. */
  123. public void setExpression(Expression exp)
  124. {
  125. exp.exprSetParent(this);
  126. m_right = exp;
  127. }
  128. /**
  129. * @see Expression#deepEquals(Expression)
  130. */
  131. public boolean deepEquals(Expression expr)
  132. {
  133. if(!isSameClass(expr))
  134. return false;
  135. if(!m_right.deepEquals(((UnaryOperation)expr).m_right))
  136. return false;
  137. return true;
  138. }
  139. }