KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3 import com.icl.saxon.functions.BooleanFn;
4
5
6 /**
7 * Boolean expression: two booleans combined using AND or OR
8 */

9
10 class BooleanExpression extends BinaryExpression {
11
12     public BooleanExpression(){};
13
14     public BooleanExpression(Expression p1, int operator, Expression p2) {
15         super(p1, operator, p2);
16     }
17
18     public Expression simplify() throws XPathException {
19         p1 = p1.simplify();
20         p2 = p2.simplify();
21         if ((p1 instanceof Value) && (p2 instanceof Value)) {
22             return evaluate(null);
23         }
24         
25         // there's no useful saving to be made if only one argument is known,
26
// evaluating it at runtime is just as fast.
27

28         // compbine two PositionRanges
29

30         if (p1 instanceof PositionRange && p2 instanceof PositionRange) {
31             PositionRange pr1 = (PositionRange)p1;
32             PositionRange pr2 = (PositionRange)p2;
33             if (pr1.getMaxPosition()==Integer.MAX_VALUE && pr2.getMinPosition()==1) {
34                 return new PositionRange(pr1.getMinPosition(), pr2.getMaxPosition());
35             }
36             if (pr2.getMaxPosition()==Integer.MAX_VALUE && pr1.getMinPosition()==1) {
37                 return new PositionRange(pr2.getMinPosition(), pr1.getMaxPosition());
38             }
39         }
40         return this;
41     }
42     
43     public Value evaluate(Context c) throws XPathException {
44         return new BooleanValue(evaluateAsBoolean(c));
45     }
46
47     public boolean evaluateAsBoolean(Context c) throws XPathException {
48         switch(operator) {
49             case Tokenizer.AND:
50                 return p1.evaluateAsBoolean(c) && p2.evaluateAsBoolean(c);
51             case Tokenizer.OR:
52                 return p1.evaluateAsBoolean(c) || p2.evaluateAsBoolean(c);
53             default:
54                 throw new XPathException("Unknown operator in boolean expression");
55         }
56     }
57
58     /**
59     * Determine the data type of the expression
60     * @return Value.BOOLEAN
61     */

62
63     public int getDataType() {
64         return Value.BOOLEAN;
65     }
66
67     /**
68     * Perform a partial evaluation of the expression, by eliminating specified dependencies
69     * on the context.
70     * @param dependencies The dependencies to be removed
71     * @param context The context to be used for the partial evaluation
72     * @return a new expression that does not have any of the specified
73     * dependencies
74     */

75
76     public Expression reduce(int dependencies, Context context) throws XPathException {
77         if ((getDependencies() & dependencies) != 0 ) {
78             Expression e = new BooleanExpression(
79                             p1.reduce(dependencies, context),
80                             operator,
81                             p2.reduce(dependencies, context));
82             e.setStaticContext(getStaticContext());
83             return e.simplify();
84         } else {
85             return this;
86         }
87     }
88
89 }
90
91 //
92
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
93
// you may not use this file except in compliance with the License. You may obtain a copy of the
94
// License at http://www.mozilla.org/MPL/
95
//
96
// Software distributed under the License is distributed on an "AS IS" basis,
97
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
98
// See the License for the specific language governing rights and limitations under the License.
99
//
100
// The Original Code is: all this file.
101
//
102
// The Initial Developer of the Original Code is
103
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
104
//
105
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
106
//
107
// Contributor(s): none.
108
//
109
Popular Tags