| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.trans.rules; |
| 9 |
|
| 10 |
import net.sf.saxon.event.Receiver; |
| 11 |
import net.sf.saxon.expr.XPathContext; |
| 12 |
import net.sf.saxon.expr.instruct.ParameterSet; |
| 13 |
import net.sf.saxon.expr.parser.Location; |
| 14 |
import net.sf.saxon.om.CopyOptions; |
| 15 |
import net.sf.saxon.om.Item; |
| 16 |
import net.sf.saxon.om.NodeInfo; |
| 17 |
import net.sf.saxon.trans.XPathException; |
| 18 |
import net.sf.saxon.type.Type; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public class DeepCopyRuleSet implements BuiltInRuleSet { |
| 24 |
|
| 25 |
private static DeepCopyRuleSet THE_INSTANCE = new DeepCopyRuleSet(); |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public static DeepCopyRuleSet getInstance() { |
| 34 |
return THE_INSTANCE; |
| 35 |
} |
| 36 |
|
| 37 |
private DeepCopyRuleSet() { |
| 38 |
} |
| 39 |
|
| 40 |
private static int COPY_OPTIONS = CopyOptions.TYPE_ANNOTATIONS | CopyOptions.ALL_NAMESPACES; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public void process(Item item, ParameterSet parameters, |
| 52 |
ParameterSet tunnelParams, XPathContext context, |
| 53 |
Location locationId) throws XPathException { |
| 54 |
if (item instanceof NodeInfo) { |
| 55 |
NodeInfo node = (NodeInfo) item; |
| 56 |
Receiver out = context.getReceiver(); |
| 57 |
switch (node.getNodeKind()) { |
| 58 |
case Type.DOCUMENT: |
| 59 |
case Type.ELEMENT: { |
| 60 |
if (out.getSystemId() == null) { |
| 61 |
out.setSystemId(node.getBaseURI()); |
| 62 |
} |
| 63 |
node.copy(out, COPY_OPTIONS, locationId); |
| 64 |
return; |
| 65 |
} |
| 66 |
case Type.TEXT: |
| 67 |
out.characters(item.getStringValueCS(), locationId, 0); |
| 68 |
return; |
| 69 |
|
| 70 |
case Type.ATTRIBUTE: |
| 71 |
case Type.COMMENT: |
| 72 |
case Type.PROCESSING_INSTRUCTION: |
| 73 |
case Type.NAMESPACE: |
| 74 |
node.copy(out, COPY_OPTIONS, locationId); |
| 75 |
return; |
| 76 |
|
| 77 |
default: |
| 78 |
} |
| 79 |
} else { |
| 80 |
context.getReceiver().append(item, locationId, 0); |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public int[] getActionForParentNodes(int nodeKind) { |
| 92 |
return new int[]{DEEP_COPY}; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
@Override |
| 101 |
public String getName() { |
| 102 |
return "deep-copy"; |
| 103 |
} |
| 104 |
} |