KickJava   Java API By Example, From Geeks To Geeks.

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


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.value.SequenceExtent;
8
9 /**
10  * A Reverser is an expression that reverses the order of a sequence of items.
11  */

12
13 public class Reverser extends UnaryExpression {
14
15     public Reverser(Expression base) {
16         super(base);
17     }
18
19
20
21     public int computeSpecialProperties() {
22         int baseProps = operand.getSpecialProperties();
23         if ((baseProps & StaticProperty.REVERSE_DOCUMENT_ORDER) != 0) {
24             return (baseProps &
25                 (~StaticProperty.REVERSE_DOCUMENT_ORDER)) |
26                 StaticProperty.ORDERED_NODESET;
27         } else if ((baseProps & StaticProperty.ORDERED_NODESET) != 0) {
28             return (baseProps &
29                 (~StaticProperty.ORDERED_NODESET)) |
30                 StaticProperty.REVERSE_DOCUMENT_ORDER;
31         } else {
32             return baseProps;
33         }
34     }
35
36     /**
37     * Promote this expression if possible
38     */

39
40     public Expression promote(PromotionOffer offer) throws XPathException {
41         Expression exp = offer.accept(this);
42         if (exp != null) {
43             return exp;
44         } else {
45             operand = doPromotion(operand, offer);
46             return this;
47         }
48     }
49
50     public SequenceIterator iterate(XPathContext context) throws XPathException {
51         SequenceIterator forwards = operand.iterate(context);
52         if (forwards instanceof ReversibleIterator) {
53             return ((ReversibleIterator)forwards).getReverseIterator();
54         } else {
55             SequenceExtent extent = new SequenceExtent(forwards);
56             return extent.reverseIterate();
57         }
58     }
59
60     public boolean effectiveBooleanValue(XPathContext context) throws XPathException {
61         return operand.effectiveBooleanValue(context);
62     }
63
64     /**
65      * Give a string representation of the operator for use in diagnostics
66      * @return the operator, as a string
67      */

68
69     protected String JavaDoc displayOperator(NamePool pool) {
70         return "reverse order";
71     }
72 }
73
74
75 //
76
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
77
// you may not use this file except in compliance with the License. You may obtain a copy of the
78
// License at http://www.mozilla.org/MPL/
79
//
80
// Software distributed under the License is distributed on an "AS IS" basis,
81
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
82
// See the License for the specific language governing rights and limitations under the License.
83
//
84
// The Original Code is: all this file.
85
//
86
// The Initial Developer of the Original Code is Michael H. Kay
87
//
88
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
89
//
90
// Contributor(s): none
91
//
Popular Tags