KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.om.NodeEnumeration;
4
5
6 /**
7 * An expression representing a nodeset that is a union of two other NodeSets
8 */

9
10 class UnionExpression extends NodeSetExpression {
11
12     // we could have implemented this as a subclass of BinaryExpression but we get more reuse
13
// this way. A rare situation where multiple inheritance would have been nice
14

15     protected Expression p1, p2;
16         
17     /**
18     * Constructor
19     * @param p1 the left-hand operand
20     * @param p2 the right-hand operand
21     */

22
23     public UnionExpression(Expression p1, Expression p2) {
24         this.p1 = p1;
25         this.p2 = p2;
26     }
27
28     /**
29     * Simplify an expression
30     * @return the simplified expression
31     */

32
33     public Expression simplify() throws XPathException {
34         p1 = p1.simplify();
35         p2 = p2.simplify();
36         if (p1 instanceof EmptyNodeSet) return p2;
37         if (p2 instanceof EmptyNodeSet) return p1;
38         return this;
39     }
40
41     /**
42     * Evaluate the union expression. The result will always be sorted in document order,
43     * with duplicates eliminated
44     * @param c The context for evaluation
45     * @param sort Request the nodes in document order (they will be, regardless)
46     * @return a NodeSetValue representing the union of the two operands
47     */

48
49     public NodeEnumeration enumerate(Context c, boolean sort) throws XPathException {
50         return new UnionEnumeration(p1.enumerate(c, true),
51                                     p2.enumerate(c, true),
52                                     c.getController());
53     }
54
55     /**
56     * Determine which aspects of the context the expression depends on. The result is
57     * a bitwise-or'ed value composed from constants such as Context.VARIABLES and
58     * Context.CURRENT_NODE
59     */

60
61     public int getDependencies() {
62         return p1.getDependencies() | p2.getDependencies();
63     }
64
65     /**
66     * Determine, in the case of an expression whose data type is Value.NODESET,
67     * whether all the nodes in the node-set are guaranteed to come from the same
68     * document as the context node. Used for optimization.
69     */

70     
71     public boolean isContextDocumentNodeSet() {
72         return p1.isContextDocumentNodeSet() && p2.isContextDocumentNodeSet();
73     }
74
75     /**
76     * Perform a partial evaluation of the expression, by eliminating specified dependencies
77     * on the context.
78     * @param dependencies The dependencies to be removed
79     * @param context The context to be used for the partial evaluation
80     * @return a new expression that does not have any of the specified
81     * dependencies
82     */

83
84     public Expression reduce(int dependencies, Context context) throws XPathException {
85         if ((getDependencies() & dependencies) != 0 ) {
86             Expression e = new UnionExpression(
87                             p1.reduce(dependencies, context),
88                             p2.reduce(dependencies, context));
89             e.setStaticContext(getStaticContext());
90             return e;
91         } else {
92             return this;
93         }
94     }
95
96     /**
97     * Diagnostic print of expression structure
98     */

99     
100     public void display(int level) {
101         System.err.println(indent(level) + "union");
102         p1.display(level+1);
103         p2.display(level+1);
104     }
105
106 }
107
108 //
109
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
110
// you may not use this file except in compliance with the License. You may obtain a copy of the
111
// License at http://www.mozilla.org/MPL/
112
//
113
// Software distributed under the License is distributed on an "AS IS" basis,
114
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
115
// See the License for the specific language governing rights and limitations under the License.
116
//
117
// The Original Code is: all this file.
118
//
119
// The Initial Developer of the Original Code is
120
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
121
//
122
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
123
//
124
// Contributor(s): none.
125
//
126
Popular Tags