001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 /*
019 * $Id: Operation.java 468655 2006-10-28 07:12:06Z minchau $
020 */
021 package org.apache.xpath.operations;
022
023 import org.apache.xpath.Expression;
024 import org.apache.xpath.ExpressionOwner;
025 import org.apache.xpath.XPathContext;
026 import org.apache.xpath.XPathVisitor;
027 import org.apache.xpath.objects.XObject;
028
029 /**
030 * The baseclass for a binary operation.
031 */
032 public class Operation extends Expression implements ExpressionOwner
033 {
034 static final long serialVersionUID = -3037139537171050430L;
035
036 /** The left operand expression.
037 * @serial */
038 protected Expression m_left;
039
040 /** The right operand expression.
041 * @serial */
042 protected Expression m_right;
043
044 /**
045 * This function is used to fixup variables from QNames to stack frame
046 * indexes at stylesheet build time.
047 * @param vars List of QNames that correspond to variables. This list
048 * should be searched backwards for the first qualified name that
049 * corresponds to the variable reference qname. The position of the
050 * QName in the vector from the start of the vector will be its position
051 * in the stack frame (but variables above the globalsTop value will need
052 * to be offset to the current stack frame).
053 */
054 public void fixupVariables(java.util.Vector vars, int globalsSize)
055 {
056 m_left.fixupVariables(vars, globalsSize);
057 m_right.fixupVariables(vars, globalsSize);
058 }
059
060
061 /**
062 * Tell if this expression or it's subexpressions can traverse outside
063 * the current subtree.
064 *
065 * @return true if traversal outside the context node's subtree can occur.
066 */
067 public boolean canTraverseOutsideSubtree()
068 {
069
070 if (null != m_left && m_left.canTraverseOutsideSubtree())
071 return true;
072
073 if (null != m_right && m_right.canTraverseOutsideSubtree())
074 return true;
075
076 return false;
077 }
078
079 /**
080 * Set the left and right operand expressions for this operation.
081 *
082 *
083 * @param l The left expression operand.
084 * @param r The right expression operand.
085 */
086 public void setLeftRight(Expression l, Expression r)
087 {
088 m_left = l;
089 m_right = r;
090 l.exprSetParent(this);
091 r.exprSetParent(this);
092 }
093
094 /**
095 * Execute a binary operation by calling execute on each of the operands,
096 * and then calling the operate method on the derived class.
097 *
098 *
099 * @param xctxt The runtime execution context.
100 *
101 * @return The XObject result of the operation.
102 *
103 * @throws javax.xml.transform.TransformerException
104 */
105 public XObject execute(XPathContext xctxt)
106 throws javax.xml.transform.TransformerException
107 {
108
109 XObject left = m_left.execute(xctxt, true);
110 XObject right = m_right.execute(xctxt, true);
111
112 XObject result = operate(left, right);
113 left.detach();
114 right.detach();
115 return result;
116 }
117
118 /**
119 * Apply the operation to two operands, and return the result.
120 *
121 *
122 * @param left non-null reference to the evaluated left operand.
123 * @param right non-null reference to the evaluated right operand.
124 *
125 * @return non-null reference to the XObject that represents the result of the operation.
126 *
127 * @throws javax.xml.transform.TransformerException
128 */
129 public XObject operate(XObject left, XObject right)
130 throws javax.xml.transform.TransformerException
131 {
132 return null; // no-op
133 }
134
135 /** @return the left operand of binary operation, as an Expression.
136 */
137 public Expression getLeftOperand(){
138 return m_left;
139 }
140
141 /** @return the right operand of binary operation, as an Expression.
142 */
143 public Expression getRightOperand(){
144 return m_right;
145 }
146
147 class LeftExprOwner implements ExpressionOwner
148 {
149 /**
150 * @see ExpressionOwner#getExpression()
151 */
152 public Expression getExpression()
153 {
154 return m_left;
155 }
156
157 /**
158 * @see ExpressionOwner#setExpression(Expression)
159 */
160 public void setExpression(Expression exp)
161 {
162 exp.exprSetParent(Operation.this);
163 m_left = exp;
164 }
165 }
166
167 /**
168 * @see org.apache.xpath.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
169 */
170 public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
171 {
172 if(visitor.visitBinaryOperation(owner, this))
173 {
174 m_left.callVisitors(new LeftExprOwner(), visitor);
175 m_right.callVisitors(this, visitor);
176 }
177 }
178
179 /**
180 * @see ExpressionOwner#getExpression()
181 */
182 public Expression getExpression()
183 {
184 return m_right;
185 }
186
187 /**
188 * @see ExpressionOwner#setExpression(Expression)
189 */
190 public void setExpression(Expression exp)
191 {
192 exp.exprSetParent(this);
193 m_right = exp;
194 }
195
196 /**
197 * @see Expression#deepEquals(Expression)
198 */
199 public boolean deepEquals(Expression expr)
200 {
201 if(!isSameClass(expr))
202 return false;
203
204 if(!m_left.deepEquals(((Operation)expr).m_left))
205 return false;
206
207 if(!m_right.deepEquals(((Operation)expr).m_right))
208 return false;
209
210 return true;
211 }
212 }