001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: UnaryOperation.java,v 1.14 2005/01/23 01:08:22 mcnamara Exp $
018: */
019: package org.apache.xpath.operations;
020:
021: import org.apache.xpath.Expression;
022: import org.apache.xpath.ExpressionOwner;
023: import org.apache.xpath.XPathContext;
024: import org.apache.xpath.XPathVisitor;
025: import org.apache.xpath.objects.XObject;
026:
027: /**
028: * The unary operation base class.
029: */
030: public abstract class UnaryOperation extends Expression implements
031: ExpressionOwner {
032: static final long serialVersionUID = 6536083808424286166L;
033:
034: /** The operand for the operation.
035: * @serial */
036: protected Expression m_right;
037:
038: /**
039: * This function is used to fixup variables from QNames to stack frame
040: * indexes at stylesheet build time.
041: * @param vars List of QNames that correspond to variables. This list
042: * should be searched backwards for the first qualified name that
043: * corresponds to the variable reference qname. The position of the
044: * QName in the vector from the start of the vector will be its position
045: * in the stack frame (but variables above the globalsTop value will need
046: * to be offset to the current stack frame).
047: */
048: public void fixupVariables(java.util.Vector vars, int globalsSize) {
049: m_right.fixupVariables(vars, globalsSize);
050: }
051:
052: /**
053: * Tell if this expression or it's subexpressions can traverse outside
054: * the current subtree.
055: *
056: * @return true if traversal outside the context node's subtree can occur.
057: */
058: public boolean canTraverseOutsideSubtree() {
059:
060: if (null != m_right && m_right.canTraverseOutsideSubtree())
061: return true;
062:
063: return false;
064: }
065:
066: /**
067: * Set the expression operand for the operation.
068: *
069: *
070: * @param r The expression operand to which the unary operation will be
071: * applied.
072: */
073: public void setRight(Expression r) {
074: m_right = r;
075: r.exprSetParent(this );
076: }
077:
078: /**
079: * Execute the operand and apply the unary operation to the result.
080: *
081: *
082: * @param xctxt The runtime execution context.
083: *
084: * @return An XObject that represents the result of applying the unary
085: * operation to the evaluated operand.
086: *
087: * @throws javax.xml.transform.TransformerException
088: */
089: public XObject execute(XPathContext xctxt)
090: throws javax.xml.transform.TransformerException {
091:
092: return operate(m_right.execute(xctxt));
093: }
094:
095: /**
096: * Apply the operation to two operands, and return the result.
097: *
098: *
099: * @param right non-null reference to the evaluated right operand.
100: *
101: * @return non-null reference to the XObject that represents the result of the operation.
102: *
103: * @throws javax.xml.transform.TransformerException
104: */
105: public abstract XObject operate(XObject right)
106: throws javax.xml.transform.TransformerException;
107:
108: /** @return the operand of unary operation, as an Expression.
109: */
110: public Expression getOperand() {
111: return m_right;
112: }
113:
114: /**
115: * @see org.apache.xpath.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
116: */
117: public void callVisitors(ExpressionOwner owner, XPathVisitor visitor) {
118: if (visitor.visitUnaryOperation(owner, this )) {
119: m_right.callVisitors(this , visitor);
120: }
121: }
122:
123: /**
124: * @see ExpressionOwner#getExpression()
125: */
126: public Expression getExpression() {
127: return m_right;
128: }
129:
130: /**
131: * @see ExpressionOwner#setExpression(Expression)
132: */
133: public void setExpression(Expression exp) {
134: exp.exprSetParent(this );
135: m_right = exp;
136: }
137:
138: /**
139: * @see Expression#deepEquals(Expression)
140: */
141: public boolean deepEquals(Expression expr) {
142: if (!isSameClass(expr))
143: return false;
144:
145: if (!m_right.deepEquals(((UnaryOperation) expr).m_right))
146: return false;
147:
148: return true;
149: }
150:
151: }
|