KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > tree > ParentNodeImpl


1 package net.sf.saxon.tree;
2 import net.sf.saxon.om.*;
3 import net.sf.saxon.pattern.AnyNodeTest;
4 import net.sf.saxon.pattern.NodeTest;
5
6 /**
7   * ParentNodeImpl is an implementation of a non-leaf node (specifically, an Element node
8   * or a Document node)
9   * @author Michael H. Kay
10   */

11
12
13 abstract class ParentNodeImpl extends NodeImpl {
14
15     private Object JavaDoc children = null; // null for no children
16
// a NodeInfo for a single child
17
// a NodeInfo[] for >1 child
18

19     protected int sequence;
20
21     /**
22     * Get the node sequence number (in document order). Sequence numbers are monotonic but not
23     * consecutive. In the current implementation, parent nodes (elements and roots) have a zero
24     * least-significant word, while namespaces, attributes, text nodes, comments, and PIs have
25     * the top word the same as their owner and the bottom half reflecting their relative position.
26     */

27
28     protected final long getSequenceNumber() {
29         return ((long)sequence)<<32;
30     }
31
32     /**
33     * Determine if the node has any children.
34     */

35
36     public final boolean hasChildNodes() {
37         return (children!=null);
38     }
39
40     /**
41     * Get an enumeration of the children of this node
42      * @param test A NodeTest to be satisfied by the child nodes, or null
43      * if all child node are to be returned
44     */

45
46     public final AxisIterator enumerateChildren(NodeTest test) {
47         if (children==null) {
48             return EmptyIterator.getInstance();
49         } else if (children instanceof NodeImpl) {
50             NodeImpl child = (NodeImpl)children;
51             if (test==null || test.matches(child)) {
52                 return SingletonIterator.makeIterator(child);
53             } else {
54                 return EmptyIterator.getInstance();
55             }
56         } else {
57             if (test==null || test instanceof AnyNodeTest) {
58                 return new ArrayIterator((NodeImpl[])children);
59             } else {
60                 return new ChildEnumeration(this, test);
61             }
62         }
63     }
64
65
66     /**
67     * Get the first child node of the element
68     * @return the first child node of the required type, or null if there are no children
69     */

70
71     public final NodeInfo getFirstChild() {
72         if (children==null) return null;
73         if (children instanceof NodeImpl) return (NodeImpl)children;
74         return ((NodeImpl[])children)[0];
75     }
76
77     /**
78     * Get the last child node of the element
79     * @return the last child of the element, or null if there are no children
80     */

81
82     public final NodeInfo getLastChild() {
83         if (children==null) return null;
84         if (children instanceof NodeImpl) return (NodeImpl)children;
85         NodeImpl[] n = (NodeImpl[])children;
86         return n[n.length-1];
87     }
88
89     /**
90     * Get the nth child node of the element (numbering from 0)
91     * @return the last child of the element, or null if there is no n'th child
92     */

93
94     protected final NodeImpl getNthChild(int n) {
95         if (children==null) return null;
96         if (children instanceof NodeImpl) {
97             return (n==0 ? (NodeImpl)children : null);
98         }
99         NodeImpl[] nodes = (NodeImpl[])children;
100         if (n<0 || n>=nodes.length) return null;
101         return nodes[n];
102     }
103
104
105     /**
106     * Return the string-value of the node, that is, the concatenation
107     * of the character content of all descendent elements and text nodes.
108     * @return the accumulated character content of the element, including descendant elements.
109     */

110
111     public String JavaDoc getStringValue() {
112         return getStringValueCS().toString();
113     }
114
115
116     public CharSequence JavaDoc getStringValueCS() {
117         FastStringBuffer sb = null;
118
119         NodeImpl next = (NodeImpl)getFirstChild();
120         while (next!=null) {
121             if (next instanceof TextImpl) {
122                 if (sb==null) {
123                     sb = new FastStringBuffer(1024);
124                 }
125                 sb.append(next.getStringValueCS());
126             }
127             next = next.getNextInDocument(this);
128         }
129         if (sb==null) return "";
130         return sb.condense();
131     }
132
133     /**
134     * Copy the string-value of this node to a given outputter
135     */

136 /*
137     public void copyStringValue(Receiver out) throws XPathException {
138         NodeImpl next = (NodeImpl)getFirstChild();
139         while (next!=null) {
140             if (next.getItemType()==Type.TEXT) {
141                 next.copyStringValue(out);
142             }
143             next = next.getNextInDocument(this);
144         }
145     }
146 */

147     /**
148     * Supply an array to be used for the array of children. For system use only.
149     */

150
151     public void useChildrenArray(NodeImpl[] array) {
152         children = array;
153     }
154
155     /**
156     * Add a child node to this node. For system use only. Note: normalizing adjacent text nodes
157     * is the responsibility of the caller.
158     */

159
160     public void addChild(NodeImpl node, int index) {
161         NodeImpl[] c;
162         if (children == null) {
163             c = new NodeImpl[10];
164         } else if (children instanceof NodeImpl) {
165             c = new NodeImpl[10];
166             c[0] = (NodeImpl)children;
167         } else {
168             c = (NodeImpl[])children;
169         }
170         if (index >= c.length) {
171             NodeImpl[] kids = new NodeImpl[c.length * 2];
172             System.arraycopy(c, 0, kids, 0, c.length);
173             c = kids;
174         }
175         c[index] = node;
176         node.parent = this;
177         node.index = index;
178         children = c;
179     }
180
181
182     /**
183     * Compact the space used by this node
184     */

185
186     public void compact(int size) {
187         if (size==0) {
188             children = null;
189         } else if (size==1) {
190             if (children instanceof NodeImpl[]) {
191                 children = ((NodeImpl[])children)[0];
192             }
193         } else {
194             NodeImpl[] kids = new NodeImpl[size];
195             System.arraycopy(children, 0, kids, 0, size);
196             children = kids;
197         }
198     }
199
200 }
201
202
203 //
204
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
205
// you may not use this file except in compliance with the License. You may obtain a copy of the
206
// License at http://www.mozilla.org/MPL/
207
//
208
// Software distributed under the License is distributed on an "AS IS" basis,
209
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
210
// See the License for the specific language governing rights and limitations under the License.
211
//
212
// The Original Code is: all this file.
213
//
214
// The Initial Developer of the Original Code is Michael H. Kay.
215
//
216
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
217
//
218
// Contributor(s): none.
219
//
220
Popular Tags