01: package net.sf.saxon.sort;
02:
03: import net.sf.saxon.expr.*;
04: import net.sf.saxon.om.NamePool;
05: import net.sf.saxon.om.SequenceIterator;
06: import net.sf.saxon.trans.XPathException;
07: import net.sf.saxon.value.SequenceExtent;
08:
09: /**
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: public int computeSpecialProperties() {
20: int baseProps = operand.getSpecialProperties();
21: if ((baseProps & StaticProperty.REVERSE_DOCUMENT_ORDER) != 0) {
22: return (baseProps & (~StaticProperty.REVERSE_DOCUMENT_ORDER))
23: | StaticProperty.ORDERED_NODESET;
24: } else if ((baseProps & StaticProperty.ORDERED_NODESET) != 0) {
25: return (baseProps & (~StaticProperty.ORDERED_NODESET))
26: | StaticProperty.REVERSE_DOCUMENT_ORDER;
27: } else {
28: return baseProps;
29: }
30: }
31:
32: /**
33: * Promote this expression if possible
34: */
35:
36: public Expression promote(PromotionOffer offer)
37: throws XPathException {
38: Expression exp = offer.accept(this );
39: if (exp != null) {
40: return exp;
41: } else {
42: operand = doPromotion(operand, offer);
43: return this ;
44: }
45: }
46:
47: public SequenceIterator iterate(XPathContext context)
48: throws XPathException {
49: SequenceIterator forwards = operand.iterate(context);
50: if (forwards instanceof ReversibleIterator) {
51: return ((ReversibleIterator) forwards).getReverseIterator();
52: } else {
53: SequenceExtent extent = new SequenceExtent(forwards);
54: return extent.reverseIterate();
55: }
56: }
57:
58: public boolean effectiveBooleanValue(XPathContext context)
59: throws XPathException {
60: return operand.effectiveBooleanValue(context);
61: }
62:
63: /**
64: * Give a string representation of the operator for use in diagnostics
65: * @return the operator, as a string
66: */
67:
68: protected String displayOperator(NamePool pool) {
69: return "reverse order";
70: }
71: }
72:
73: //
74: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
75: // you may not use this file except in compliance with the License. You may obtain a copy of the
76: // License at http://www.mozilla.org/MPL/
77: //
78: // Software distributed under the License is distributed on an "AS IS" basis,
79: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
80: // See the License for the specific language governing rights and limitations under the License.
81: //
82: // The Original Code is: all this file.
83: //
84: // The Initial Developer of the Original Code is Michael H. Kay
85: //
86: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
87: //
88: // Contributor(s): none
89: //
|