KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xpath > internal > operations > Operation


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: Operation.java,v 1.12 2004/02/17 04:35:12 minchau Exp $
18  */

19 package com.sun.org.apache.xpath.internal.operations;
20
21 import com.sun.org.apache.xpath.internal.Expression;
22 import com.sun.org.apache.xpath.internal.ExpressionOwner;
23 import com.sun.org.apache.xpath.internal.XPathContext;
24 import com.sun.org.apache.xpath.internal.XPathVisitor;
25 import com.sun.org.apache.xpath.internal.objects.XObject;
26
27 /**
28  * The baseclass for a binary operation.
29  */

30 public class Operation extends Expression implements ExpressionOwner
31 {
32
33   /** The left operand expression.
34    * @serial */

35   protected Expression m_left;
36
37   /** The right operand expression.
38    * @serial */

39   protected Expression m_right;
40   
41   /**
42    * This function is used to fixup variables from QNames to stack frame
43    * indexes at stylesheet build time.
44    * @param vars List of QNames that correspond to variables. This list
45    * should be searched backwards for the first qualified name that
46    * corresponds to the variable reference qname. The position of the
47    * QName in the vector from the start of the vector will be its position
48    * in the stack frame (but variables above the globalsTop value will need
49    * to be offset to the current stack frame).
50    */

51   public void fixupVariables(java.util.Vector JavaDoc vars, int globalsSize)
52   {
53     m_left.fixupVariables(vars, globalsSize);
54     m_right.fixupVariables(vars, globalsSize);
55   }
56
57
58   /**
59    * Tell if this expression or it's subexpressions can traverse outside
60    * the current subtree.
61    *
62    * @return true if traversal outside the context node's subtree can occur.
63    */

64   public boolean canTraverseOutsideSubtree()
65   {
66
67     if (null != m_left && m_left.canTraverseOutsideSubtree())
68       return true;
69
70     if (null != m_right && m_right.canTraverseOutsideSubtree())
71       return true;
72
73     return false;
74   }
75
76   /**
77    * Set the left and right operand expressions for this operation.
78    *
79    *
80    * @param l The left expression operand.
81    * @param r The right expression operand.
82    */

83   public void setLeftRight(Expression l, Expression r)
84   {
85     m_left = l;
86     m_right = r;
87     l.exprSetParent(this);
88     r.exprSetParent(this);
89   }
90
91   /**
92    * Execute a binary operation by calling execute on each of the operands,
93    * and then calling the operate method on the derived class.
94    *
95    *
96    * @param xctxt The runtime execution context.
97    *
98    * @return The XObject result of the operation.
99    *
100    * @throws javax.xml.transform.TransformerException
101    */

102   public XObject execute(XPathContext xctxt)
103           throws javax.xml.transform.TransformerException JavaDoc
104   {
105
106     XObject left = m_left.execute(xctxt, true);
107     XObject right = m_right.execute(xctxt, true);
108
109     XObject result = operate(left, right);
110     left.detach();
111     right.detach();
112     return result;
113   }
114
115   /**
116    * Apply the operation to two operands, and return the result.
117    *
118    *
119    * @param left non-null reference to the evaluated left operand.
120    * @param right non-null reference to the evaluated right operand.
121    *
122    * @return non-null reference to the XObject that represents the result of the operation.
123    *
124    * @throws javax.xml.transform.TransformerException
125    */

126   public XObject operate(XObject left, XObject right)
127           throws javax.xml.transform.TransformerException JavaDoc
128   {
129     return null; // no-op
130
}
131
132   /** @return the left operand of binary operation, as an Expression.
133    */

134   public Expression getLeftOperand(){
135     return m_left;
136   }
137
138   /** @return the right operand of binary operation, as an Expression.
139    */

140   public Expression getRightOperand(){
141     return m_right;
142   }
143   
144   class LeftExprOwner implements ExpressionOwner
145   {
146     /**
147      * @see ExpressionOwner#getExpression()
148      */

149     public Expression getExpression()
150     {
151       return m_left;
152     }
153
154     /**
155      * @see ExpressionOwner#setExpression(Expression)
156      */

157     public void setExpression(Expression exp)
158     {
159         exp.exprSetParent(Operation.this);
160         m_left = exp;
161     }
162   }
163
164   /**
165    * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
166    */

167   public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
168   {
169     if(visitor.visitBinaryOperation(owner, this))
170     {
171         m_left.callVisitors(new LeftExprOwner(), visitor);
172         m_right.callVisitors(this, visitor);
173     }
174   }
175
176   /**
177    * @see ExpressionOwner#getExpression()
178    */

179   public Expression getExpression()
180   {
181     return m_right;
182   }
183
184   /**
185    * @see ExpressionOwner#setExpression(Expression)
186    */

187   public void setExpression(Expression exp)
188   {
189     exp.exprSetParent(this);
190     m_right = exp;
191   }
192
193   /**
194    * @see Expression#deepEquals(Expression)
195    */

196   public boolean deepEquals(Expression expr)
197   {
198     if(!isSameClass(expr))
199         return false;
200         
201     if(!m_left.deepEquals(((Operation)expr).m_left))
202         return false;
203         
204     if(!m_right.deepEquals(((Operation)expr).m_right))
205         return false;
206         
207     return true;
208   }
209 }
210
Popular Tags