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