| 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.Receiver; |
| 11 | import net.sf.saxon.expr.parser.Location; |
| 12 | import net.sf.saxon.om.AtomicSequence; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.tree.util.FastStringBuffer; |
| 15 | import net.sf.saxon.type.Type; |
| 16 | import net.sf.saxon.value.UntypedAtomicValue; |
| 17 | |
| 18 | /** |
| 19 | * A node in the XML parse tree representing a text node with compressed whitespace content |
| 20 | * |
| 21 | * @author Michael H. Kay |
| 22 | */ |
| 23 | |
| 24 | public final class WhitespaceTextImpl extends TinyNodeImpl { |
| 25 | |
| 26 | /** |
| 27 | * Create a compressed whitespace text node |
| 28 | * |
| 29 | * @param tree the tree to contain the node |
| 30 | * @param nodeNr the internal node number |
| 31 | */ |
| 32 | |
| 33 | public WhitespaceTextImpl(TinyTree tree, int nodeNr) { |
| 34 | this.tree = tree; |
| 35 | this.nodeNr = nodeNr; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Return the character value of the node. |
| 40 | * |
| 41 | * @return the string value of the node |
| 42 | */ |
| 43 | |
| 44 | public String getStringValue() { |
| 45 | return getStringValueCS().toString(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get the value of the item as a CharSequence. This is in some cases more efficient than |
| 50 | * the version of the method that returns a String. For a WhitespaceTextImpl node, it avoids the |
| 51 | * cost of decompressing the whitespace |
| 52 | */ |
| 53 | |
| 54 | public CharSequence getStringValueCS() { |
| 55 | long value = ((long) tree.alpha[nodeNr] << 32) | ((long) tree.beta[nodeNr] & 0xffffffffL); |
| 56 | return new CompressedWhitespace(value); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Static method to get the string value of a text node without first constructing the node object |
| 61 | * |
| 62 | * @param tree the tree |
| 63 | * @param nodeNr the node number of the text node |
| 64 | * @return the string value of the text node |
| 65 | */ |
| 66 | |
| 67 | public static CharSequence getStringValueCS(TinyTree tree, int nodeNr) { |
| 68 | long value = ((long) tree.alpha[nodeNr] << 32) | ((long) tree.beta[nodeNr] & 0xffffffffL); |
| 69 | return new CompressedWhitespace(value); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Static method to get the string value of a text node and append it to a supplied buffer |
| 74 | * without first constructing the node object |
| 75 | * |
| 76 | * @param tree the tree |
| 77 | * @param nodeNr the node number of the text node |
| 78 | * @param buffer a buffer to which the string value will be appended |
| 79 | */ |
| 80 | |
| 81 | public static void appendStringValue(TinyTree tree, int nodeNr, FastStringBuffer buffer) { |
| 82 | long value = ((long) tree.alpha[nodeNr] << 32) | ((long) tree.beta[nodeNr] & 0xffffffffL); |
| 83 | CompressedWhitespace.uncompress(value, buffer); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get the typed value. |
| 88 | * |
| 89 | * @return the typed value. This will either be a single AtomicValue or a Value whose items are |
| 90 | * atomic values. |
| 91 | * @since 8.5 |
| 92 | */ |
| 93 | |
| 94 | public AtomicSequence atomize() { |
| 95 | return new UntypedAtomicValue(getStringValueCS()); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Static method to get the "long" value representing the content of a whitespace text node |
| 100 | * |
| 101 | * @param tree the TinyTree |
| 102 | * @param nodeNr the internal node number |
| 103 | * @return a value representing the compressed whitespace content |
| 104 | * @see CompressedWhitespace |
| 105 | */ |
| 106 | |
| 107 | public static long getLongValue(TinyTree tree, int nodeNr) { |
| 108 | return ((long) tree.alpha[nodeNr] << 32) | ((long) tree.beta[nodeNr] & 0xffffffffL); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Return the type of node. |
| 113 | * |
| 114 | * @return Type.TEXT |
| 115 | */ |
| 116 | |
| 117 | public final int getNodeKind() { |
| 118 | return Type.TEXT; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Copy this node to a given outputter |
| 123 | */ |
| 124 | |
| 125 | public void copy(/*@NotNull*/ Receiver out, int copyOptions, Location locationId) throws XPathException { |
| 126 | out.characters(getStringValueCS(), locationId, 0); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | } |
| 131 |