KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > jasper > xmlparser > TreeNode


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.icesoft.jasper.xmlparser;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24
25 /**
26  * Simplified implementation of a Node from a Document Object Model (DOM) parse
27  * of an XML document. This class is used to represent a DOM tree so that the
28  * XML parser's implementation of <code>org.w3c.dom</code> need not be visible
29  * to the remainder of Jasper.
30  * <p/>
31  * <strong>WARNING</strong> - Construction of a new tree, or modifications to an
32  * existing one, are not thread-safe and such accesses must be synchronized.
33  *
34  * @author Craig R. McClanahan
35  * @version $Revision: 1.2 $ $Date: 2004/03/17 19:23:05 $
36  */

37
38 public class TreeNode {
39
40     // ----------------------------------------------------------- Constructors
41

42
43     /**
44      * Construct a new node with no parent.
45      *
46      * @param name The name of this node
47      */

48     public TreeNode(String JavaDoc name) {
49
50         this(name, null);
51
52     }
53
54
55     /**
56      * Construct a new node with the specified parent.
57      *
58      * @param name The name of this node
59      * @param parent The node that is the parent of this node
60      */

61     public TreeNode(String JavaDoc name, TreeNode parent) {
62
63         super();
64         this.name = name;
65         this.parent = parent;
66         if (this.parent != null)
67             this.parent.addChild(this);
68
69     }
70
71     // ----------------------------------------------------- Instance Variables
72

73
74     /**
75      * The attributes of this node, keyed by attribute name, Instantiated only
76      * if required.
77      */

78     protected HashMap JavaDoc attributes = null;
79
80
81     /**
82      * The body text associated with this node (if any).
83      */

84     protected String JavaDoc body = null;
85
86
87     /**
88      * The children of this node, instantiated only if required.
89      */

90     protected ArrayList JavaDoc children = null;
91
92
93     /**
94      * The name of this node.
95      */

96     protected String JavaDoc name = null;
97
98
99     /**
100      * The parent node of this node.
101      */

102     protected TreeNode parent = null;
103
104     // --------------------------------------------------------- Public Methods
105

106
107     /**
108      * Add an attribute to this node, replacing any existing attribute with the
109      * same name.
110      *
111      * @param name The attribute name to add
112      * @param value The new attribute value
113      */

114     public void addAttribute(String JavaDoc name, String JavaDoc value) {
115
116         if (attributes == null)
117             attributes = new HashMap JavaDoc();
118         attributes.put(name, value);
119
120     }
121
122
123     /**
124      * Add a new child node to this node.
125      *
126      * @param node The new child node
127      */

128     public void addChild(TreeNode node) {
129
130         if (children == null)
131             children = new ArrayList JavaDoc();
132         children.add(node);
133
134     }
135
136
137     /**
138      * Return the value of the specified node attribute if it exists, or
139      * <code>null</code> otherwise.
140      *
141      * @param name Name of the requested attribute
142      */

143     public String JavaDoc findAttribute(String JavaDoc name) {
144
145         if (attributes == null)
146             return (null);
147         else
148             return ((String JavaDoc) attributes.get(name));
149
150     }
151
152
153     /**
154      * Return an Iterator of the attribute names of this node. If there are no
155      * attributes, an empty Iterator is returned.
156      */

157     public Iterator JavaDoc findAttributes() {
158
159         if (attributes == null)
160             return (Collections.EMPTY_LIST.iterator());
161         else
162             return (attributes.keySet().iterator());
163
164     }
165
166
167     /**
168      * Return the first child node of this node with the specified name, if
169      * there is one; otherwise, return <code>null</code>.
170      *
171      * @param name Name of the desired child element
172      */

173     public TreeNode findChild(String JavaDoc name) {
174
175         if (children == null)
176             return (null);
177         Iterator JavaDoc items = children.iterator();
178         while (items.hasNext()) {
179             TreeNode item = (TreeNode) items.next();
180             if (name.equals(item.getName()))
181                 return (item);
182         }
183         return (null);
184
185     }
186
187
188     /**
189      * Return an Iterator of all children of this node. If there are no
190      * children, an empty Iterator is returned.
191      */

192     public Iterator JavaDoc findChildren() {
193
194         if (children == null)
195             return (Collections.EMPTY_LIST.iterator());
196         else
197             return (children.iterator());
198
199     }
200
201
202     /**
203      * Return an Iterator over all children of this node that have the specified
204      * name. If there are no such children, an empty Iterator is returned.
205      *
206      * @param name Name used to select children
207      */

208     public Iterator JavaDoc findChildren(String JavaDoc name) {
209
210         if (children == null)
211             return (Collections.EMPTY_LIST.iterator());
212
213         ArrayList JavaDoc results = new ArrayList JavaDoc();
214         Iterator JavaDoc items = children.iterator();
215         while (items.hasNext()) {
216             TreeNode item = (TreeNode) items.next();
217             if (name.equals(item.getName()))
218                 results.add(item);
219         }
220         return (results.iterator());
221
222     }
223
224
225     /**
226      * Return the body text associated with this node (if any).
227      */

228     public String JavaDoc getBody() {
229
230         return (this.body);
231
232     }
233
234
235     /**
236      * Return the name of this node.
237      */

238     public String JavaDoc getName() {
239
240         return (this.name);
241
242     }
243
244
245     /**
246      * Remove any existing value for the specified attribute name.
247      *
248      * @param name The attribute name to remove
249      */

250     public void removeAttribute(String JavaDoc name) {
251
252         if (attributes != null)
253             attributes.remove(name);
254
255     }
256
257
258     /**
259      * Remove a child node from this node, if it is one.
260      *
261      * @param node The child node to remove
262      */

263     public void removeNode(TreeNode node) {
264
265         if (children != null)
266             children.remove(node);
267
268     }
269
270
271     /**
272      * Set the body text associated with this node (if any).
273      *
274      * @param body The body text (if any)
275      */

276     public void setBody(String JavaDoc body) {
277
278         this.body = body;
279
280     }
281
282
283     /**
284      * Return a String representation of this TreeNode.
285      */

286     public String JavaDoc toString() {
287
288         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
289         toString(sb, 0, this);
290         return (sb.toString());
291
292     }
293
294     // ------------------------------------------------------ Protected Methods
295

296
297     /**
298      * Append to the specified StringBuffer a character representation of this
299      * node, with the specified amount of indentation.
300      *
301      * @param sb The StringBuffer to append to
302      * @param indent Number of characters of indentation
303      * @param node The TreeNode to be printed
304      */

305     protected void toString(StringBuffer JavaDoc sb, int indent,
306                             TreeNode node) {
307
308         int indent2 = indent + 2;
309
310         // Reconstruct an opening node
311
for (int i = 0; i < indent; i++)
312             sb.append(' ');
313         sb.append('<');
314         sb.append(node.getName());
315         Iterator JavaDoc names = node.findAttributes();
316         while (names.hasNext()) {
317             sb.append(' ');
318             String JavaDoc name = (String JavaDoc) names.next();
319             sb.append(name);
320             sb.append("=\"");
321             String JavaDoc value = node.findAttribute(name);
322             sb.append(value);
323             sb.append("\"");
324         }
325         sb.append(">\n");
326
327         // Reconstruct the body text of this node (if any)
328
String JavaDoc body = node.getBody();
329         if ((body != null) && (body.length() > 0)) {
330             for (int i = 0; i < indent2; i++)
331                 sb.append(' ');
332             sb.append(body);
333             sb.append("\n");
334         }
335
336         // Reconstruct child nodes with extra indentation
337
Iterator JavaDoc children = node.findChildren();
338         while (children.hasNext()) {
339             TreeNode child = (TreeNode) children.next();
340             toString(sb, indent2, child);
341         }
342
343         // Reconstruct a closing node marker
344
for (int i = 0; i < indent; i++)
345             sb.append(' ');
346         sb.append("</");
347         sb.append(node.getName());
348         sb.append(">\n");
349
350     }
351
352
353 }
354
Popular Tags