Debian Codegraph @

net/sf/saxon/tree/tiny/TinyBuilderMonitor.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.tiny;
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.trans.XPathException;
15 import net.sf.saxon.tree.iter.AxisIterator;
16 import net.sf.saxon.type.SchemaType;
17 import net.sf.saxon.type.SimpleType;
18 import net.sf.saxon.type.Type;
19
20 /**
21 * Monitor construction of a TinyTree. This allows a marker to be set during tree construction, in such a way
22 * that the node corresponding to the marker can be retrieved at the end of tree construction. This is used in the
23 * implementation of the XSLT 3.0 snapshot function.
24 */
25 public class TinyBuilderMonitor extends BuilderMonitor {
26
27 private TinyBuilder builder;
28 private int mark = -1;
29 private int markedNodeNr = -1;
30 private int markedAttribute = -1;
31 private int markedNamespace = -1;
32
33 public TinyBuilderMonitor(/*@NotNull*/ TinyBuilder builder) {
34 super(builder);
35 this.builder = builder;
36 }
37
38 public void markNextNode(int nodeKind) {
39 mark = nodeKind;
40 }
41
42 @Override
43 public void markAttributeNode(NodeInfo origin) {
44 int[] atts = builder.getTree().getAttributeNameCodeArray();
45 int used = builder.getTree().getNumberOfAttributes();
46 int fp = origin.getFingerprint();
47 for (int i=used-1; i>=0; i--) {
48 if ((atts[i] & NamePool.FP_MASK) == fp) {
49 markedAttribute = i;
50 return;
51 }
52 }
53 throw new IllegalStateException("Selected attribute not found");
54 }
55
56 @Override
57 public void markNamespaceNode(NodeInfo origin) {
58 NamespaceBinding[] atts = builder.getTree().getNamespaceBindings();
59 int used = builder.getTree().getNumberOfNamespaces();
60 String local = origin.getLocalPart();
61 for (int i=used-1; i>=0; i--) {
62 if (atts[i].getPrefix().equals(local)) {
63 markedNamespace = i;
64 return;
65 }
66 }
67 throw new IllegalStateException("Selected namespace not found");
68 }
69
70 public void startDocument(int properties) throws XPathException {
71 if (mark == Type.DOCUMENT) {
72 markedNodeNr = builder.getTree().getNumberOfNodes();
73 }
74 mark = -1;
75 super.startDocument(properties);
76 }
77
78 public void startElement(NodeName nameCode, SchemaType typeCode, Location location, int properties) throws XPathException {
79 if (mark == Type.ELEMENT) {
80 markedNodeNr = builder.getTree().getNumberOfNodes();
81 }
82 mark = -1;
83 super.startElement(nameCode, typeCode, location, properties);
84 }
85
86 public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
87 if (mark == Type.TEXT) {
88 markedNodeNr = builder.getTree().getNumberOfNodes();
89 }
90 mark = -1;
91 super.characters(chars, locationId, properties);
92 }
93
94 public void comment(CharSequence chars, Location locationId, int properties) throws XPathException {
95 if (mark == Type.COMMENT) {
96 markedNodeNr = builder.getTree().getNumberOfNodes();
97 }
98 mark = -1;
99 super.comment(chars, locationId, properties);
100 }
101
102 public void processingInstruction(String target, CharSequence data, Location locationId, int properties) throws XPathException {
103 if (mark == Type.PROCESSING_INSTRUCTION) {
104 markedNodeNr = builder.getTree().getNumberOfNodes();
105 }
106 mark = -1;
107 super.processingInstruction(target, data, locationId, properties);
108 }
109
110 public void attribute(NodeName nameCode, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException {
111 if (mark == Type.ATTRIBUTE) {
112 markedAttribute = builder.getTree().getNumberOfAttributes();
113 }
114 mark = -1;
115 super.attribute(nameCode, typeCode, value, locationId, properties);
116 }
117
118 public void namespace(NamespaceBindingSet namespaceBindings, int properties) throws XPathException {
119 if (mark == Type.NAMESPACE) {
120 markedNamespace = builder.getTree().getNumberOfNamespaces();
121 }
122 mark = -1;
123 super.namespace(namespaceBindings, properties);
124 }
125
126 /*@Nullable*/
127 public NodeInfo getMarkedNode() {
128 if (markedNodeNr != -1) {
129 return builder.getTree().getNode(markedNodeNr);
130 } else if (markedAttribute != -1) {
131 return builder.getTree().getAttributeNode(markedAttribute);
132 } else if (markedNamespace != -1) {
133 NamespaceBinding nscode = builder.getTree().namespaceBinding[markedNamespace];
134 NamePool pool = builder.getConfiguration().getNamePool();
135 String prefix = nscode.getPrefix();
136 NodeInfo parent = builder.getTree().getNode(builder.getTree().namespaceParent[markedNamespace]);
137 NameTest test = new NameTest(Type.NAMESPACE, "", prefix, pool);
138 AxisIterator iter = parent.iterateAxis(AxisInfo.NAMESPACE, test);
139 return iter.next();
140 } else {
141 return null;
142 }
143 }
144 }
145