KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > tinytree > TinyParentNodeImpl


1 package net.sf.saxon.tinytree;
2 import net.sf.saxon.type.Type;
3 import net.sf.saxon.om.FastStringBuffer;
4
5 /**
6   * TinyParentNodeImpl is an implementation of a non-leaf node (specifically, an Element node
7   * or a Document node)
8   * @author Michael H. Kay
9   */

10
11
12 abstract class TinyParentNodeImpl extends TinyNodeImpl {
13
14     /**
15     * Determine if the node has children.
16     */

17
18     public final boolean hasChildNodes() {
19         return (nodeNr+1 < tree.numberOfNodes &&
20                 tree.depth[nodeNr+1] > tree.depth[nodeNr]);
21     }
22
23     /**
24     * Return the string-value of the node, that is, the concatenation
25     * of the character content of all descendent elements and text nodes.
26     * @return the accumulated character content of the element, including descendant elements.
27     */

28
29     public final String JavaDoc getStringValue() {
30         return getStringValue(tree, nodeNr).toString();
31     }
32
33     /**
34      * Get the value of the item as a CharSequence. This is in some cases more efficient than
35      * the version of the method that returns a String.
36      */

37
38     public CharSequence JavaDoc getStringValueCS() {
39         return getStringValue(tree, nodeNr);
40     }
41
42     /**
43      * Get the string value of a node. This static method allows the string value of a node
44      * to be obtained without instantiating the node as a Java object. The method also returns
45      * a CharSequence rather than a string, which means it can sometimes avoid copying the
46      * data.
47      * @param tree The containing document
48      * @param nodeNr identifies the node whose string value is required. This must be a
49      * document or element node. The caller is trusted to ensure this.
50      * @return the string value of the node, as a CharSequence
51      */

52
53     public static final CharSequence JavaDoc getStringValue(TinyTree tree, int nodeNr) {
54         int level = tree.depth[nodeNr];
55
56         // note, we can't rely on the value being contiguously stored because of whitespace
57
// nodes: the data for these may still be present.
58

59         int next = nodeNr+1;
60
61         // we optimize two special cases: firstly, where the node has no children, and secondly,
62
// where it has a single text node as a child.
63

64         if (tree.depth[next] <= level) {
65             return "";
66         } else if (tree.nodeKind[next] == Type.TEXT && tree.depth[next+1] <= level) {
67             int length = tree.beta[next];
68             int start = tree.alpha[next];
69             return new CharSlice(tree.charBuffer, start, length);
70         }
71
72         // now handle the general case
73

74         FastStringBuffer sb = null;
75         while (next < tree.numberOfNodes && tree.depth[next] > level) {
76             if (tree.nodeKind[next]==Type.TEXT) {
77                 int length = tree.beta[next];
78                 int start = tree.alpha[next];
79                 if (sb==null) {
80                     sb = new FastStringBuffer(1024);
81                 }
82                 sb.append(tree.charBuffer, start, length);
83             }
84             next++;
85         }
86         if (sb==null) return "";
87         return sb.condense();
88     }
89
90 }
91
92
93 //
94
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
95
// you may not use this file except in compliance with the License. You may obtain a copy of the
96
// License at http://www.mozilla.org/MPL/
97
//
98
// Software distributed under the License is distributed on an "AS IS" basis,
99
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
100
// See the License for the specific language governing rights and limitations under the License.
101
//
102
// The Original Code is: all this file.
103
//
104
// The Initial Developer of the Original Code is Michael H. Kay.
105
//
106
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
107
//
108
// Contributor(s): none.
109
//
110
Popular Tags