| 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.type.Type; |
| 15 | import net.sf.saxon.value.UntypedAtomicValue; |
| 16 | |
| 17 | /** |
| 18 | * A node in the XML parse tree representing character content |
| 19 | * |
| 20 | * @author Michael H. Kay |
| 21 | */ |
| 22 | |
| 23 | public final class TinyTextImpl extends TinyNodeImpl { |
| 24 | |
| 25 | /** |
| 26 | * Create a text node |
| 27 | * |
| 28 | * @param tree the tree to contain the node |
| 29 | * @param nodeNr the internal node number |
| 30 | */ |
| 31 | |
| 32 | public TinyTextImpl(TinyTree tree, int nodeNr) { |
| 33 | this.tree = tree; |
| 34 | this.nodeNr = nodeNr; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Return the character value of the node. |
| 39 | * |
| 40 | * @return the string value of the node |
| 41 | */ |
| 42 | |
| 43 | public String getStringValue() { |
| 44 | return getStringValueCS().toString(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get the value of the item as a CharSequence. This is in some cases more efficient than |
| 49 | * the version of the method that returns a String. |
| 50 | */ |
| 51 | |
| 52 | public CharSequence getStringValueCS() { |
| 53 | int start = tree.alpha[nodeNr]; |
| 54 | int len = tree.beta[nodeNr]; |
| 55 | return tree.charBuffer.subSequence(start, start + len); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Static method to get the string value of a text node without first constructing the node object |
| 60 | * |
| 61 | * @param tree the tree |
| 62 | * @param nodeNr the node number of the text node |
| 63 | * @return the string value of the text node |
| 64 | */ |
| 65 | |
| 66 | public static CharSequence getStringValue(TinyTree tree, int nodeNr) { |
| 67 | int start = tree.alpha[nodeNr]; |
| 68 | int len = tree.beta[nodeNr]; |
| 69 | return tree.charBuffer.subSequence(start, start + len); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Return the type of node. |
| 74 | * |
| 75 | * @return Type.TEXT |
| 76 | */ |
| 77 | |
| 78 | public final int getNodeKind() { |
| 79 | return Type.TEXT; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Copy this node to a given outputter |
| 84 | */ |
| 85 | |
| 86 | public void copy(Receiver out, int copyOptions, Location locationId) throws XPathException { |
| 87 | out.characters(getStringValueCS(), locationId, 0); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the typed value. However, this method is often more convenient and may be |
| 92 | * more efficient, especially in the common case where the value is expected to be a singleton. |
| 93 | * |
| 94 | * @return the typed value. It will be a Value representing a sequence whose items are atomic |
| 95 | * values. |
| 96 | * @since 8.5 |
| 97 | */ |
| 98 | |
| 99 | /*@NotNull*/ |
| 100 | public AtomicSequence atomize() throws XPathException { |
| 101 | return new UntypedAtomicValue(getStringValueCS()); |
| 102 | } |
| 103 | } |
| 104 |