Debian Codegraph @

net/sf/saxon/trans/rules/TextOnlyCopyRuleSet.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.expr.XPathContext;
12 import net.sf.saxon.expr.XPathContextMajor;
13 import net.sf.saxon.expr.instruct.ParameterSet;
14 import net.sf.saxon.expr.instruct.TailCall;
15 import net.sf.saxon.expr.parser.Location;
16 import net.sf.saxon.ma.arrays.ArrayFunctionSet;
17 import net.sf.saxon.ma.arrays.ArrayItem;
18 import net.sf.saxon.om.*;
19 import net.sf.saxon.trans.XPathException;
20 import net.sf.saxon.type.Type;
21 import net.sf.saxon.value.AtomicValue;
22
23 /**
24 * The built-in rule set used for 1.0 and 2.0, which for document and element nodes does an apply-templates
25 * to children, and for text nodes and attribute nodes copies the node.
26 */
27 public class TextOnlyCopyRuleSet implements BuiltInRuleSet {
28
29 private static TextOnlyCopyRuleSet THE_INSTANCE = new TextOnlyCopyRuleSet();
30
31 /**
32 * Get the singleton instance of this class
33 *
34 * @return the singleton instance
35 */
36
37 public static TextOnlyCopyRuleSet getInstance() {
38 return THE_INSTANCE;
39 }
40
41 private TextOnlyCopyRuleSet() {
42 }
43
44 /**
45 * Perform the built-in template action for a given item.
46 *
47 * @param item the item to be processed
48 * @param parameters the parameters supplied to apply-templates
49 * @param tunnelParams the tunnel parameters to be passed through
50 * @param context the dynamic evaluation context
51 * @param locationId location of the instruction (apply-templates, apply-imports etc) that caused
52 * the built-in template to be invoked
53 * @throws net.sf.saxon.trans.XPathException
54 * if any dynamic error occurs
55 */
56
57 public void process(Item item, ParameterSet parameters,
58 ParameterSet tunnelParams, /*@NotNull*/ XPathContext context,
59 Location locationId) throws XPathException {
60 if (item instanceof NodeInfo) {
61 NodeInfo node = (NodeInfo) item;
62 switch (node.getNodeKind()) {
63 case Type.DOCUMENT:
64 case Type.ELEMENT:
65 PipelineConfiguration pipe = context.getReceiver().getPipelineConfiguration();
66 XPathContextMajor c2 = context.newContext();
67 c2.setOrigin(this);
68 c2.trackFocus(node.iterateAxis(AxisInfo.CHILD));
69 c2.setCurrentComponent(c2.getCurrentMode()); // Bug 3508
70 pipe.setXPathContext(c2);
71 TailCall tc = c2.getCurrentMode().getActor().applyTemplates(parameters, tunnelParams, c2, locationId);
72 while (tc != null) {
73 tc = tc.processLeavingTail();
74 }
75 pipe.setXPathContext(context);
76 return;
77 case Type.TEXT:
78 // NOTE: I tried changing this to use the text node's copy() method, but
79 // performance was worse
80 case Type.ATTRIBUTE:
81 context.getReceiver().characters(item.getStringValueCS(), locationId, 0);
82 return;
83 case Type.COMMENT:
84 case Type.PROCESSING_INSTRUCTION:
85 case Type.NAMESPACE:
86 // no action
87 }
88 } else if (item instanceof ArrayItem) {
89 Sequence<?> seq = ArrayFunctionSet.ArrayToSequence.toSequence((ArrayItem)item);
90 SequenceIterator<?> members = seq.iterate();
91 XPathContextMajor c2 = context.newContext();
92 c2.setOrigin(this);
93 c2.trackFocus(members);
94 c2.setCurrentComponent(c2.getCurrentMode()); // Bug 3508
95 TailCall tc = c2.getCurrentMode().getActor().applyTemplates(parameters, tunnelParams, c2, locationId);
96 while (tc != null) {
97 tc = tc.processLeavingTail();
98 }
99 } else if (item instanceof AtomicValue) {
100 context.getReceiver().characters(item.getStringValueCS(), locationId, 0);
101 } else {
102 // no action (e.g. for function items
103 }
104 }
105
106
107 /**
108 * Identify this built-in rule set
109 *
110 * @return "text-only"
111 */
112
113 @Override
114 public String getName() {
115 return "text-only";
116 }
117
118 /**
119 * Get the default action for unmatched nodes
120 *
121 * @param nodeKind the node kind
122 * @return the default action for unmatched nodes: one of DEEP_COPY, SHALLOW_SKIP, DEEP_SKIP, FAIL, etc
123 */
124 public int[] getActionForParentNodes(int nodeKind) {
125 return new int[]{APPLY_TEMPLATES_TO_CHILDREN};
126 }
127 }
128