KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.sort.*;
4 import com.icl.saxon.om.NodeEnumeration;
5 //import java.util.*;
6

7 /**
8 * A NodeSetExpression that retrieves nodes in order according to a specified sort key. <br>
9 * Note there is no direct XSL expression syntax that generates this. <br>
10 * The expression sorts a base NodeSet according to the value of a specified
11 * sort key. The sort key may be composite. The base NodeSet will always be in document
12 * order.
13 */

14
15 public class SortedSelection extends NodeSetExpression {
16   
17     private Expression selection;
18     private SortKeyDefinition[] sortkeys;
19                                            // in major-to-minor order
20
private int numberOfSortKeys;
21
22     /**
23     * Constructor
24     * @param s An expression whose value is the base nodeset to be sorted
25     * @param k the number of sort keys
26     */

27
28     public SortedSelection(Expression s, int k) {
29         selection = s;
30         sortkeys = new SortKeyDefinition[k];
31         numberOfSortKeys = k;
32     }
33
34     /**
35     * Add a sort key and other sorting parameters
36     * @param sk A SortKeyDefinition
37     * @param k The index of this SortKeyDefinition. The first sort key in major-to-minor
38     * order is numbered 0 (zero), the others are 1, 2, ... in sequence.
39     * @throws ArrayIndexOutOfBoundsException if the sort key index is out of range,
40     * according to the number of sort keys defined when the SortedSelection was
41     * initialized.
42     */

43
44     public void setSortKey(SortKeyDefinition sk, int k) {
45         sortkeys[k] = sk;
46     }
47
48     /**
49     * Simplify an expression
50     * @return the simplified expression
51     */

52
53     public Expression simplify() throws XPathException {
54         
55         // simplify the base expression and the sort keys
56
selection = selection.simplify();
57         for (int i=0; i<numberOfSortKeys; i++) {
58             SortKeyDefinition sk = sortkeys[i];
59             sk.setSortKey(sk.getSortKey().simplify());
60             sk.setOrder(sk.getOrder().simplify());
61             sk.setDataType(sk.getDataType().simplify());
62             sk.setCaseOrder(sk.getCaseOrder().simplify());
63             sk.setLanguage(sk.getLanguage().simplify());
64         }
65         return this;
66     }
67
68     /**
69     * Determine which aspects of the context the expression depends on. The result is
70     * a bitwise-or'ed value composed from constants such as Context.VARIABLES and
71     * Context.CURRENT_NODE
72     */

73
74     public int getDependencies() {
75         int dep = selection.getDependencies();
76         for (int i=0; i<sortkeys.length; i++) {
77             SortKeyDefinition sk = sortkeys[i];
78             // Not all dependencies in the sort key matter, because the context node, etc,
79
// are not dependent on the outer context
80
dep |= (sk.getSortKey().getDependencies() &
81                         (Context.VARIABLES | Context.CONTROLLER));
82             dep |= sk.getOrder().getDependencies();
83             dep |= sk.getDataType().getDependencies();
84             dep |= sk.getCaseOrder().getDependencies();
85             dep |= sk.getLanguage().getDependencies();
86         }
87         return dep;
88     }
89
90     /**
91     * Perform a partial evaluation of the expression, by eliminating specified dependencies
92     * on the context.
93     * @param dependencies The dependencies to be removed
94     * @param context The context to be used for the partial evaluation
95     * @return a new expression that does not have any of the specified
96     * dependencies
97     */

98
99     public Expression reduce(int dependencies, Context context) throws XPathException {
100         if ((dependencies & getDependencies()) != 0) {
101             Expression newselection = selection.reduce(dependencies, context);
102             SortedSelection newss = new SortedSelection(newselection, numberOfSortKeys);
103             newss.setStaticContext(getStaticContext());
104             for (int i=0; i<numberOfSortKeys; i++) {
105                 SortKeyDefinition sk = sortkeys[i];
106                 SortKeyDefinition sknew = new SortKeyDefinition();
107                 sknew.setStaticContext(getStaticContext());
108                 sknew.setSortKey(
109                     sk.getSortKey().reduce(
110                         dependencies & (Context.VARIABLES | Context.CONTROLLER),
111                         context));
112                 sknew.setOrder(sk.getOrder().reduce(dependencies, context));
113                 sknew.setDataType(sk.getDataType().reduce(dependencies, context));
114                 sknew.setCaseOrder(sk.getCaseOrder().reduce(dependencies, context));
115                 sknew.setLanguage(sk.getLanguage().reduce(dependencies, context));
116                 newss.setSortKey(sknew, i);
117             }
118             return newss.simplify();
119         } else {
120             return this;
121         }
122     }
123
124     /**
125     * Evaluate the expression by sorting the base nodeset using the supplied key.
126     * @param context The context for the evaluation
127     * @param sort: must be false (because document order would be meaningless)
128     * @return the sorted nodeset
129     */

130
131     public NodeEnumeration enumerate(Context context, boolean sort) throws XPathException
132     {
133         if (sort==true) {
134             throw new XPathException("SortedSelection doesn't provide nodes in document order");
135         }
136         NodeEnumeration base = selection.enumerate(context, false);
137         return new SortKeyEnumeration(context, base, sortkeys);
138     }
139
140     /**
141     * Diagnostic print of expression structure
142     */

143     
144     public void display(int level) {
145         System.err.println(indent(level) + "sorted");
146         selection.display(level+1);
147     }
148
149 }
150
151 //
152
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
153
// you may not use this file except in compliance with the License. You may obtain a copy of the
154
// License at http://www.mozilla.org/MPL/
155
//
156
// Software distributed under the License is distributed on an "AS IS" basis,
157
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
158
// See the License for the specific language governing rights and limitations under the License.
159
//
160
// The Original Code is: all this file.
161
//
162
// The Initial Developer of the Original Code is
163
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
164
//
165
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
166
//
167
// Contributor(s): none.
168
//
169
Popular Tags