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.XPathContext;
  62. import org.apache.xpath.XPathVisitor;
  63. import org.apache.xpath.ExpressionOwner;
  64. import org.apache.xpath.objects.XObject;
  65. /**
  66. * The unary operation base class.
  67. */
  68. public abstract class UnaryOperation extends Expression implements ExpressionOwner
  69. {
  70. /** The operand for the operation.
  71. * @serial */
  72. protected Expression m_right;
  73. /**
  74. * This function is used to fixup variables from QNames to stack frame
  75. * indexes at stylesheet build time.
  76. * @param vars List of QNames that correspond to variables. This list
  77. * should be searched backwards for the first qualified name that
  78. * corresponds to the variable reference qname. The position of the
  79. * QName in the vector from the start of the vector will be its position
  80. * in the stack frame (but variables above the globalsTop value will need
  81. * to be offset to the current stack frame).
  82. */
  83. public void fixupVariables(java.util.Vector vars, int globalsSize)
  84. {
  85. m_right.fixupVariables(vars, globalsSize);
  86. }
  87. /**
  88. * Tell if this expression or it's subexpressions can traverse outside
  89. * the current subtree.
  90. *
  91. * @return true if traversal outside the context node's subtree can occur.
  92. */
  93. public boolean canTraverseOutsideSubtree()
  94. {
  95. if (null != m_right && m_right.canTraverseOutsideSubtree())
  96. return true;
  97. return false;
  98. }
  99. /**
  100. * Set the expression operand for the operation.
  101. *
  102. *
  103. * @param r The expression operand to which the unary operation will be
  104. * applied.
  105. */
  106. public void setRight(Expression r)
  107. {
  108. m_right = r;
  109. r.exprSetParent(this);
  110. }
  111. /**
  112. * Execute the operand and apply the unary operation to the result.
  113. *
  114. *
  115. * @param xctxt The runtime execution context.
  116. *
  117. * @return An XObject that represents the result of applying the unary
  118. * operation to the evaluated operand.
  119. *
  120. * @throws javax.xml.transform.TransformerException
  121. */
  122. public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
  123. {
  124. return operate(m_right.execute(xctxt));
  125. }
  126. /**
  127. * Apply the operation to two operands, and return the result.
  128. *
  129. *
  130. * @param right non-null reference to the evaluated right operand.
  131. *
  132. * @return non-null reference to the XObject that represents the result of the operation.
  133. *
  134. * @throws javax.xml.transform.TransformerException
  135. */
  136. public abstract XObject operate(XObject right)
  137. throws javax.xml.transform.TransformerException;
  138. /** @return the operand of unary operation, as an Expression.
  139. */
  140. public Expression getOperand(){
  141. return m_right;
  142. }
  143. /**
  144. * @see XPathVisitable#callVisitors(XPathVisitor)
  145. */
  146. public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
  147. {
  148. if(visitor.visitUnaryOperation(owner, this))
  149. {
  150. m_right.callVisitors(this, visitor);
  151. }
  152. }
  153. /**
  154. * @see ExpressionOwner#getExpression()
  155. */
  156. public Expression getExpression()
  157. {
  158. return m_right;
  159. }
  160. /**
  161. * @see ExpressionOwner#setExpression(Expression)
  162. */
  163. public void setExpression(Expression exp)
  164. {
  165. exp.exprSetParent(this);
  166. m_right = exp;
  167. }
  168. /**
  169. * @see Expression#deepEquals(Expression)
  170. */
  171. public boolean deepEquals(Expression expr)
  172. {
  173. if(!isSameClass(expr))
  174. return false;
  175. if(!m_right.deepEquals(((UnaryOperation)expr).m_right))
  176. return false;
  177. return true;
  178. }
  179. }