001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.jasper.xmlparser;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.HashMap;
022: import java.util.Iterator;
023:
024: /**
025: * Simplified implementation of a Node from a Document Object Model (DOM)
026: * parse of an XML document. This class is used to represent a DOM tree
027: * so that the XML parser's implementation of <code>org.w3c.dom</code> need
028: * not be visible to the remainder of Jasper.
029: * <p>
030: * <strong>WARNING</strong> - Construction of a new tree, or modifications
031: * to an existing one, are not thread-safe and such accesses must be
032: * synchronized.
033: *
034: * @author Craig R. McClanahan
035: * @version $Revision: 1.2 $ $Date: 2004/03/17 19:23:05 $
036: */
037:
038: public class TreeNode {
039:
040: // ----------------------------------------------------------- Constructors
041:
042: /**
043: * Construct a new node with no parent.
044: *
045: * @param name The name of this node
046: */
047: public TreeNode(String name) {
048:
049: this (name, null);
050:
051: }
052:
053: /**
054: * Construct a new node with the specified parent.
055: *
056: * @param name The name of this node
057: * @param parent The node that is the parent of this node
058: */
059: public TreeNode(String name, TreeNode parent) {
060:
061: super ();
062: this .name = name;
063: this .parent = parent;
064: if (this .parent != null)
065: this .parent.addChild(this );
066:
067: }
068:
069: // ----------------------------------------------------- Instance Variables
070:
071: /**
072: * The attributes of this node, keyed by attribute name,
073: * Instantiated only if required.
074: */
075: protected HashMap attributes = null;
076:
077: /**
078: * The body text associated with this node (if any).
079: */
080: protected String body = null;
081:
082: /**
083: * The children of this node, instantiated only if required.
084: */
085: protected ArrayList children = null;
086:
087: /**
088: * The name of this node.
089: */
090: protected String name = null;
091:
092: /**
093: * The parent node of this node.
094: */
095: protected TreeNode parent = null;
096:
097: // --------------------------------------------------------- Public Methods
098:
099: /**
100: * Add an attribute to this node, replacing any existing attribute
101: * with the same name.
102: *
103: * @param name The attribute name to add
104: * @param value The new attribute value
105: */
106: public void addAttribute(String name, String value) {
107:
108: if (attributes == null)
109: attributes = new HashMap();
110: attributes.put(name, value);
111:
112: }
113:
114: /**
115: * Add a new child node to this node.
116: *
117: * @param node The new child node
118: */
119: public void addChild(TreeNode node) {
120:
121: if (children == null)
122: children = new ArrayList();
123: children.add(node);
124:
125: }
126:
127: /**
128: * Return the value of the specified node attribute if it exists, or
129: * <code>null</code> otherwise.
130: *
131: * @param name Name of the requested attribute
132: */
133: public String findAttribute(String name) {
134:
135: if (attributes == null)
136: return (null);
137: else
138: return ((String) attributes.get(name));
139:
140: }
141:
142: /**
143: * Return an Iterator of the attribute names of this node. If there are
144: * no attributes, an empty Iterator is returned.
145: */
146: public Iterator findAttributes() {
147:
148: if (attributes == null)
149: return (Collections.EMPTY_LIST.iterator());
150: else
151: return (attributes.keySet().iterator());
152:
153: }
154:
155: /**
156: * Return the first child node of this node with the specified name,
157: * if there is one; otherwise, return <code>null</code>.
158: *
159: * @param name Name of the desired child element
160: */
161: public TreeNode findChild(String name) {
162:
163: if (children == null)
164: return (null);
165: Iterator items = children.iterator();
166: while (items.hasNext()) {
167: TreeNode item = (TreeNode) items.next();
168: if (name.equals(item.getName()))
169: return (item);
170: }
171: return (null);
172:
173: }
174:
175: /**
176: * Return an Iterator of all children of this node. If there are no
177: * children, an empty Iterator is returned.
178: */
179: public Iterator findChildren() {
180:
181: if (children == null)
182: return (Collections.EMPTY_LIST.iterator());
183: else
184: return (children.iterator());
185:
186: }
187:
188: /**
189: * Return an Iterator over all children of this node that have the
190: * specified name. If there are no such children, an empty Iterator
191: * is returned.
192: *
193: * @param name Name used to select children
194: */
195: public Iterator findChildren(String name) {
196:
197: if (children == null)
198: return (Collections.EMPTY_LIST.iterator());
199:
200: ArrayList results = new ArrayList();
201: Iterator items = children.iterator();
202: while (items.hasNext()) {
203: TreeNode item = (TreeNode) items.next();
204: if (name.equals(item.getName()))
205: results.add(item);
206: }
207: return (results.iterator());
208:
209: }
210:
211: /**
212: * Return the body text associated with this node (if any).
213: */
214: public String getBody() {
215:
216: return (this .body);
217:
218: }
219:
220: /**
221: * Return the name of this node.
222: */
223: public String getName() {
224:
225: return (this .name);
226:
227: }
228:
229: /**
230: * Remove any existing value for the specified attribute name.
231: *
232: * @param name The attribute name to remove
233: */
234: public void removeAttribute(String name) {
235:
236: if (attributes != null)
237: attributes.remove(name);
238:
239: }
240:
241: /**
242: * Remove a child node from this node, if it is one.
243: *
244: * @param node The child node to remove
245: */
246: public void removeNode(TreeNode node) {
247:
248: if (children != null)
249: children.remove(node);
250:
251: }
252:
253: /**
254: * Set the body text associated with this node (if any).
255: *
256: * @param body The body text (if any)
257: */
258: public void setBody(String body) {
259:
260: this .body = body;
261:
262: }
263:
264: /**
265: * Return a String representation of this TreeNode.
266: */
267: public String toString() {
268:
269: StringBuffer sb = new StringBuffer();
270: toString(sb, 0, this );
271: return (sb.toString());
272:
273: }
274:
275: // ------------------------------------------------------ Protected Methods
276:
277: /**
278: * Append to the specified StringBuffer a character representation of
279: * this node, with the specified amount of indentation.
280: *
281: * @param sb The StringBuffer to append to
282: * @param indent Number of characters of indentation
283: * @param node The TreeNode to be printed
284: */
285: protected void toString(StringBuffer sb, int indent, TreeNode node) {
286:
287: int indent2 = indent + 2;
288:
289: // Reconstruct an opening node
290: for (int i = 0; i < indent; i++)
291: sb.append(' ');
292: sb.append('<');
293: sb.append(node.getName());
294: Iterator names = node.findAttributes();
295: while (names.hasNext()) {
296: sb.append(' ');
297: String name = (String) names.next();
298: sb.append(name);
299: sb.append("=\"");
300: String value = node.findAttribute(name);
301: sb.append(value);
302: sb.append("\"");
303: }
304: sb.append(">\n");
305:
306: // Reconstruct the body text of this node (if any)
307: String body = node.getBody();
308: if ((body != null) && (body.length() > 0)) {
309: for (int i = 0; i < indent2; i++)
310: sb.append(' ');
311: sb.append(body);
312: sb.append("\n");
313: }
314:
315: // Reconstruct child nodes with extra indentation
316: Iterator children = node.findChildren();
317: while (children.hasNext()) {
318: TreeNode child = (TreeNode) children.next();
319: toString(sb, indent2, child);
320: }
321:
322: // Reconstruct a closing node marker
323: for (int i = 0; i < indent; i++)
324: sb.append(' ');
325: sb.append("</");
326: sb.append(node.getName());
327: sb.append(">\n");
328:
329: }
330:
331: }
|