| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr.accum; |
| 9 |
|
| 10 |
import net.sf.saxon.functions.AccumulatorFn; |
| 11 |
import net.sf.saxon.om.Item; |
| 12 |
import net.sf.saxon.om.NodeInfo; |
| 13 |
import net.sf.saxon.om.Sequence; |
| 14 |
import net.sf.saxon.om.StructuredQName; |
| 15 |
import net.sf.saxon.style.StyleElement; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.value.Whitespace; |
| 18 |
|
| 19 |
import java.util.*; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class AccumulatorRegistry { |
| 26 |
|
| 27 |
protected Map<StructuredQName, Accumulator> accumulatorsByName = new HashMap<StructuredQName, Accumulator>(); |
| 28 |
|
| 29 |
|
| 30 |
public AccumulatorRegistry() { |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public Set<Accumulator> getUsedAccumulators(String useAccumulatorsAtt, StyleElement styleElement) throws XPathException { |
| 42 |
Set<Accumulator> accumulators = new HashSet<Accumulator>(); |
| 43 |
String attNames = Whitespace.trim(useAccumulatorsAtt); |
| 44 |
String[] tokens = attNames.split("[ \t\r\n]+"); |
| 45 |
if (tokens.length == 1 && tokens[0].equals("#all")) { |
| 46 |
for (Accumulator acc : getAllAccumulators()) { |
| 47 |
accumulators.add(acc); |
| 48 |
} |
| 49 |
} else if (tokens.length == 1 && tokens[0].isEmpty()) { |
| 50 |
|
| 51 |
} else { |
| 52 |
List<StructuredQName> names = new ArrayList<StructuredQName>(tokens.length); |
| 53 |
for (String token : tokens) { |
| 54 |
if (token.equals("#all")) { |
| 55 |
throw new XPathException("If use-accumulators contains the token '#all', it must be the only token", "XTSE3300"); |
| 56 |
} |
| 57 |
StructuredQName name = styleElement.makeQName(token); |
| 58 |
if (names.contains(name)) { |
| 59 |
throw new XPathException("Duplicate QName in use-accumulators attribute: " + token, "XTSE3300"); |
| 60 |
} |
| 61 |
Accumulator acc = getAccumulator(name); |
| 62 |
if (acc == null) { |
| 63 |
throw new XPathException("Unknown accumulator name: " + token, "XTSE3300"); |
| 64 |
} |
| 65 |
names.add(name); |
| 66 |
accumulators.add(acc); |
| 67 |
} |
| 68 |
} |
| 69 |
return accumulators; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public void addAccumulator(Accumulator acc) { |
| 79 |
if (acc.getAccumulatorName() != null) { |
| 80 |
accumulatorsByName.put(acc.getAccumulatorName(), acc); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public Accumulator getAccumulator(StructuredQName name) { |
| 91 |
return accumulatorsByName.get(name); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public Iterable<Accumulator> getAllAccumulators() { |
| 99 |
return accumulatorsByName.values(); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public Sequence<?> getStreamingAccumulatorValue(NodeInfo node, Accumulator accumulator, AccumulatorFn.Phase phase) |
| 113 |
throws XPathException { |
| 114 |
return null; |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
|