001: package net.sf.saxon.sort;
002:
003: import net.sf.saxon.expr.*;
004: import net.sf.saxon.om.NamePool;
005: import net.sf.saxon.om.SequenceIterator;
006: import net.sf.saxon.trans.XPathException;
007: import net.sf.saxon.type.ItemType;
008:
009: /**
010: * A DocumentSorter is an expression that sorts a sequence of nodes into
011: * document order.
012: */
013: public class DocumentSorter extends UnaryExpression {
014:
015: private NodeOrderComparer comparer;
016:
017: public DocumentSorter(Expression base) {
018: super (base);
019: int props = base.getSpecialProperties();
020: if (((props & StaticProperty.CONTEXT_DOCUMENT_NODESET) != 0)
021: || (props & StaticProperty.SINGLE_DOCUMENT_NODESET) != 0) {
022: comparer = LocalOrderComparer.getInstance();
023: } else {
024: comparer = GlobalOrderComparer.getInstance();
025: }
026: }
027:
028: public Expression simplify(StaticContext env) throws XPathException {
029: operand = operand.simplify(env);
030: if ((operand.getSpecialProperties() & StaticProperty.ORDERED_NODESET) != 0) {
031: // this can happen as a result of further simplification
032: return operand;
033: }
034: return this ;
035: }
036:
037: public Expression optimize(Optimizer opt, StaticContext env,
038: ItemType contextItemType) throws XPathException {
039: operand = operand.optimize(opt, env, contextItemType);
040: if ((operand.getSpecialProperties() & StaticProperty.ORDERED_NODESET) != 0) {
041: // this can happen as a result of further simplification
042: return operand;
043: }
044: return this ;
045: }
046:
047: public int computeSpecialProperties() {
048: return operand.getSpecialProperties()
049: | StaticProperty.ORDERED_NODESET;
050: }
051:
052: /**
053: * Promote this expression if possible
054: */
055:
056: public Expression promote(PromotionOffer offer)
057: throws XPathException {
058: Expression exp = offer.accept(this );
059: if (exp != null) {
060: return exp;
061: } else {
062: operand = doPromotion(operand, offer);
063: return this ;
064: }
065: }
066:
067: public SequenceIterator iterate(XPathContext context)
068: throws XPathException {
069: return new DocumentOrderIterator(operand.iterate(context),
070: comparer);
071: }
072:
073: public boolean effectiveBooleanValue(XPathContext context)
074: throws XPathException {
075: return operand.effectiveBooleanValue(context);
076: }
077:
078: /**
079: * Give a string representation of the operator for use in diagnostics
080: * @return the operator, as a string
081: */
082:
083: protected String displayOperator(NamePool pool) {
084: if (comparer instanceof LocalOrderComparer) {
085: return "intra-document sort and deduplicate";
086: } else {
087: return "sort and deduplicate";
088: }
089: }
090:
091: }
092:
093: //
094: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
095: // you may not use this file except in compliance with the License. You may obtain a copy of the
096: // License at http://www.mozilla.org/MPL/
097: //
098: // Software distributed under the License is distributed on an "AS IS" basis,
099: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
100: // See the License for the specific language governing rights and limitations under the License.
101: //
102: // The Original Code is: all this file.
103: //
104: // The Initial Developer of the Original Code is Michael H. Kay
105: //
106: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
107: //
108: // Contributor(s): none
109: //
|