Debian Codegraph @

net/sf/saxon/trans/rules/DeepCopyRuleSet.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * The built-in rule set introduced in XSLT 3.0, which performs a deep copy of any unmatched node.
22 */
23 public class DeepCopyRuleSet implements BuiltInRuleSet {
24
25 private static DeepCopyRuleSet THE_INSTANCE = new DeepCopyRuleSet();
26
27 /**
28 * Get the singleton instance of this class
29 *
30 * @return the singleton instance
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 * Perform the built-in template action for a given node.
43 * @param item
44 * @param parameters the parameters supplied to apply-templates
45 * @param tunnelParams the tunnel parameters to be passed through
46 * @param context the dynamic evaluation context
47 * @param locationId location of the instruction (apply-templates, apply-imports etc) that caused
48 * the built-in template to be invoked @throws net.sf.saxon.trans.XPathException
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 * Get the default action for unmatched nodes
87 *
88 * @param nodeKind the node kind
89 * @return the default action for unmatched element nodes: one of DEEP_COPY, APPLY_TEMPLATES, SKIP, FAIL
90 */
91 public int[] getActionForParentNodes(int nodeKind) {
92 return new int[]{DEEP_COPY};
93 }
94
95 /**
96 * Identify this built-in rule set
97 * @return "deep-copy"
98 */
99
100 @Override
101 public String getName() {
102 return "deep-copy";
103 }
104 }