| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Expression; |
| 11 |
import net.sf.saxon.expr.SystemFunctionCall; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.expr.sort.GroupIterator; |
| 14 |
import net.sf.saxon.expr.sort.MergeGroupingIterator; |
| 15 |
import net.sf.saxon.expr.sort.MergeInstr; |
| 16 |
import net.sf.saxon.om.Sequence; |
| 17 |
import net.sf.saxon.om.SequenceIterator; |
| 18 |
import net.sf.saxon.om.SequenceTool; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.type.AnyItemType; |
| 21 |
import net.sf.saxon.type.ItemType; |
| 22 |
|
| 23 |
import java.util.HashSet; |
| 24 |
import java.util.Set; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public class CurrentMergeGroup extends SystemFunction { |
| 31 |
|
| 32 |
private boolean isInLoop = false; |
| 33 |
private MergeInstr controllingInstruction = null; |
| 34 |
private Set<String> allowedNames = new HashSet<>(); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public void setControllingInstruction(MergeInstr instruction, boolean isInLoop) { |
| 44 |
this.controllingInstruction = instruction; |
| 45 |
this.isInLoop = isInLoop; |
| 46 |
for (MergeInstr.MergeSource m : instruction.getMergeSources()) { |
| 47 |
String name = m.sourceName; |
| 48 |
if (name != null) { |
| 49 |
allowedNames.add(name); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public MergeInstr getControllingInstruction() { |
| 60 |
return controllingInstruction; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public boolean isInLoop() { |
| 70 |
return isInLoop; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
@Override |
| 78 |
public ItemType getResultItemType() { |
| 79 |
return AnyItemType.getInstance(); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
@Override |
| 89 |
public int getSpecialProperties(Expression[] arguments) { |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
@Override |
| 102 |
public Expression makeFunctionCall(Expression... arguments) { |
| 103 |
return new SystemFunctionCall(this, arguments) { |
| 104 |
@Override |
| 105 |
public Expression getScopingExpression() { |
| 106 |
return getControllingInstruction(); |
| 107 |
} |
| 108 |
}; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public Sequence<?> call(XPathContext context, Sequence[] arguments ) throws XPathException { |
| 121 |
String source = null; |
| 122 |
if (arguments.length > 0) { |
| 123 |
source = arguments[0].head().getStringValue(); |
| 124 |
} |
| 125 |
return SequenceTool.toLazySequence(currentGroup(source, context)); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
private SequenceIterator<?> currentGroup(String source, XPathContext c) throws XPathException { |
| 137 |
GroupIterator gi = c.getCurrentMergeGroupIterator(); |
| 138 |
if (gi == null) { |
| 139 |
throw new XPathException("There is no current merge group", "XTDE3480"); |
| 140 |
} |
| 141 |
if (source == null) { |
| 142 |
return gi.iterateCurrentGroup(); |
| 143 |
} else { |
| 144 |
if (!allowedNames.contains(source)) { |
| 145 |
throw new XPathException("Supplied argument (" + source + |
| 146 |
") is not the name of any xsl:merge-source in the containing xsl:merge instruction", "XTDE3490"); |
| 147 |
} |
| 148 |
return ((MergeGroupingIterator)gi).iterateCurrentGroup(source); |
| 149 |
} |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
public String getStreamerName() { |
| 154 |
return "CurrentMergeGroup"; |
| 155 |
} |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
|