KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > sort > DocumentSorter


1 package net.sf.saxon.sort;
2
3 import net.sf.saxon.expr.*;
4 import net.sf.saxon.om.NamePool;
5 import net.sf.saxon.om.SequenceIterator;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.type.ItemType;
8
9 /**
10  * A DocumentSorter is an expression that sorts a sequence of nodes into
11  * document order.
12  */

13 public class DocumentSorter extends UnaryExpression {
14
15     private NodeOrderComparer comparer;
16
17     public DocumentSorter(Expression base) {
18         super(base);
19         int props = base.getSpecialProperties();
20         if (((props & StaticProperty.CONTEXT_DOCUMENT_NODESET) != 0) ||
21                 (props & StaticProperty.SINGLE_DOCUMENT_NODESET) != 0) {
22             comparer = LocalOrderComparer.getInstance();
23         } else {
24             comparer = GlobalOrderComparer.getInstance();
25         }
26     }
27
28     public Expression simplify(StaticContext env) throws XPathException {
29         operand = operand.simplify(env);
30         if ((operand.getSpecialProperties() & StaticProperty.ORDERED_NODESET) != 0) {
31             // this can happen as a result of further simplification
32
return operand;
33         }
34         return this;
35     }
36
37     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
38         operand = operand.optimize(opt, env, contextItemType);
39         if ((operand.getSpecialProperties() & StaticProperty.ORDERED_NODESET) != 0) {
40             // this can happen as a result of further simplification
41
return operand;
42         }
43         return this;
44     }
45
46
47     public int computeSpecialProperties() {
48         return operand.getSpecialProperties() | StaticProperty.ORDERED_NODESET;
49     }
50
51     /**
52      * Promote this expression if possible
53      */

54
55     public Expression promote(PromotionOffer offer) throws XPathException {
56         Expression exp = offer.accept(this);
57         if (exp != null) {
58             return exp;
59         } else {
60             operand = doPromotion(operand, offer);
61             return this;
62         }
63     }
64
65     public SequenceIterator iterate(XPathContext context) throws XPathException {
66         return new DocumentOrderIterator(operand.iterate(context), comparer);
67     }
68
69     public boolean effectiveBooleanValue(XPathContext context) throws XPathException {
70         return operand.effectiveBooleanValue(context);
71     }
72
73     /**
74      * Give a string representation of the operator for use in diagnostics
75      * @return the operator, as a string
76      */

77
78     protected String JavaDoc displayOperator(NamePool pool) {
79         if (comparer instanceof LocalOrderComparer) {
80             return "intra-document sort and deduplicate";
81         } else {
82             return "sort and deduplicate";
83         }
84     }
85
86 }
87
88
89 //
90
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
91
// you may not use this file except in compliance with the License. You may obtain a copy of the
92
// License at http://www.mozilla.org/MPL/
93
//
94
// Software distributed under the License is distributed on an "AS IS" basis,
95
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
96
// See the License for the specific language governing rights and limitations under the License.
97
//
98
// The Original Code is: all this file.
99
//
100
// The Initial Developer of the Original Code is Michael H. Kay
101
//
102
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
103
//
104
// Contributor(s): none
105
//
Popular Tags