KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.expr;
2 import net.sf.saxon.om.Item;
3 import net.sf.saxon.om.NodeInfo;
4 import net.sf.saxon.sort.GlobalOrderComparer;
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.BooleanValue;
9 import net.sf.saxon.value.SequenceType;
10
11
12 /**
13 * IdentityComparison: a boolean expression that compares two nodes
14 * for equals, not-equals, greater-than or less-than based on identity and
15 * document ordering
16 */

17
18 public final class IdentityComparison extends BinaryExpression {
19
20     private boolean generateIdEmulation = false;
21         // this flag is set if an "X is Y" or "X isnot Y" comparison is being used
22
// to emulate generate-id(X) = / != generate-id(Y). The handling of an empty
23
// sequence in the two cases is different.
24

25     /**
26     * Create an identity comparison identifying the two operands and the operator
27     * @param p1 the left-hand operand
28     * @param op the operator, as a token returned by the Tokenizer (e.g. Token.LT)
29     * @param p2 the right-hand operand
30     */

31
32     public IdentityComparison(Expression p1, int op, Expression p2) {
33         super(p1, op, p2);
34     }
35
36     /**
37      * Set flag to indicate different empty-sequence behavior when emulating
38      * comparison of two generate-id's
39      */

40
41     public void setGenerateIdEmulation(boolean flag) {
42         generateIdEmulation = flag;
43     }
44
45     /**
46     * Type-check the expression
47     */

48
49     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
50
51         operand0 = operand0.typeCheck(env, contextItemType);
52         operand1 = operand1.typeCheck(env, contextItemType);
53
54         RoleLocator role0 = new RoleLocator(RoleLocator.BINARY_EXPR, Token.tokens[operator], 0, null);
55         role0.setSourceLocator(this);
56         operand0 = TypeChecker.staticTypeCheck(
57                 operand0, SequenceType.OPTIONAL_NODE, false, role0, env);
58
59         RoleLocator role1 = new RoleLocator(RoleLocator.BINARY_EXPR, Token.tokens[operator], 1, null);
60         role1.setSourceLocator(this);
61         operand1 = TypeChecker.staticTypeCheck(
62                 operand1, SequenceType.OPTIONAL_NODE, false, role1, env);
63         return this;
64     }
65
66
67
68     /**
69     * Evaluate the expression
70     */

71
72     public Item evaluateItem(XPathContext context) throws XPathException {
73         NodeInfo node1 = getNode(operand0, context);
74         if (node1==null) {
75             if (generateIdEmulation) {
76                 return BooleanValue.get(getNode(operand1, context)==null);
77             }
78             return null;
79         }
80
81         NodeInfo node2 = getNode(operand1, context);
82         if (node2==null) {
83             if (generateIdEmulation) {
84                 return BooleanValue.FALSE;
85             }
86             return null;
87         }
88
89         return BooleanValue.get(compareIdentity(node1, node2));
90     }
91
92     public boolean effectiveBooleanValue(XPathContext context) throws XPathException {
93         NodeInfo node1 = getNode(operand0, context);
94         if (node1==null) {
95             if (generateIdEmulation) {
96                 return getNode(operand1, context) == null;
97             }
98             return false;
99         }
100
101         NodeInfo node2 = getNode(operand1, context);
102         if (node2==null) return false;
103
104         return compareIdentity(node1, node2);
105     }
106
107     private boolean compareIdentity(NodeInfo node1, NodeInfo node2) {
108
109         switch (operator) {
110         case Token.IS:
111             return node1.isSameNodeInfo(node2);
112 // case Token.ISNOT:
113
// return !node1.isSameNode(node2);
114
case Token.PRECEDES:
115             return GlobalOrderComparer.getInstance().compare(node1, node2) < 0;
116         case Token.FOLLOWS:
117             return GlobalOrderComparer.getInstance().compare(node1, node2) > 0;
118         default:
119             throw new UnsupportedOperationException JavaDoc("Unknown node identity test");
120         }
121     }
122
123     private NodeInfo getNode(Expression exp, XPathContext c) throws XPathException {
124         return (NodeInfo)exp.evaluateItem(c);
125     }
126
127
128     /**
129     * Determine the data type of the expression
130     * @return Type.BOOLEAN
131     */

132
133     public ItemType getItemType() {
134         return Type.BOOLEAN_TYPE;
135     }
136
137
138 }
139
140 //
141
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
142
// you may not use this file except in compliance with the License. You may obtain a copy of the
143
// License at http://www.mozilla.org/MPL/
144
//
145
// Software distributed under the License is distributed on an "AS IS" basis,
146
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
147
// See the License for the specific language governing rights and limitations under the License.
148
//
149
// The Original Code is: all this file.
150
//
151
// The Initial Developer of the Original Code is Michael H. Kay
152
//
153
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
154
//
155
// Contributor(s): none.
156
//
157
Popular Tags