package com.huawei.bss.xml;


import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;


/**
* The Class XmlTree.
*/
public class XmlTree
{

    /** The root. */
    private XmlNode root = null;

    /**
     * Instantiates a new xml tree.
     */
    public XmlTree()
    {
    }

    /**
     * Instantiates a new xml tree.
     * 
     * @param root the root
     */
    public XmlTree(XmlNode root)
    {
        this.root = root;
    }

    /**
     * Gets the root.
     * 
     * @return the root
     */
    public XmlNode getRoot()
    {
        return this.root;
    }

    /**
     * Sets the root.
     * 
     * @param root the new root
     */
    public void setRoot(XmlNode root)
    {
        this.root = root;
    }

    /**
     * Show all.
     */
    public void showAll()
    {
        if (this.root == null)
        {
            System.out.println("null");
        }
        else
        {
            System.out.println(show(this.root, 0));
        }
    }

    /**
     * Show.
     * 
     * @param node the node
     * @param space the space
     * @return the string
     */
    private String show(XmlNode node, int space)
    {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < space; ++i)
        {
            str.append("    ");
        }
        str.append("<").append(node.getTagName());
        if (node.getAttributes().size() > 0)
        {
            str.append(" ").append(getNodeAttribs(node.getAttributes()));
        }
        str.append(">");
        if (node.getChildren() != null)
        {
            int size=node.getChildren().size();
            for (int i = 0; i < size; ++i)
            {
                str.append("\r\n");
                str.append(show((XmlNode) node.getChildren().get(i),
                    space + 1));
                
                //得到子類的Node
                XmlNode childNode=(XmlNode)node.getChildren().get(i);
                if (node.getChildren().get(i).getClass() == XmlNode.class)
                {
                    str.append("</").append(childNode.getTagName()).append(">");
                }
            }
        }
        
        if(node.isRoot())
        {
            str.append("\r\n</").append(node.getTagName()).append(">");
        }

        return str.toString();
    }

    /**
     * Gets the node attribs.
     * 
     * @param attribs the attribs
     * @return the node attribs
     */
    private String getNodeAttribs(Map<String, String> attribs)
    {
        StringBuilder sb = new StringBuilder();
        for (Iterator<Entry<String, String>> iterator =
            attribs.entrySet().iterator(); iterator.hasNext();)
        {
            Entry<String, String> e = iterator.next();
            String key = e.getKey();
            String value = e.getValue();
            sb.append(key).append("=").append("\"").append(value).append("\"")
                .append(" ");

        }

        return sb.toString();
    }
}

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()