Debian Codegraph @

net/sf/saxon/tree/linked/LinkedBuilderMonitor.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.tree.linked;
9
10 import net.sf.saxon.event.BuilderMonitor;
11 import net.sf.saxon.expr.parser.Location;
12 import net.sf.saxon.om.*;
13 import net.sf.saxon.pattern.NameTest;
14 import net.sf.saxon.pattern.SameNameTest;
15 import net.sf.saxon.trans.XPathException;
16 import net.sf.saxon.type.SimpleType;
17 import net.sf.saxon.type.Type;
18
19 /**
20 * Monitor construction of a TinyTree. This allows a marker to be set during tree construction, in such a way
21 * that the node corresponding to the marker can be retrieved at the end of tree construction. This is used in the
22 * implementation of the XSLT 3.0 snapshot function.
23 */
24 public class LinkedBuilderMonitor extends BuilderMonitor {
25
26 private LinkedTreeBuilder builder;
27 private int mark = -1;
28 /*@Nullable*/ private NodeInfo markedNode;
29
30 public LinkedBuilderMonitor(/*@NotNull*/ LinkedTreeBuilder builder) {
31 super(builder);
32 this.builder = builder;
33 }
34
35 public void markNextNode(int nodeKind) {
36 mark = nodeKind;
37 }
38
39 @Override
40 public void markAttributeNode(NodeInfo origin) {
41 NodeInfo element = builder.getCurrentParentNode();
42 markedNode = element.iterateAxis(
43 AxisInfo.ATTRIBUTE, new SameNameTest(origin)).next();
44 mark = -1;
45 }
46
47 @Override
48 public void markNamespaceNode(NodeInfo origin) {
49 NodeInfo element = builder.getCurrentParentNode();
50 NamePool pool = builder.getConfiguration().getNamePool();
51 String prefix = origin.getLocalPart();
52 markedNode = element.iterateAxis(
53 AxisInfo.NAMESPACE, new NameTest(Type.NAMESPACE, "", prefix, pool)).next();
54 mark = -1;
55 }
56
57 public void startDocument(int properties) throws XPathException {
58 super.startDocument(properties);
59 if (mark == Type.DOCUMENT) {
60 markedNode = builder.getCurrentParentNode();
61 }
62 mark = -1;
63 }
64
65
66 public void startContent() throws XPathException {
67 super.startContent();
68 if (mark == Type.ELEMENT) {
69 markedNode = builder.getCurrentParentNode();
70 }
71 mark = -1;
72 }
73
74 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
75 super.characters(chars, locationId, properties);
76 if (mark == Type.TEXT) {
77 markedNode = builder.getCurrentLeafNode();
78 }
79 mark = -1;
80 }
81
82 public void comment(CharSequence chars, Location locationId, int properties) throws XPathException {
83 super.comment(chars, locationId, properties);
84 if (mark == Type.COMMENT) {
85 markedNode = builder.getCurrentLeafNode();
86 }
87 mark = -1;
88 }
89
90 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
91 super.processingInstruction(target, data, locationId, properties);
92 if (mark == Type.PROCESSING_INSTRUCTION) {
93 markedNode = builder.getCurrentLeafNode();
94 }
95 mark = -1;
96 }
97
98 public void attribute(NodeName name, SimpleType typeCode, CharSequence value, Location locationId, int properties)
99 throws XPathException {
100 super.attribute(name, typeCode, value, locationId, properties);
101 if (mark == Type.ATTRIBUTE) {
102 NodeInfo element = builder.getCurrentParentNode();
103 markedNode = element.iterateAxis(
104 AxisInfo.ATTRIBUTE, new NameTest(Type.ATTRIBUTE, name, builder.getConfiguration().getNamePool())).next();
105 mark = -1;
106 }
107 }
108
109 public void namespace(/*@NotNull*/ NamespaceBindingSet namespaceBindings, int properties) throws XPathException {
110 super.namespace(namespaceBindings, properties);
111 if (mark == Type.NAMESPACE) {
112 for (NamespaceBinding ns : namespaceBindings) {
113 NodeInfo element = builder.getCurrentParentNode();
114 NamePool pool = builder.getConfiguration().getNamePool();
115 String prefix = ns.getPrefix();
116 markedNode = element.iterateAxis(
117 AxisInfo.NAMESPACE, new NameTest(Type.NAMESPACE, "", prefix, pool)).next();
118 mark = -1;
119 }
120 }
121 }
122
123 /*@Nullable*/
124 public NodeInfo getMarkedNode() {
125 return markedNode;
126 }
127 }
128