FileDocCategorySizeDatePackage
TreeNode.javaAPI DocGlassfish v2 API10042Fri May 04 22:33:00 BST 2007org.apache.jasper.xmlparser

TreeNode

public class TreeNode extends Object
Simplified implementation of a Node from a Document Object Model (DOM) parse of an XML document. This class is used to represent a DOM tree so that the XML parser's implementation of org.w3c.dom need not be visible to the remainder of Jasper.

WARNING - Construction of a new tree, or modifications to an existing one, are not thread-safe and such accesses must be synchronized.

author
Craig R. McClanahan
version
$Revision: 1.3 $ $Date: 2007/05/05 05:32:59 $

Fields Summary
protected HashMap
attributes
The attributes of this node, keyed by attribute name, Instantiated only if required.
protected String
body
The body text associated with this node (if any).
protected ArrayList
children
The children of this node, instantiated only if required.
protected String
name
The name of this node.
protected TreeNode
parent
The parent node of this node.
Constructors Summary
public TreeNode(String name)
Construct a new node with no parent.

param
name The name of this node


        this(name, null);

    
public TreeNode(String name, TreeNode parent)
Construct a new node with the specified parent.

param
name The name of this node
param
parent The node that is the parent of this node


        super();
        this.name = name;
        this.parent = parent;
        if (this.parent != null)
            this.parent.addChild(this);

    
Methods Summary
public voidaddAttribute(java.lang.String name, java.lang.String value)
Add an attribute to this node, replacing any existing attribute with the same name.

param
name The attribute name to add
param
value The new attribute value



    // --------------------------------------------------------- Public Methods


                                    
          

        if (attributes == null)
            attributes = new HashMap();
        attributes.put(name, value);

    
public voidaddChild(org.apache.jasper.xmlparser.TreeNode node)
Add a new child node to this node.

param
node The new child node


        if (children == null)
            children = new ArrayList();
        children.add(node);

    
public java.lang.StringfindAttribute(java.lang.String name)
Return the value of the specified node attribute if it exists, or null otherwise.

param
name Name of the requested attribute


        if (attributes == null)
            return (null);
        else
            return ((String) attributes.get(name));

    
public java.util.IteratorfindAttributes()
Return an Iterator of the attribute names of this node. If there are no attributes, an empty Iterator is returned.


        if (attributes == null)
            return (Collections.EMPTY_LIST.iterator());
        else
            return (attributes.keySet().iterator());

    
public org.apache.jasper.xmlparser.TreeNodefindChild(java.lang.String name)
Return the first child node of this node with the specified name, if there is one; otherwise, return null.

param
name Name of the desired child element


        if (children == null)
            return (null);
        Iterator items = children.iterator();
        while (items.hasNext()) {
            TreeNode item = (TreeNode) items.next();
            if (name.equals(item.getName()))
                return (item);
        }
        return (null);

    
public java.util.IteratorfindChildren()
Return an Iterator of all children of this node. If there are no children, an empty Iterator is returned.


        if (children == null)
            return (Collections.EMPTY_LIST.iterator());
        else
            return (children.iterator());

    
public java.util.IteratorfindChildren(java.lang.String name)
Return an Iterator over all children of this node that have the specified name. If there are no such children, an empty Iterator is returned.

param
name Name used to select children


        if (children == null)
            return (Collections.EMPTY_LIST.iterator());

        ArrayList results = new ArrayList();
        Iterator items = children.iterator();
        while (items.hasNext()) {
            TreeNode item = (TreeNode) items.next();
            if (name.equals(item.getName()))
                results.add(item);
        }
        return (results.iterator());

    
public java.lang.StringgetBody()
Return the body text associated with this node (if any).


        return (this.body);

    
public java.lang.StringgetName()
Return the name of this node.


        return (this.name);

    
public voidremoveAttribute(java.lang.String name)
Remove any existing value for the specified attribute name.

param
name The attribute name to remove


        if (attributes != null)
            attributes.remove(name);

    
public voidremoveNode(org.apache.jasper.xmlparser.TreeNode node)
Remove a child node from this node, if it is one.

param
node The child node to remove


        if (children != null)
            children.remove(node);

    
public voidsetBody(java.lang.String body)
Set the body text associated with this node (if any).

param
body The body text (if any)


        this.body = body;

    
public java.lang.StringtoString()
Return a String representation of this TreeNode.


        StringBuffer sb = new StringBuffer();
        toString(sb, 0, this);
        return (sb.toString());

    
protected voidtoString(java.lang.StringBuffer sb, int indent, org.apache.jasper.xmlparser.TreeNode node)
Append to the specified StringBuffer a character representation of this node, with the specified amount of indentation.

param
sb The StringBuffer to append to
param
indent Number of characters of indentation
param
node The TreeNode to be printed


        int indent2 = indent + 2;

        // Reconstruct an opening node
        for (int i = 0; i < indent; i++)
            sb.append(' ");
        sb.append('<");
        sb.append(node.getName());
        Iterator names = node.findAttributes();
        while (names.hasNext()) {
            sb.append(' ");
            String name = (String) names.next();
            sb.append(name);
            sb.append("=\"");
            String value = node.findAttribute(name);
            sb.append(value);
            sb.append("\"");
        }
        sb.append(">\n");

        // Reconstruct the body text of this node (if any)
        String body = node.getBody();
        if ((body != null) && (body.length() > 0)) {
            for (int i = 0; i < indent2; i++)
                sb.append(' ");
            sb.append(body);
            sb.append("\n");
        }

        // Reconstruct child nodes with extra indentation
        Iterator children = node.findChildren();
        while (children.hasNext()) {
            TreeNode child = (TreeNode) children.next();
            toString(sb, indent2, child);
        }

        // Reconstruct a closing node marker
        for (int i = 0; i < indent; i++)
            sb.append(' ");
        sb.append("</");
        sb.append(node.getName());
        sb.append(">\n");