| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.XPathContext; |
| 11 |
import net.sf.saxon.expr.accum.Accumulator; |
| 12 |
import net.sf.saxon.expr.accum.AccumulatorManager; |
| 13 |
import net.sf.saxon.expr.accum.AccumulatorRegistry; |
| 14 |
import net.sf.saxon.expr.accum.IAccumulatorData; |
| 15 |
import net.sf.saxon.om.*; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.trans.XsltController; |
| 18 |
import net.sf.saxon.type.Type; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public abstract class AccumulatorFn extends SystemFunction { |
| 24 |
|
| 25 |
public enum Phase {AFTER, BEFORE} |
| 26 |
|
| 27 |
public abstract Phase getPhase(); |
| 28 |
|
| 29 |
|
| 30 |
private Sequence<?> getAccumulatorValue(String name, Phase phase, XPathContext context) throws XPathException { |
| 31 |
StructuredQName qName; |
| 32 |
try { |
| 33 |
qName = StructuredQName.fromLexicalQName(name, false, true, getRetainedStaticContext()); |
| 34 |
} catch (XPathException err) { |
| 35 |
throw new XPathException("Invalid accumulator name: " + err.getMessage(), "XTDE3340", context); |
| 36 |
} |
| 37 |
AccumulatorRegistry registry = getRetainedStaticContext().getPackageData().getAccumulatorRegistry(); |
| 38 |
Accumulator accumulator = registry == null ? null : registry.getAccumulator(qName); |
| 39 |
if (accumulator == null) { |
| 40 |
throw new XPathException("Accumulator " + name + " has not been declared", "XTDE3340"); |
| 41 |
} |
| 42 |
|
| 43 |
Item node = context.getContextItem(); |
| 44 |
if (node == null) { |
| 45 |
throw new XPathException("No context item for evaluation of accumulator function", "XTDE3350", context); |
| 46 |
} |
| 47 |
if (!(node instanceof NodeInfo)) { |
| 48 |
throw new XPathException("Context item for evaluation of accumulator function must be a node", "XTTE3360", context); |
| 49 |
} |
| 50 |
int kind = ((NodeInfo)node).getNodeKind(); |
| 51 |
if (kind == Type.ATTRIBUTE || kind == Type.NAMESPACE) { |
| 52 |
throw new XPathException("Context item for evaluation of accumulator function must not be an attribute or namespace node", "XTTE3360", context); |
| 53 |
} |
| 54 |
|
| 55 |
Sequence<?> streamedAccVal = registry.getStreamingAccumulatorValue((NodeInfo) node, accumulator, phase); |
| 56 |
if (streamedAccVal != null) { |
| 57 |
return streamedAccVal; |
| 58 |
} |
| 59 |
|
| 60 |
TreeInfo root = ((NodeInfo)node).getTreeInfo(); |
| 61 |
XsltController controller = (XsltController)context.getController(); |
| 62 |
if (!accumulator.isUniversallyApplicable() && !controller.getAccumulatorManager().isApplicable(root, accumulator)) { |
| 63 |
throw new XPathException("Accumulator " + name + " is not applicable to the current document", "XTDE3362"); |
| 64 |
} |
| 65 |
AccumulatorManager manager = controller.getAccumulatorManager(); |
| 66 |
IAccumulatorData data = manager.getAccumulatorData(root, accumulator, context); |
| 67 |
return data.getValue((NodeInfo) node, phase == Phase.AFTER); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 92 |
String name = arguments[0].head().getStringValue(); |
| 93 |
return getAccumulatorValue(name, getPhase(), context); |
| 94 |
} |
| 95 |
|
| 96 |
public static class AccumulatorBefore extends AccumulatorFn { |
| 97 |
|
| 98 |
public AccumulatorBefore() { |
| 99 |
|
| 100 |
} |
| 101 |
@Override |
| 102 |
public Phase getPhase() { |
| 103 |
return Phase.BEFORE; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public static class AccumulatorAfter extends AccumulatorFn { |
| 108 |
@Override |
| 109 |
public Phase getPhase() { |
| 110 |
return Phase.AFTER; |
| 111 |
} |
| 112 |
|
| 113 |
public String getStreamerName() { |
| 114 |
return "AccumulatorAfter"; |
| 115 |
} |
| 116 |
} |
| 117 |
} |