| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.trans.rules; |
| 9 |
|
| 10 |
import net.sf.saxon.event.PipelineConfiguration; |
| 11 |
import net.sf.saxon.event.Receiver; |
| 12 |
import net.sf.saxon.expr.XPathContext; |
| 13 |
import net.sf.saxon.expr.XPathContextMajor; |
| 14 |
import net.sf.saxon.expr.instruct.ParameterSet; |
| 15 |
import net.sf.saxon.expr.instruct.TailCall; |
| 16 |
import net.sf.saxon.expr.parser.Location; |
| 17 |
import net.sf.saxon.om.*; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 20 |
import net.sf.saxon.tree.iter.EmptyIterator; |
| 21 |
import net.sf.saxon.tree.util.NamespaceIterator; |
| 22 |
import net.sf.saxon.type.Type; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class ShallowCopyRuleSet implements BuiltInRuleSet { |
| 28 |
|
| 29 |
private static ShallowCopyRuleSet THE_INSTANCE = new ShallowCopyRuleSet(); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public static ShallowCopyRuleSet getInstance() { |
| 38 |
return THE_INSTANCE; |
| 39 |
} |
| 40 |
|
| 41 |
private ShallowCopyRuleSet() { |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public void process(Item item, ParameterSet parameters, |
| 55 |
ParameterSet tunnelParams, XPathContext context, |
| 56 |
Location locationId) throws XPathException { |
| 57 |
if (item instanceof NodeInfo) { |
| 58 |
NodeInfo node = (NodeInfo) item; |
| 59 |
switch (node.getNodeKind()) { |
| 60 |
case Type.DOCUMENT: { |
| 61 |
Receiver out = context.getReceiver(); |
| 62 |
PipelineConfiguration pipe = out.getPipelineConfiguration(); |
| 63 |
if (out.getSystemId() == null) { |
| 64 |
out.setSystemId(node.getBaseURI()); |
| 65 |
} |
| 66 |
out.startDocument(0); |
| 67 |
XPathContextMajor c2 = context.newContext(); |
| 68 |
c2.setOrigin(this); |
| 69 |
c2.trackFocus(node.iterateAxis(AxisInfo.CHILD)); |
| 70 |
c2.setCurrentComponent(c2.getCurrentMode()); |
| 71 |
pipe.setXPathContext(c2); |
| 72 |
TailCall tc = context.getCurrentMode().getActor().applyTemplates(parameters, tunnelParams, c2, locationId); |
| 73 |
while (tc != null) { |
| 74 |
tc = tc.processLeavingTail(); |
| 75 |
} |
| 76 |
out.endDocument(); |
| 77 |
pipe.setXPathContext(context); |
| 78 |
return; |
| 79 |
} |
| 80 |
case Type.ELEMENT: { |
| 81 |
Receiver out = context.getReceiver(); |
| 82 |
PipelineConfiguration pipe = out.getPipelineConfiguration(); |
| 83 |
if (out.getSystemId() == null) { |
| 84 |
out.setSystemId(node.getBaseURI()); |
| 85 |
} |
| 86 |
NodeName fqn = NameOfNode.makeName(node); |
| 87 |
out.startElement(fqn, node.getSchemaType(), locationId, 0); |
| 88 |
NamespaceIterator.sendNamespaces(node, out); |
| 89 |
|
| 90 |
XPathContextMajor c2 = context.newContext(); |
| 91 |
c2.setCurrentComponent(c2.getCurrentMode()); |
| 92 |
pipe.setXPathContext(c2); |
| 93 |
|
| 94 |
|
| 95 |
AxisIterator attributes = node.iterateAxis(AxisInfo.ATTRIBUTE); |
| 96 |
if (attributes != EmptyIterator.OfNodes.THE_INSTANCE) { |
| 97 |
c2.setOrigin(this); |
| 98 |
c2.trackFocus(attributes); |
| 99 |
TailCall tc = c2.getCurrentMode().getActor().applyTemplates(parameters, tunnelParams, c2, locationId); |
| 100 |
while (tc != null) { |
| 101 |
tc = tc.processLeavingTail(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
if (node.hasChildNodes()) { |
| 107 |
c2.trackFocus(node.iterateAxis(AxisInfo.CHILD)); |
| 108 |
TailCall tc = c2.getCurrentMode().getActor().applyTemplates(parameters, tunnelParams, c2, locationId); |
| 109 |
while (tc != null) { |
| 110 |
tc = tc.processLeavingTail(); |
| 111 |
} |
| 112 |
} |
| 113 |
out.endElement(); |
| 114 |
pipe.setXPathContext(context); |
| 115 |
return; |
| 116 |
} |
| 117 |
case Type.TEXT: |
| 118 |
context.getReceiver().characters(item.getStringValueCS(), locationId, 0); |
| 119 |
return; |
| 120 |
|
| 121 |
case Type.ATTRIBUTE: |
| 122 |
case Type.COMMENT: |
| 123 |
case Type.PROCESSING_INSTRUCTION: |
| 124 |
case Type.NAMESPACE: |
| 125 |
node.copy(context.getReceiver(), 0, locationId); |
| 126 |
return; |
| 127 |
|
| 128 |
default: |
| 129 |
} |
| 130 |
} else { |
| 131 |
context.getReceiver().append(item, locationId, 0); |
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
@Override |
| 144 |
public String getName() { |
| 145 |
return "shallow-copy"; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public int[] getActionForParentNodes(int nodeKind) { |
| 155 |
return new int[]{SHALLOW_COPY, APPLY_TEMPLATES_TO_ATTRIBUTES, APPLY_TEMPLATES_TO_CHILDREN}; |
| 156 |
} |
| 157 |
} |