Debian Codegraph @

net/sf/saxon/tree/tiny/TinyParentNodeImpl.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.tree.util.FastStringBuffer;
11 import net.sf.saxon.type.Type;
12
13 /**
14 * TinyParentNodeImpl is an implementation of a non-leaf node (specifically, an Element node
15 * or a Document node)
16 *
17 * @author Michael H. Kay
18 */
19
20
21 public abstract class TinyParentNodeImpl extends TinyNodeImpl {
22
23 /**
24 * Determine if the node has children.
25 */
26
27 public boolean hasChildNodes() {
28 return nodeNr + 1 < tree.numberOfNodes &&
29 tree.depth[nodeNr + 1] > tree.depth[nodeNr];
30 }
31
32 /**
33 * Return the string-value of the node, that is, the concatenation
34 * of the character content of all descendent elements and text nodes.
35 *
36 * @return the accumulated character content of the element, including descendant elements.
37 */
38
39 public String getStringValue() {
40 return getStringValueCS(tree, nodeNr).toString();
41 }
42
43 /**
44 * Get the value of the item as a CharSequence. This is in some cases more efficient than
45 * the version of the method that returns a String.
46 */
47
48 public CharSequence getStringValueCS() {
49 return getStringValueCS(tree, nodeNr);
50 }
51
52 /**
53 * Get the string value of a node. This static method allows the string value of a node
54 * to be obtained without instantiating the node as a Java object. The method also returns
55 * a CharSequence rather than a string, which means it can sometimes avoid copying the
56 * data.
57 *
58 * @param tree The containing document
59 * @param nodeNr identifies the node whose string value is required. This must be a
60 * document or element node. The caller is trusted to ensure this.
61 * @return the string value of the node, as a CharSequence
62 */
63
64 public static CharSequence getStringValueCS(/*@NotNull*/ TinyTree tree, int nodeNr) {
65 int level = tree.depth[nodeNr];
66
67 // note, we can't rely on the value being contiguously stored because of whitespace
68 // nodes: the data for these may still be present.
69
70 int next = nodeNr + 1;
71
72 // we optimize two special cases: firstly, where the node has no children, and secondly,
73 // where it has a single text node as a child.
74
75 if (tree.nodeKind[nodeNr] == Type.TEXTUAL_ELEMENT) {
76 return TinyTextImpl.getStringValue(tree, nodeNr);
77 } else if (tree.nodeKind[nodeNr] == Type.EXTERNAL_NODE_REFERENCE) {
78 GraftedElement graft = tree.externalNodes.get(tree.alpha[nodeNr]);
79 return graft.getStringValueCS();
80 } else if (tree.depth[next] <= level) {
81 return "";
82 } else if (tree.nodeKind[next] == Type.TEXT && (next + 1 >= tree.numberOfNodes || tree.depth[next + 1] <= level)) {
83 return TinyTextImpl.getStringValue(tree, next);
84 }
85
86 // now handle the general case
87
88 FastStringBuffer sb = null;
89 while (next < tree.numberOfNodes && tree.depth[next] > level) {
90 final byte kind = tree.nodeKind[next];
91 if (kind == Type.TEXT || kind == Type.TEXTUAL_ELEMENT) {
92 if (sb == null) {
93 sb = new FastStringBuffer(FastStringBuffer.C256);
94 }
95 sb.append(TinyTextImpl.getStringValue(tree, next));
96 } else if (kind == Type.WHITESPACE_TEXT) {
97 if (sb == null) {
98 sb = new FastStringBuffer(FastStringBuffer.C256);
99 }
100 WhitespaceTextImpl.appendStringValue(tree, next, sb);
101 } else if (kind == Type.EXTERNAL_NODE_REFERENCE) {
102 GraftedElement graft = (GraftedElement) tree.getNode(next);
103 if (sb == null) {
104 sb = new FastStringBuffer(FastStringBuffer.C256);
105 }
106 sb.append(graft.getStringValueCS());
107 }
108 next++;
109 }
110 if (sb == null) {
111 return "";
112 }
113 return sb.condense();
114 }
115
116 }
117