Debian Codegraph @

net/sf/saxon/trans/rules/ShallowCopyRuleSet.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.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 * The built-in rule set introduced in XSLT 3.0, which is effectively an identity template.
26 */
27 public class ShallowCopyRuleSet implements BuiltInRuleSet {
28
29 private static ShallowCopyRuleSet THE_INSTANCE = new ShallowCopyRuleSet();
30
31 /**
32 * Get the singleton instance of this class
33 *
34 * @return the singleton instance
35 */
36
37 public static ShallowCopyRuleSet getInstance() {
38 return THE_INSTANCE;
39 }
40
41 private ShallowCopyRuleSet() {
42 }
43
44 /**
45 * Perform the built-in template action for a given node.
46 * @param item
47 * @param parameters the parameters supplied to apply-templates
48 * @param tunnelParams the tunnel parameters to be passed through
49 * @param context the dynamic evaluation context
50 * @param locationId location of the instruction (apply-templates, apply-imports etc) that caused
51 * the built-in template to be invoked @throws net.sf.saxon.trans.XPathException
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()); // Bug 3508
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()); // Bug 3508
92 pipe.setXPathContext(c2);
93
94 // apply-templates to all attributes
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 // apply-templates to all children
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 * Identify this built-in rule set
139 *
140 * @return "shallow-copy"
141 */
142
143 @Override
144 public String getName() {
145 return "shallow-copy";
146 }
147
148 /**
149 * Get the default action for unmatched nodes
150 *
151 * @param nodeKind the node kind
152 * @return the default action for unmatched nodes: one of DEEP_COPY, APPLY_TEMPLATES, DEEP_SKIP, FAIL
153 */
154 public int[] getActionForParentNodes(int nodeKind) {
155 return new int[]{SHALLOW_COPY, APPLY_TEMPLATES_TO_ATTRIBUTES, APPLY_TEMPLATES_TO_CHILDREN};
156 }
157 }