KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.expr;
2 import net.sf.saxon.om.Item;
3 import net.sf.saxon.sort.AtomicComparer;
4 import net.sf.saxon.trans.DynamicError;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.type.ItemType;
7 import net.sf.saxon.type.Type;
8 import net.sf.saxon.value.AtomicValue;
9 import net.sf.saxon.value.BooleanValue;
10 import net.sf.saxon.ConversionContext;
11
12 import java.util.Comparator JavaDoc;
13
14
15 /**
16 * Class to handle comparisons of singletons. Unlike ValueComparison, this class
17 * converts untyped atomic values to the type of the other argument, and returns false
18  * (rather than ()) if either operand is ().
19 */

20
21 public class SingletonComparison extends BinaryExpression {
22
23     private AtomicComparer comparer;
24
25     public SingletonComparison(Expression p1, int operator, Expression p2) {
26         super(p1, operator, p2);
27     }
28
29     public void setComparator(Comparator JavaDoc comp, ConversionContext conversion) {
30         if (comp instanceof AtomicComparer) {
31             comparer = (AtomicComparer)comp;
32         } else {
33             comparer = new AtomicComparer(comp, conversion);
34         }
35     }
36
37     /**
38     * Determine the static cardinality. Returns [1..1]
39     */

40
41     public int computeCardinality() {
42         return StaticProperty.EXACTLY_ONE;
43     }
44
45     /**
46     * Determine the data type of the expression
47     * @return Type.BOOLEAN
48     */

49
50     public ItemType getItemType() {
51         return Type.BOOLEAN_TYPE;
52     }
53
54     /**
55     * Evaluate the expression in a given context
56     * @param context the given context for evaluation
57     * @return a BooleanValue representing the result of the numeric comparison of the two operands
58     */

59
60     public Item evaluateItem(XPathContext context) throws XPathException {
61         return BooleanValue.get(effectiveBooleanValue(context));
62     }
63
64     /**
65     * Evaluate the expression in a boolean context
66     * @param context the given context for evaluation
67     * @return a boolean representing the result of the numeric comparison of the two operands
68     */

69
70     public boolean effectiveBooleanValue(XPathContext context) throws XPathException {
71         AtomicValue v1 = (AtomicValue)operand0.evaluateItem(context);
72         if (v1==null) return false;
73         AtomicValue v2 = (AtomicValue)operand1.evaluateItem(context);
74         if (v2==null) return false;
75
76         try {
77             return GeneralComparison.compare(v1, operator, v2, comparer, context);
78         } catch (DynamicError e) {
79             // re-throw the exception with location information added
80
if (e.getXPathContext() == null) {
81                 e.setXPathContext(context);
82             }
83             if (e.getLocator() == null) {
84                 e.setLocator(this);
85             }
86             throw e;
87         }
88     }
89
90     protected String JavaDoc displayOperator() {
91         return "singleton " + super.displayOperator();
92     }
93
94 }
95
96 //
97
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
98
// you may not use this file except in compliance with the License. You may obtain a copy of the
99
// License at http://www.mozilla.org/MPL/
100
//
101
// Software distributed under the License is distributed on an "AS IS" basis,
102
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
103
// See the License for the specific language governing rights and limitations under the License.
104
//
105
// The Original Code is: all this file.
106
//
107
// The Initial Developer of the Original Code is Michael H. Kay
108
//
109
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
110
//
111
// Contributor(s): none.
112
//
113
Popular Tags