Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JSiteMap - Java Site Map Generator - TeutoTreeNode.java

TeutoTreeNode.java

Caricato da: Teutoburgo
Scarica il programma completo

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 1999 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution, if
  20.  *    any, must include the following acknowlegement:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowlegement may appear in the software itself,
  24.  *    if and wherever such third-party acknowlegements normally appear.
  25.  *
  26.  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
  27.  *    Foundation" must not be used to endorse or promote products derived
  28.  *    from this software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache"
  32.  *    nor may "Apache" appear in their names without prior written
  33.  *    permission of the Apache Group.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation.  For more
  51.  * information on the Apache Software Foundation, please see
  52.  * <http://www.apache.org/>.
  53.  *
  54.  
  55. This class was derived from org.apache.jasper.xmlparser.TreeNode;
  56. */
  57. package tk.teutoburgo.jsitemap;
  58.  
  59. import java.util.Vector;
  60. import java.util.Collections;
  61. import java.util.Hashtable;
  62. import java.util.Enumeration;
  63.  
  64.  
  65. /**
  66.  * Simplified implementation of a Node from a Document Object Model (DOM)
  67.  * parse of an XML document.  This class is used to represent a DOM tree
  68.  * so that the XML parser's implementation of <code>org.w3c.dom</code> need
  69.  * not be visible to the remainder of Jasper.
  70.  * <p>
  71.  * <strong>WARNING</strong> - Construction of a new tree, or modifications
  72.  * to an existing one, are not thread-safe and such accesses must be
  73.  * synchronized.
  74.  *
  75.  * @author Craig R. McClanahan, Pierre Blanc
  76.  * @version $Revision: 1.1.1.1 $ $Date: 2003/03/03 18:46:20 $
  77.  */
  78.  
  79. public class TeutoTreeNode {
  80.  
  81.  
  82.     // ----------------------------------------------------------- Constructors
  83.  
  84.  
  85.     /**
  86.      * Construct a new node with no parent.
  87.      *
  88.      * @param name The name of this node
  89.      */
  90.     public TeutoTreeNode(String name) {
  91.  
  92.         this(name, null);
  93.  
  94.     }
  95.  
  96.  
  97.     /**
  98.      * Construct a new node with the specified parent.
  99.      *
  100.      * @param name The name of this node
  101.      * @param parent The node that is the parent of this node
  102.      */
  103.     public TeutoTreeNode(String name, TeutoTreeNode parent) {
  104.  
  105.         super();
  106.         this.name = name;
  107.         this.parent = parent;
  108.         if (this.parent != null)
  109.             this.parent.addChild(this);
  110.  
  111.     }
  112.  
  113.  
  114.     // ----------------------------------------------------- Instance Variables
  115.  
  116.  
  117.     /**
  118.      * The attributes of this node, keyed by attribute name,
  119.      * Instantiated only if required.
  120.      */
  121.     protected Hashtable attributes = null;
  122.  
  123.  
  124.     /**
  125.      * The body text associated with this node (if any).
  126.      */
  127.     protected String body = null;
  128.  
  129.  
  130.     /**
  131.      * The children of this node, instantiated only if required.
  132.      */
  133.     protected Vector children = null;
  134.  
  135.  
  136.     /**
  137.      * The name of this node.
  138.      */
  139.     protected String name = null;
  140.  
  141.  
  142.     /**
  143.      * The parent node of this node.
  144.      */
  145.     protected TeutoTreeNode parent = null;
  146.  
  147.  
  148.     // --------------------------------------------------------- Public Methods
  149.  
  150.  
  151.     /**
  152.      * Add an attribute to this node, replacing any existing attribute
  153.      * with the same name.
  154.      *
  155.      * @param name The attribute name to add
  156.      * @param value The new attribute value
  157.      */
  158.     public void addAttribute(String name, String value) {
  159.  
  160.         if (attributes == null)
  161.             attributes = new Hashtable();
  162.         attributes.put(name, value);
  163.  
  164.     }
  165.  
  166.  
  167.     /**
  168.      * Add a new child node to this node.
  169.      *
  170.      * @param node The new child node
  171.      */
  172.     public void addChild(TeutoTreeNode node) {
  173.  
  174.         if (children == null)
  175.             children = new Vector();
  176.         children.addElement(node);
  177.  
  178.     }
  179.  
  180.  
  181.     /**
  182.      * Return the value of the specified node attribute if it exists, or
  183.      * <code>null</code> otherwise.
  184.      *
  185.      * @param name Name of the requested attribute
  186.      */
  187.     public String findAttribute(String name) {
  188.  
  189.         if (attributes == null)
  190.             return (null);
  191.         else
  192.             return ((String) attributes.get(name));
  193.  
  194.     }
  195.  
  196.  
  197.     /**
  198.      * Return an Enumeration of the attribute names of this node.  If there are
  199.      * no attributes, an empty Enumeration is returned.
  200.      */
  201.     public Enumeration findAttributes() {
  202.  
  203.         if (attributes == null)
  204.             return (null);
  205.         else
  206.             return (attributes.elements());
  207.  
  208.     }
  209.  
  210.  
  211.     /**
  212.      * Return the first child node of this node with the specified name,
  213.      * if there is one; otherwise, return <code>null</code>.
  214.      *
  215.      * @param name Name of the desired child element
  216.      */
  217.     public TeutoTreeNode findChild(String name) {
  218.  
  219.         if (children == null)
  220.             return (null);
  221.         Enumeration items = children.elements();
  222.         while (items.hasMoreElements()) {
  223.             TeutoTreeNode item = (TeutoTreeNode) items.nextElement();
  224.             if (name.equals(item.getName()))
  225.                 return (item);
  226.         }
  227.         return (null);
  228.  
  229.     }
  230.  
  231.  
  232.     /**
  233.      * Return an Enumeration of all children of this node.  If there are no
  234.      * children, an empty Enumeration is returned.
  235.      */
  236.     public Enumeration findChildren() {
  237.  
  238.         if (children == null)
  239.             return (null);
  240.         else
  241.             return (children.elements());
  242.  
  243.     }
  244.  
  245.  
  246.     /**
  247.      * Return an Enumeration over all children of this node that have the
  248.      * specified name.  If there are no such children, an empty Enumeration
  249.      * is returned.
  250.      *
  251.      * @param name Name used to select children
  252.      */
  253.   /*  public Enumeration findChildren(String name) {
  254.  
  255.         if (children == null)
  256.             return (null);
  257.  
  258.         Vector results = new Vector();
  259.         Enumeration items = children.iterator();
  260.         while (items.hasMoreElements()) {
  261.             TeutoTreeNode item = (TeutoTreeNode) items.nextElement();
  262.             if (name.equals(item.getName()))
  263.                 results.addElement(item);
  264.         }
  265.         return (results.iterator());
  266.  
  267.     }*/
  268.  
  269.  
  270.     /**
  271.      * Return the body text associated with this node (if any).
  272.      */
  273.     public String getBody() {
  274.  
  275.         return (this.body);
  276.  
  277.     }
  278.  
  279.  
  280.     /**
  281.      * Return the name of this node.
  282.      */
  283.     public String getName() {
  284.  
  285.         return (this.name);
  286.  
  287.     }
  288.  
  289.  
  290.     /**
  291.      * Remove any existing value for the specified attribute name.
  292.      *
  293.      * @param name The attribute name to remove
  294.      */
  295.     public void removeAttribute(String name) {
  296.  
  297.         if (attributes != null)
  298.             attributes.remove(name);
  299.  
  300.     }
  301.  
  302.  
  303.     /**
  304.      * Remove a child node from this node, if it is one.
  305.      *
  306.      * @param node The child node to remove
  307.      */
  308.     public void removeNode(TeutoTreeNode node) {
  309.  
  310.         if (children != null)
  311.             children.remove(node);
  312.  
  313.     }
  314.  
  315.  
  316.     /**
  317.      * Set the body text associated with this node (if any).
  318.      *
  319.      * @param body The body text (if any)
  320.      */
  321.     public void setBody(String body) {
  322.  
  323.         this.body = body;
  324.  
  325.     }
  326.  
  327.  
  328.     /**
  329.      * Return a String representation of this TeutoTreeNode.
  330.      */
  331.     public String toString() {
  332.  
  333.         StringBuffer sb = new StringBuffer();
  334.         toString(sb, 0, this);
  335.         return (sb.toString());
  336.  
  337.     }
  338.  
  339.  
  340.     // ------------------------------------------------------ Protected Methods
  341.  
  342.  
  343.     /**
  344.      * Append to the specified StringBuffer a character representation of
  345.      * this node, with the specified amount of indentation.
  346.      *
  347.      * @param sb The StringBuffer to append to
  348.      * @param indent Number of characters of indentation
  349.      * @param node The TeutoTreeNode to be printed
  350.      */
  351.     protected void toString(StringBuffer sb, int indent,
  352.                             TeutoTreeNode node) {
  353.  
  354.         int indent2 = indent + 2;
  355.  
  356.         // Reconstruct an opening node
  357.         for (int i = 0; i < indent; i++)
  358.             sb.append(' ');
  359.         sb.append('<');
  360.         sb.append(node.getName());
  361.         Enumeration names = node.findAttributes();
  362.         if(names!=null)
  363.           while (names.hasMoreElements()) {
  364.             sb.append(' ');
  365.             String name = (String) names.nextElement();
  366.             sb.append(name);
  367.             sb.append("=\"");
  368.             String value = node.findAttribute(name);
  369.             sb.append(value);
  370.             sb.append("\"");
  371.           }
  372.         sb.append(">\n");
  373.  
  374.         // Reconstruct the body text of this node (if any)
  375.         String body = node.getBody();
  376.         if ((body != null) && (body.length() > 0)) {
  377.             for (int i = 0; i < indent2; i++)
  378.                 sb.append(' ');
  379.             sb.append(body);
  380.             sb.append("\n");
  381.         }
  382.  
  383.         // Reconstruct child nodes with extra indentation
  384.         Enumeration children = node.findChildren();
  385.         while (children.hasMoreElements()) {
  386.             TeutoTreeNode child = (TeutoTreeNode) children.nextElement();
  387.             toString(sb, indent2, child);
  388.         }
  389.  
  390.         // Reconstruct a closing node marker
  391.         for (int i = 0; i < indent; i++)
  392.             sb.append(' ');
  393.         sb.append("</");
  394.         sb.append(node.getName());
  395.         sb.append(">\n");
  396.  
  397.     }
  398.  
  399.  
  400. }