KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > expr > BooleanExpression


1 package net.sf.saxon.expr;
2 import net.sf.saxon.om.Item;
3 import net.sf.saxon.trans.XPathException;
4 import net.sf.saxon.type.ItemType;
5 import net.sf.saxon.type.Type;
6 import net.sf.saxon.value.BooleanValue;
7
8 import java.util.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10
11
12 /**
13 * Boolean expression: two truth values combined using AND or OR.
14 */

15
16 public class BooleanExpression extends BinaryExpression {
17
18     public BooleanExpression(Expression p1, int operator, Expression p2) {
19         super(p1, operator, p2);
20     }
21
22     /**
23     * Determine the static cardinality. Returns [1..1]
24     */

25
26     public int computeCardinality() {
27         return StaticProperty.EXACTLY_ONE;
28     }
29
30     /**
31     * Evaluate the expression
32     */

33
34     public Item evaluateItem(XPathContext context) throws XPathException {
35         return BooleanValue.get(effectiveBooleanValue(context));
36     }
37
38     /**
39     * Evaluate as a boolean.
40     */

41
42     public boolean effectiveBooleanValue(XPathContext c) throws XPathException {
43         switch(operator) {
44             case Token.AND:
45                 return operand0.effectiveBooleanValue(c) && operand1.effectiveBooleanValue(c);
46
47             case Token.OR:
48                 return operand0.effectiveBooleanValue(c) || operand1.effectiveBooleanValue(c);
49
50             default:
51                 throw new UnsupportedOperationException JavaDoc("Unknown operator in boolean expression");
52         }
53     }
54
55     /**
56     * Determine the data type of the expression
57     * @return Type.BOOLEAN
58     */

59
60     public ItemType getItemType() {
61         return Type.BOOLEAN_TYPE;
62     }
63
64     /**
65      * Construct a list containing the "anded" subexpressions of an expression:
66      * if the expression is (A and B and C), this returns (A, B, C).
67      * TODO: could do more complete analysis to convert the expression to conjunctive normal form
68      * @param exp the expression to be decomposed
69      * @param list the list to which the subexpressions are to be added.
70      */

71
72     public static void listAndComponents(Expression exp, List JavaDoc list) {
73         if (exp instanceof BooleanExpression && ((BooleanExpression)exp).getOperator() == Token.AND) {
74             for (Iterator JavaDoc iter = exp.iterateSubExpressions(); iter.hasNext();) {
75                  listAndComponents((Expression)iter.next(), list);
76             }
77         } else {
78             list.add(exp);
79         }
80     }
81 }
82
83 //
84
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
85
// you may not use this file except in compliance with the License. You may obtain a copy of the
86
// License at http://www.mozilla.org/MPL/
87
//
88
// Software distributed under the License is distributed on an "AS IS" basis,
89
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
90
// See the License for the specific language governing rights and limitations under the License.
91
//
92
// The Original Code is: all this file.
93
//
94
// The Initial Developer of the Original Code is Michael H. Kay.
95
//
96
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
97
//
98
// Contributor(s): none.
99
//
100
Popular Tags