| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.flwor; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.parser.*; |
| 12 |
import net.sf.saxon.expr.sort.AtomicComparer; |
| 13 |
import net.sf.saxon.expr.sort.SortKeyDefinition; |
| 14 |
import net.sf.saxon.expr.sort.SortKeyDefinitionList; |
| 15 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 18 |
import net.sf.saxon.value.AtomicValue; |
| 19 |
import net.sf.saxon.value.SequenceType; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class OrderByClause extends Clause { |
| 25 |
|
| 26 |
Operand sortKeysOp; |
| 27 |
AtomicComparer[] comparators; |
| 28 |
Operand tupleOp; |
| 29 |
|
| 30 |
public OrderByClause(FLWORExpression flwor, SortKeyDefinition[] sortKeys, TupleExpression tupleExpression) { |
| 31 |
this.sortKeysOp = new Operand(flwor, new SortKeyDefinitionList(sortKeys), OperandRole.REPEAT_NAVIGATE_CONSTRAINED); |
| 32 |
this.tupleOp = new Operand(flwor, tupleExpression, OperandRole.REPEAT_NAVIGATE_CONSTRAINED); |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
public int getClauseKey() { |
| 37 |
return ORDERBYCLAUSE; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public boolean containsNonInlineableVariableReference(Binding binding) { |
| 42 |
return getTupleExpression().includesBinding(binding); |
| 43 |
} |
| 44 |
|
| 45 |
public OrderByClause copy(FLWORExpression flwor, RebindingMap rebindings) { |
| 46 |
SortKeyDefinitionList sortKeys = getSortKeyDefinitions(); |
| 47 |
SortKeyDefinition[] sk2 = new SortKeyDefinition[sortKeys.size()]; |
| 48 |
for (int i = 0; i < sortKeys.size(); i++) { |
| 49 |
sk2[i] = sortKeys.getSortKeyDefinition(i).copy(rebindings); |
| 50 |
} |
| 51 |
OrderByClause obc = new OrderByClause(flwor, sk2, (TupleExpression) getTupleExpression().copy(rebindings)); |
| 52 |
obc.setLocation(getLocation()); |
| 53 |
obc.setPackageData(getPackageData()); |
| 54 |
obc.comparators = comparators; |
| 55 |
return obc; |
| 56 |
} |
| 57 |
|
| 58 |
public SortKeyDefinitionList getSortKeyDefinitions() { |
| 59 |
return (SortKeyDefinitionList)sortKeysOp.getChildExpression(); |
| 60 |
} |
| 61 |
|
| 62 |
public AtomicComparer[] getAtomicComparers() { |
| 63 |
return comparators; |
| 64 |
} |
| 65 |
|
| 66 |
public TupleExpression getTupleExpression() { |
| 67 |
return (TupleExpression)tupleOp.getChildExpression(); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
@Override |
| 79 |
public TuplePull getPullStream(TuplePull base, XPathContext context) { |
| 80 |
return new OrderByClausePull(base, getTupleExpression(), this, context); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
@Override |
| 93 |
public TuplePush getPushStream(TuplePush destination, XPathContext context) { |
| 94 |
return new OrderByClausePush(destination, getTupleExpression(), this, context); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
@Override |
| 103 |
public void processOperands(OperandProcessor processor) throws XPathException { |
| 104 |
processor.processOperand(tupleOp); |
| 105 |
processor.processOperand(sortKeysOp); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public void typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 116 |
boolean allKeysFixed = true; |
| 117 |
SortKeyDefinitionList sortKeys = getSortKeyDefinitions(); |
| 118 |
for (SortKeyDefinition sk : sortKeys) { |
| 119 |
if (!sk.isFixed()) { |
| 120 |
allKeysFixed = false; |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if (allKeysFixed) { |
| 126 |
comparators = new AtomicComparer[sortKeys.size()]; |
| 127 |
} |
| 128 |
|
| 129 |
int i=0; |
| 130 |
TypeChecker tc = visitor.getConfiguration().getTypeChecker(false); |
| 131 |
for (SortKeyDefinition skd : sortKeys) { |
| 132 |
Expression sortKey = skd.getSortKey(); |
| 133 |
RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.ORDER_BY, "", i); |
| 134 |
role.setErrorCode("XPTY0004"); |
| 135 |
sortKey = tc.staticTypeCheck(sortKey, SequenceType.OPTIONAL_ATOMIC, role, visitor); |
| 136 |
skd.setSortKey(sortKey, false); |
| 137 |
skd.typeCheck(visitor, contextInfo); |
| 138 |
if (skd.isFixed()) { |
| 139 |
AtomicComparer comp = skd.makeComparator( |
| 140 |
visitor.getStaticContext().makeEarlyEvaluationContext()); |
| 141 |
skd.setFinalComparator(comp); |
| 142 |
if (allKeysFixed) { |
| 143 |
comparators[i] = comp; |
| 144 |
} |
| 145 |
} |
| 146 |
i++; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public void addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 151 |
SortKeyDefinitionList sortKeys = getSortKeyDefinitions(); |
| 152 |
for (SortKeyDefinition skd : sortKeys) { |
| 153 |
Expression sortKey = skd.getSortKey(); |
| 154 |
sortKey.addToPathMap(pathMap, pathMapNodeSet); |
| 155 |
|
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
@Override |
| 166 |
public void explain(ExpressionPresenter out) throws XPathException { |
| 167 |
out.startElement("order-by"); |
| 168 |
for (SortKeyDefinition k : getSortKeyDefinitions()) { |
| 169 |
out.startSubsidiaryElement("key"); |
| 170 |
k.getSortKey().export(out); |
| 171 |
out.endSubsidiaryElement(); |
| 172 |
} |
| 173 |
out.endElement(); |
| 174 |
} |
| 175 |
|
| 176 |
public String toString() { |
| 177 |
FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64); |
| 178 |
fsb.append("order by ... "); |
| 179 |
return fsb.toString(); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public AtomicValue evaluateSortKey(int n, XPathContext c) throws XPathException { |
| 193 |
SortKeyDefinitionList sortKeys = getSortKeyDefinitions(); |
| 194 |
return (AtomicValue) sortKeys.getSortKeyDefinition(n).getSortKey().evaluateItem(c); |
| 195 |
} |
| 196 |
|
| 197 |
} |
| 198 |
|