KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > expr > BinaryExpression


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3
4 import java.util.*;
5
6 /**
7 * Binary Expression: a numeric expression consisting of the two operands and an operator
8 */

9
10 abstract class BinaryExpression extends Expression {
11
12     protected Expression p1, p2;
13     protected int operator; // represented by the token number from class Tokenizer
14

15     /**
16     * Default constructor
17     */

18
19     public BinaryExpression() {}
20
21     /**
22     * Create a binary expression identifying the two operands and the operator
23     * @param p1 the left-hand operand
24     * @param op the operator, as a token returned by the Tokenizer (e.g. Tokenizer.AND)
25     * @param p2 the right-hand operand
26     */

27
28     public BinaryExpression(Expression p1, int op, Expression p2) {
29             this.p1 = p1;
30             this.p2 = p2;
31             this.operator = op;
32     }
33
34     /**
35     * Identify the two operands and the operator (for use when the default constructor was used)
36     * @param p1 the left-hand operand
37     * @param op the operator, as a token returned by the Tokenizer (e.g. Tokenizer.AND)
38     * @param p2 the right-hand operand
39     */

40
41     public void setDetails(Expression p1, int op, Expression p2) {
42             this.p1 = p1;
43             this.p2 = p2;
44             this.operator = op;
45     }
46
47     /**
48     * Simplify an expression
49     * @return the simplified expression
50     */

51
52     public Expression simplify() throws XPathException {
53         p1 = p1.simplify();
54         p2 = p2.simplify();
55
56         // if both operands are known, pre-evaluate the expression
57
if ((p1 instanceof Value) && (p2 instanceof Value)) {
58             return evaluate(null);
59         }
60         return this;
61     }
62
63     /**
64     * Determine which aspects of the context the expression depends on. The result is
65     * a bitwise-or'ed value composed from constants such as Context.VARIABLES and
66     * Context.CURRENT_NODE
67     */

68
69     public int getDependencies() {
70         return p1.getDependencies() | p2.getDependencies();
71     }
72
73     /**
74     * Diagnostic print of expression structure
75     */

76     
77     public void display(int level) {
78         System.err.println(indent(level) + Tokenizer.tokens[operator]);
79         p1.display(level+1);
80         p2.display(level+1);
81     }
82
83 }
84
85 //
86
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
87
// you may not use this file except in compliance with the License. You may obtain a copy of the
88
// License at http://www.mozilla.org/MPL/
89
//
90
// Software distributed under the License is distributed on an "AS IS" basis,
91
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
92
// See the License for the specific language governing rights and limitations under the License.
93
//
94
// The Original Code is: all this file.
95
//
96
// The Initial Developer of the Original Code is
97
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
98
//
99
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
100
//
101
// Contributor(s): none.
102
//
103
Popular Tags