| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.event.BuilderMonitor; |
| 11 |
import net.sf.saxon.event.Receiver; |
| 12 |
import net.sf.saxon.expr.Expression; |
| 13 |
import net.sf.saxon.expr.ItemMappingFunction; |
| 14 |
import net.sf.saxon.expr.ItemMappingIterator; |
| 15 |
import net.sf.saxon.expr.XPathContext; |
| 16 |
import net.sf.saxon.expr.parser.ExplicitLocation; |
| 17 |
import net.sf.saxon.om.*; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.tree.tiny.TinyBuilder; |
| 20 |
import net.sf.saxon.tree.wrapper.SnapshotNode; |
| 21 |
import net.sf.saxon.tree.wrapper.VirtualCopy; |
| 22 |
import net.sf.saxon.type.AnyType; |
| 23 |
import net.sf.saxon.type.SchemaType; |
| 24 |
import net.sf.saxon.type.Type; |
| 25 |
import net.sf.saxon.type.Untyped; |
| 26 |
|
| 27 |
import java.util.ArrayList; |
| 28 |
import java.util.Iterator; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class SnapshotFn extends SystemFunction { |
| 35 |
|
| 36 |
public int getCardinality(Expression[] arguments) { |
| 37 |
return arguments[0].getCardinality(); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public Sequence<?> call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 50 |
Sequence<?> in = arguments.length == 0 ? context.getContextItem() : arguments[0]; |
| 51 |
SequenceIterator<?> iter = snapshotSequence(in.iterate(), context); |
| 52 |
return new LazySequence<>(iter); |
| 53 |
} |
| 54 |
|
| 55 |
public static SequenceIterator<?> snapshotSequence(SequenceIterator<?> nodes, final XPathContext context) { |
| 56 |
return new ItemMappingIterator(nodes, getMappingFunction()); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static ItemMappingFunction<Item<?>, Item<?>> getMappingFunction() { |
| 65 |
return SnapshotFn::snapshotSingle; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public static Item snapshotSingle(Item origin) { |
| 75 |
if (origin instanceof NodeInfo) { |
| 76 |
if (((NodeInfo)origin).getParent() == null) { |
| 77 |
VirtualCopy vc = VirtualCopy.makeVirtualCopy((NodeInfo) origin); |
| 78 |
vc.getTreeInfo().setCopyAccumulators(true); |
| 79 |
return vc; |
| 80 |
} else { |
| 81 |
return SnapshotNode.makeSnapshot((NodeInfo) origin); |
| 82 |
} |
| 83 |
} else { |
| 84 |
return origin; |
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
public static List<NodeInfo> makeAncestorList(NodeInfo origin) { |
| 90 |
List<NodeInfo> ancestors = new ArrayList<NodeInfo>(20); |
| 91 |
origin.iterateAxis(AxisInfo.ANCESTOR).forEach(ancestors::add); |
| 92 |
return ancestors; |
| 93 |
} |
| 94 |
|
| 95 |
public static BuilderMonitor openAncestors(NodeInfo origin, List<NodeInfo> ancestors, XPathContext context) throws XPathException { |
| 96 |
NodeInfo root = origin.getRoot(); |
| 97 |
TinyBuilder builder = new TinyBuilder(context.getController().makePipelineConfiguration()); |
| 98 |
builder.setStatistics(context.getConfiguration().getTreeStatistics().TEMPORARY_TREE_STATISTICS); |
| 99 |
builder.setSystemId(root.getSystemId()); |
| 100 |
builder.setTiming(false); |
| 101 |
|
| 102 |
BuilderMonitor bm = builder.getBuilderMonitor(); |
| 103 |
bm.open(); |
| 104 |
|
| 105 |
TreeInfo source = root.getTreeInfo(); |
| 106 |
Iterator<String> unparsedEntities = source.getUnparsedEntityNames(); |
| 107 |
while (unparsedEntities.hasNext()) { |
| 108 |
String name = unparsedEntities.next(); |
| 109 |
String[] properties = source.getUnparsedEntity(name); |
| 110 |
builder.setUnparsedEntity(name, properties[0], properties[1]); |
| 111 |
} |
| 112 |
|
| 113 |
SchemaType ancestorType = context.getController().getExecutable().isSchemaAware() ? |
| 114 |
AnyType.getInstance() : Untyped.getInstance(); |
| 115 |
for (int i = ancestors.size() - 1; i >= 0; i--) { |
| 116 |
NodeInfo anc = ancestors.get(i); |
| 117 |
int kind = anc.getNodeKind(); |
| 118 |
switch (kind) { |
| 119 |
|
| 120 |
case Type.ELEMENT: { |
| 121 |
bm.startElement(NameOfNode.makeName(anc), ancestorType, ExplicitLocation.UNKNOWN_LOCATION, 0); |
| 122 |
NamespaceBinding[] localDeclarations = anc.getDeclaredNamespaces(null); |
| 123 |
for (int n = 0; n < localDeclarations.length; ) { |
| 124 |
NamespaceBinding nsCode = localDeclarations[n++]; |
| 125 |
if (nsCode == null) { |
| 126 |
break; |
| 127 |
} |
| 128 |
bm.namespace(nsCode, 0); |
| 129 |
} |
| 130 |
anc.iterateAxis(AxisInfo.ATTRIBUTE).forEachOrFail( |
| 131 |
att -> att.copy(bm, 0, ExplicitLocation.UNKNOWN_LOCATION) |
| 132 |
); |
| 133 |
bm.startContent(); |
| 134 |
break; |
| 135 |
} |
| 136 |
|
| 137 |
case Type.DOCUMENT: { |
| 138 |
bm.startDocument(0); |
| 139 |
break; |
| 140 |
} |
| 141 |
default: { |
| 142 |
throw new IllegalStateException("Unknown ancestor node kind " + anc.getNodeKind()); |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
return bm; |
| 147 |
} |
| 148 |
|
| 149 |
public static void closeAncestors(List<NodeInfo> ancestors, Receiver bm) throws XPathException { |
| 150 |
for (NodeInfo anc : ancestors) { |
| 151 |
switch (anc.getNodeKind()) { |
| 152 |
case Type.ELEMENT: { |
| 153 |
bm.endElement(); |
| 154 |
break; |
| 155 |
} |
| 156 |
case Type.DOCUMENT: { |
| 157 |
bm.endDocument(); |
| 158 |
break; |
| 159 |
} |
| 160 |
default: { |
| 161 |
throw new IllegalStateException("Unknown ancestor node kind " + anc.getNodeKind()); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
bm.close(); |
| 166 |
} |
| 167 |
|
| 168 |
public String getStreamerName() { |
| 169 |
return "SnapshotFn"; |
| 170 |
} |
| 171 |
} |
| 172 |
|