FileDocCategorySizeDatePackage
DOMTreeFull.javaAPI DocApache Xerces 3.0.112244Fri Sep 14 20:33:58 BST 2007ui

DOMTreeFull

public class DOMTreeFull extends JTree
DOMTree class to enter every DOM node into a Swing JTree tree. The forward and backward mappings between Nodes to TreeNodes are kept.
version
$Id: DOMTreeFull.java 447689 2006-09-19 02:40:36Z mrglavas $

Fields Summary
private static final long
serialVersionUID
public static String[]
whatArray
Constructors Summary
public DOMTreeFull()
Default constructor.


    //
    // Constructors
    //

       
      
        this(null);
        
public DOMTreeFull(Node root)
Constructs a tree with the specified document.

        super(new Model());

        // set tree properties
        setRootVisible(false);

        // set properties
        setRootNode(root);

        
Methods Summary
public org.w3c.dom.NodegetNode(java.lang.Object treeNode)
get the org.w3c.Node for a MutableTreeNode.

        return ((Model)getModel()).getNode(treeNode);
    
public org.w3c.dom.NodegetRootNode()
Returns the root.

        return ((Model)getModel()).getRootNode();
        
public javax.swing.tree.TreeNodegetTreeNode(java.lang.Object node)
get the TreeNode for the org.w3c.Node

        return ((Model)getModel()).getTreeNode(node);
    
protected static java.lang.Stringnormalize(java.lang.String s)
Normalizes the given string.

            StringBuffer str = new StringBuffer();

            int len = (s != null) ? s.length() : 0;
            for (int i = 0; i < len; i++) {
                char ch = s.charAt(i);
                switch (ch) {
                    case '<": {
                        str.append("<");
                        break;
                    }
                    case '>": {
                        str.append(">");
                        break;
                    }
                    case '&": {
                        str.append("&");
                        break;
                    }
                    case '"": {
                        str.append(""");
                        break;
                    }
                    case '\r":
                    case '\n": 
                    default: {
                        str.append(ch);
                    }
                }
            }

            return str.toString();

        
public voidsetRootNode(org.w3c.dom.Node root)
Sets the root.

        ((Model)getModel()).setRootNode(root);
        expandRow(0);
        
protected static org.w3c.dom.Attr[]sortAttributes(org.w3c.dom.NamedNodeMap attrs)
Returns a sorted list of attributes.


            int len = (attrs != null) ? attrs.getLength() : 0;
            Attr array[] = new Attr[len];
            for (int i = 0; i < len; i++) {
                array[i] = (Attr)attrs.item(i);
            }
            for (int i = 0; i < len - 1; i++) {
                String name  = array[i].getNodeName();
                int    index = i;
                for (int j = i + 1; j < len; j++) {
                    String curName = array[j].getNodeName();
                    if (curName.compareTo(name) < 0) {
                        name  = curName;
                        index = j;
                    }
                }
                if (index != i) {
                    Attr temp    = array[i];
                    array[i]     = array[index];
                    array[index] = temp;
                }
            }

            return array;

        
public static java.lang.StringtoString(org.w3c.dom.Node node)

        //
        // Public methods
        //
                
             
            StringBuffer sb = new StringBuffer();
                
            // is there anything to do?
            if (node == null) {
                return "";
            }

            int type = node.getNodeType();
            sb.append(whatArray[type]);
            sb.append(" : ");
            sb.append(node.getNodeName());
            String value = node.getNodeValue();
            if (value != null) {
                sb.append(" Value: \"");
                sb.append(value);
                sb.append("\"");
            }
                    
            switch (type) {
                    
                // document
                case Node.DOCUMENT_NODE: {
                    break;
                }

                // element with attributes
                case Node.ELEMENT_NODE: {
                    Attr attrs[] = sortAttributes(node.getAttributes());
                    if (attrs.length > 0)
                        sb.append(" ATTRS:");
                    for (int i = 0; i < attrs.length; i++) {
                        Attr attr = attrs[i];
                            
                        sb.append(' ");
                        sb.append(attr.getNodeName());
                        sb.append("=\"");
                        sb.append(normalize(attr.getNodeValue()));
                        sb.append('"");
                    }
                    sb.append('>");
                    break;
                }

                // handle entity reference nodes
                case Node.ENTITY_REFERENCE_NODE: {
                    break;
                }

                // cdata sections
                case Node.CDATA_SECTION_NODE: {
                    break;
                }

                // text
                case Node.TEXT_NODE: {
                    break;
                }

                // processing instruction
                case Node.PROCESSING_INSTRUCTION_NODE: {
                    break;
                }
                    
                // comment node 
                case Node.COMMENT_NODE: {
                    break;
                }
                // DOCTYPE node
                case Node.DOCUMENT_TYPE_NODE: {
                    break;
                }
                // Notation node
                case Node.NOTATION_NODE: {
                        sb.append("public:");
                        String id = ((Notation)node).getPublicId();
                        if (id == null) {
                            sb.append("PUBLIC ");
                            sb.append(id);
                            sb.append(" ");
                        }
                        id = ((Notation)node).getSystemId();
                        if (id == null) {
                            sb.append("system: ");
                            sb.append(id);
                            sb.append(" ");
                        }
                    break;
                }
            }
            return sb.toString();