Debian Codegraph @

net/sf/saxon/tree/tiny/GraftedElement.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
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.expr.parser.Location;
13 import net.sf.saxon.om.*;
14 import net.sf.saxon.pattern.AnyNodeTest;
15 import net.sf.saxon.pattern.NodeTest;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.tree.iter.AxisIterator;
18 import net.sf.saxon.tree.util.Navigator;
19 import net.sf.saxon.tree.wrapper.VirtualCopy;
20
21 import java.util.Optional;
22
23 /**
24 * This class represents an element node in a TinyTree that is actually a reference to an element
25 * node held elsewhere. Some properties of the node come from the containing TinyTree (for example,
26 * document number, base URI, etc) while others are actually properties of the external node. Notably,
27 * the parent and siblings of the node are found in the containing tree, while the children and
28 * descendants (and attributes) are found in the external tree.
29 */
30
31 public class GraftedElement extends TinyElementImpl {
32
33 private NodeInfo externalNode;
34 private VirtualCopy externalNodeCopy;
35 private Optional<Boolean> hasDefaultNamespace = Optional.empty();
36
37 /**
38 * Created a grafted element node
39 * @param tree the host tree that is to contain a reference to an external node
40 * @param nodeNr the node number at which the external reference is to appear
41 * @param externalNode the external node, in some other tree
42 * @param copyNamespaces indicates whether namespaces in the external tree are
43 * retained in the virtual copy.
44 */
45
46 public GraftedElement(TinyTree tree, int nodeNr, NodeInfo externalNode, boolean copyNamespaces) {
47 super(tree, nodeNr);
48 this.externalNode = externalNode;
49 this.externalNodeCopy = VirtualCopy.makeVirtualCopy(externalNode, this);
50 this.externalNodeCopy.setDropNamespaces(!copyNamespaces);
51 }
52
53 public NodeInfo getExternalNode() {
54 return externalNode;
55 }
56
57 @Override
58 public boolean hasChildNodes() {
59 return externalNodeCopy.hasChildNodes();
60 }
61
62 @Override
63 public CharSequence getStringValueCS() {
64 return externalNode.getStringValueCS();
65 }
66
67 @Override
68 public String getStringValue() {
69 return externalNode.getStringValue();
70 }
71
72 @Override
73 public AtomicSequence atomize() throws XPathException {
74 return externalNode.atomize();
75 }
76
77 @Override
78 public void copy(Receiver receiver, int copyOptions, Location location) throws XPathException {
79 externalNodeCopy.copy(receiver, copyOptions, location);
80 }
81
82 @Override
83 public AxisIterator iterateAxis(byte axisNumber) {
84 return iterateAxis(axisNumber, AnyNodeTest.getInstance());
85 }
86
87 @Override
88 public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) {
89 switch (axisNumber) {
90 case AxisInfo.ANCESTOR:
91 return super.iterateAxis(axisNumber, nodeTest);
92
93 case AxisInfo.ANCESTOR_OR_SELF:
94 return super.iterateAxis(axisNumber, nodeTest);
95
96 case AxisInfo.ATTRIBUTE:
97 return externalNodeCopy.iterateAxis(axisNumber, nodeTest);
98
99 case AxisInfo.CHILD:
100 return externalNodeCopy.iterateAxis(axisNumber, nodeTest);
101
102 case AxisInfo.DESCENDANT:
103 return externalNodeCopy.iterateAxis(axisNumber, nodeTest);
104
105 case AxisInfo.DESCENDANT_OR_SELF:
106 return externalNodeCopy.iterateAxis(axisNumber, nodeTest);
107
108 case AxisInfo.FOLLOWING:
109 return super.iterateAxis(axisNumber, nodeTest);
110
111 case AxisInfo.FOLLOWING_SIBLING:
112 return super.iterateAxis(axisNumber, nodeTest);
113
114 case AxisInfo.NAMESPACE:
115 return externalNodeCopy.iterateAxis(axisNumber, nodeTest);
116
117 case AxisInfo.PARENT:
118 return super.iterateAxis(axisNumber, nodeTest);
119
120 case AxisInfo.PRECEDING:
121 return super.iterateAxis(axisNumber, nodeTest);
122
123 case AxisInfo.PRECEDING_SIBLING:
124 return super.iterateAxis(axisNumber, nodeTest);
125
126 case AxisInfo.SELF:
127 return Navigator.filteredSingleton(this, nodeTest);
128
129 case AxisInfo.PRECEDING_OR_ANCESTOR:
130 return super.iterateAxis(axisNumber, nodeTest);
131
132 default:
133 throw new IllegalArgumentException("Unknown axis number " + axisNumber);
134 }
135 }
136
137 /**
138 * Get the value of the attribute with a given fingerprint.
139 *
140 * @param fp the fingerprint of the required attribute
141 * @return the string value of the attribute if present, or null if absent
142 */
143
144 public String getAttributeValue(int fp) {
145 NodeInfo actual = externalNodeCopy.getOriginalNode();
146 if (actual instanceof TinyElementImpl) {
147 return ((TinyElementImpl) actual).getAttributeValue(fp);
148 } else {
149 StructuredQName qn = getNamePool().getStructuredQName(fp);
150 return actual.getAttributeValue(qn.getURI(), qn.getLocalPart());
151 }
152 }
153
154 @Override
155 public String getAttributeValue(String uri, String local) {
156 return externalNode.getAttributeValue(uri, local);
157 }
158
159 public boolean hasDefaultNamespace() {
160 if (!hasDefaultNamespace.isPresent()) {
161 hasDefaultNamespace = Optional.of(
162 !new InscopeNamespaceResolver(getParent()).getURIForPrefix("", true).isEmpty());
163 }
164 return hasDefaultNamespace.get();
165 }
166
167 @Override
168 public NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] buffer) {
169 return externalNodeCopy.getDeclaredNamespaces(buffer);
170 }
171 }
172