JDK Java

/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.util.Iterator;
import java.util.List;
/**
 * Utility to take xml string and convert it to a javascript based tree within html.
 *
 * @author Tom Elrod
 */
public class XMLToHTMLTreeBuilder
{
   /**
    * Expects the xml to string to be in same format as calling JNDIView.listXML().
    * Will then build a tree in html that uses javascript.  Depends on dtree.js script
    * being available.
    *
    * @param xml
    * @return
    * @throws DocumentException
    */
   public static String convertJNDIXML(String xml) throws DocumentException
   {
      Document jndiXml = DocumentHelper.parseText(xml);
      String scriptHead = "\n";
      String ejbTree = convertEjbModule(jndiXml);
      String contextTree = convertContext(jndiXml);
      return scriptHead + ejbTree + "

" + contextTree;
   }
   private static String convertContext(Document jndiXml)
   {
      StringBuffer html = new StringBuffer();
      Element jndiRoot = jndiXml.getRootElement();
      List contextRoot = jndiRoot.elements("context");
      int globalCounter = 0;
      if (contextRoot.size() > 0)
      {
         html.append(createTreeCommandLinks("contextTree"));
         // create tree and add root node
         html.append("\n\n");
         html.append("\n

\n");
      }
      return html.toString();
   }
   private static String createTreeCommandLinks(String treeName)
   {
      return "

Expands all | Collapse all

";
   }
   private static String convertEjbModule(Document jndiXml)
   {
      StringBuffer html = new StringBuffer();
      Element jndiRoot = jndiXml.getRootElement();
      List ejbModules = jndiRoot.elements("ejbmodule");
      int globalCounter = 0;
      if (ejbModules.size() > 0)
      {
         html.append(createTreeCommandLinks("ejbTree"));
         // create tree and add root node
         html.append("\n\n");
      }
      return html.toString();
   }
   private static int buildTree(Element ejbElm, String[] searchNames, StringBuffer html,
                                int globalCounter, int parentId, String treeName)
   {
      if (searchNames != null)
      {
         for (int x = 0; x < searchNames.length; x++)
         {
            String searchName = searchNames[x];
            List contextElms = ejbElm.elements(searchName);
            Iterator elmItr = contextElms.iterator();
            while (elmItr.hasNext())
            {
               Element contextElm = (Element) elmItr.next();
               String name = "";
               String type = "";
               String typeValue = "";
               // check for context name
               Element nameElm = contextElm.element("name");
               if (nameElm != null)
               {
                  name = nameElm.getText();
               }
               Element attrElem = contextElm.element("attribute");
               if (attrElem != null)
               {
                  type = attrElem.attributeValue("name");
                  typeValue = attrElem.getText();
               }
               //html.append("treeName.add(" + globalCounter++ + ", " + parentId + ", '" + name + " -- " + type + "[" + typeValue + "]');");
               html.append(add(treeName, globalCounter++, parentId, name + " -- " + type + "[" + typeValue + "]"));
               // now recurse
               globalCounter = buildTree(contextElm, searchNames, html, globalCounter, globalCounter - 1, treeName);
            }
         }
      }
      return globalCounter;
   }
   private static String add(String tree, int global, int parent, String name)
   {
      return tree + ".add(" + global + ", " + parent + ", '" + name + "');\n";
   }
   public static void main(String[] args)
   {
      String xml = "\n" +
            "\t\n" +
            "\t\tnull\n" +
            "\t\t\n" +
            "\t\t\tjava:comp\n" +
            "\t\t\tMEJB\n" +
            "\t\t\t\n" +
            "\t\t\t\tenv\n" +
            "\t\t\t\torg.jnp.interfaces.NamingContext\n" +
            "\t\t\t\t\n" +
            "\t\t\t\t\tServer-Name\n" +
            "\t\t\t\t\tjava.lang.String\n" +
            "\t\t\t\t
\n" +
            "\t\t\t
\n" +
            "\t\t
\n" +
            "\t
\n" +
            "\t\n" +
            "\t\tjava:\n" +
            "\t\t\n" +
            "\t\t\tXAConnectionFactory\n" +
            "\t\t\torg.jboss.mq.SpyXAConnectionFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tDefaultDS\n" +
            "\t\t\tjavax.sql.DataSource\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tSecurityProxyFactory\n" +
            "\t\t\torg.jboss.security.SubjectSecurityProxyFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tDefaultJMSProvider\n" +
            "\t\t\torg.jboss.jms.jndi.JBossMQProvider\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tcomp\n" +
            "\t\t\tjavax.naming.Context\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tConnectionFactory\n" +
            "\t\t\torg.jboss.mq.SpyConnectionFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tJmsXA\n" +
            "\t\t\torg.jboss.resource.adapter.jms.JmsConnectionFactoryImpl\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tjaas\n" +
            "\t\t\tjavax.naming.Context\n" +
            "\t\t\t\n" +
            "\t\t\t\tJmsXARealm\n" +
            "\t\t\t\torg.jboss.security.plugins.SecurityDomainContext\n" +
            "\t\t\t
\n" +
            "\t\t\t\n" +
            "\t\t\t\tjbossmq\n" +
            "\t\t\t\torg.jboss.security.plugins.SecurityDomainContext\n" +
            "\t\t\t
\n" +
            "\t\t\t\n" +
            "\t\t\t\tHsqlDbRealm\n" +
            "\t\t\t\torg.jboss.security.plugins.SecurityDomainContext\n" +
            "\t\t\t
\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\ttimedCacheFactory\n" +
            "\t\t\tjavax.naming.Context\n" +
            "\t\t\t\n" +
            "\t\t\t\tFailed to list contents of: timedCacheFactory, errmsg=null\n" +
            "\t\t\t
\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tTransactionPropagationContextExporter\n" +
            "\t\t\torg.jboss.tm.TransactionPropagationContextFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tMail\n" +
            "\t\t\tjavax.mail.Session\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tStdJMSPool\n" +
            "\t\t\torg.jboss.jms.asf.StdServerSessionPoolFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tTransactionPropagationContextImporter\n" +
            "\t\t\torg.jboss.tm.TransactionPropagationContextImporter\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tTransactionManager\n" +
            "\t\t\torg.jboss.tm.TxManager\n" +
            "\t\t
\n" +
            "\t
\n" +
            "\t\n" +
            "\t\tGlobal\n" +
            "\t\t\n" +
            "\t\t\tjmx\n" +
            "\t\t\torg.jnp.interfaces.NamingContext\n" +
            "\t\t\t\n" +
            "\t\t\t\tinvoker\n" +
            "\t\t\t\torg.jnp.interfaces.NamingContext\n" +
            "\t\t\t\t\n" +
            "\t\t\t\t\tRMIAdaptor\n" +
            "\t\t\t\t\t$Proxy38\n" +
            "\t\t\t\t
\n" +
            "\t\t\t
\n" +
            "\t\t\t\n" +
            "\t\t\t\trmi\n" +
            "\t\t\t\torg.jnp.interfaces.NamingContext\n" +
            "\t\t\t\t\n" +
            "\t\t\t\t\tRMIAdaptor\n" +
            "\t\t\t\t\tjmx/invoker/RMIAdaptor\n" +
            "\t\t\t\t\tjavax.naming.LinkRef\n" +
            "\t\t\t\t
\n" +
            "\t\t\t
\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tOIL2XAConnectionFactory\n" +
            "\t\t\torg.jboss.mq.SpyXAConnectionFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tHTTPXAConnectionFactory\n" +
            "\t\t\torg.jboss.mq.SpyXAConnectionFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tConnectionFactory\n" +
            "\t\t\torg.jboss.mq.SpyConnectionFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tUserTransactionSessionFactory\n" +
            "\t\t\t$Proxy13\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tHTTPConnectionFactory\n" +
            "\t\t\torg.jboss.mq.SpyConnectionFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tXAConnectionFactory\n" +
            "\t\t\torg.jboss.mq.SpyXAConnectionFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tinvokers\n" +
            "\t\t\torg.jnp.interfaces.NamingContext\n" +
            "\t\t\t\n" +
            "\t\t\t\tTCK3\n" +
            "\t\t\t\torg.jnp.interfaces.NamingContext\n" +
            "\t\t\t\t\n" +
            "\t\t\t\t\tpooled\n" +
            "\t\t\t\t\torg.jboss.invocation.pooled.interfaces.PooledInvokerProxy\n" +
            "\t\t\t\t
\n" +
            "\t\t\t\t\n" +
            "\t\t\t\t\tjrmp\n" +
            "\t\t\t\t\torg.jboss.invocation.jrmp.interfaces.JRMPInvokerProxy\n" +
            "\t\t\t\t
\n" +
            "\t\t\t\t\n" +
            "\t\t\t\t\thttp\n" +
            "\t\t\t\t\torg.jboss.invocation.http.interfaces.HttpInvokerProxy\n" +
            "\t\t\t\t
\n" +
            "\t\t\t
\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tUserTransaction\n" +
            "\t\t\torg.jboss.tm.usertx.client.ClientUserTransaction\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tRMIXAConnectionFactory\n" +
            "\t\t\torg.jboss.mq.SpyXAConnectionFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tUIL2XAConnectionFactory\n" +
            "\t\t\torg.jboss.mq.SpyXAConnectionFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tqueue\n" +
            "\t\t\torg.jnp.interfaces.NamingContext\n" +
            "\t\t\t\n" +
            "\t\t\t\tA\n" +
            "\t\t\t\torg.jboss.mq.SpyQueue\n" +
            "\t\t\t
\n" +
            "\t\t\t\n" +
            "\t\t\t\ttestQueue\n" +
            "\t\t\t\torg.jboss.mq.SpyQueue\n" +
            "\t\t\t
\n" +
            "\t\t\t\n" +
            "\t\t\t\tex\n" +
            "\t\t\t\torg.jboss.mq.SpyQueue\n" +
            "\t\t\t
\n" +
            "\t\t\t\n" +
            "\t\t\t\tDLQ\n" +
            "\t\t\t\torg.jboss.mq.SpyQueue\n" +
            "\t\t\t
\n" +
            "\t\t\t\n" +
            "\t\t\t\tD\n" +
            "\t\t\t\torg.jboss.mq.SpyQueue\n" +
            "\t\t\t
\n" +
            "\t\t\t\n" +
            "\t\t\t\tC\n" +
            "\t\t\t\torg.jboss.mq.SpyQueue\n" +
            "\t\t\t
\n" +
            "\t\t\t\n" +
            "\t\t\t\tB\n" +
            "\t\t\t\torg.jboss.mq.SpyQueue\n" +
            "\t\t\t
\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\ttopic\n" +
            "\t\t\torg.jnp.interfaces.NamingContext\n" +
            "\t\t\t\n" +
            "\t\t\t\ttestDurableTopic\n" +
            "\t\t\t\torg.jboss.mq.SpyTopic\n" +
            "\t\t\t
\n" +
            "\t\t\t\n" +
            "\t\t\t\ttestTopic\n" +
            "\t\t\t\torg.jboss.mq.SpyTopic\n" +
            "\t\t\t
\n" +
            "\t\t\t\n" +
            "\t\t\t\tsecuredTopic\n" +
            "\t\t\t\torg.jboss.mq.SpyTopic\n" +
            "\t\t\t
\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tconsole\n" +
            "\t\t\torg.jnp.interfaces.NamingContext\n" +
            "\t\t\t\n" +
            "\t\t\t\tPluginManager\n" +
            "\t\t\t\t$Proxy39\n" +
            "\t\t\t
\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tUIL2ConnectionFactory\n" +
            "\t\t\torg.jboss.mq.SpyConnectionFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tHiLoKeyGeneratorFactory\n" +
            "\t\t\torg.jboss.ejb.plugins.keygenerator.hilo.HiLoKeyGeneratorFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tRMIConnectionFactory\n" +
            "\t\t\torg.jboss.mq.SpyConnectionFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tejb\n" +
            "\t\t\torg.jnp.interfaces.NamingContext\n" +
            "\t\t\t\n" +
            "\t\t\t\tmgmt\n" +
            "\t\t\t\torg.jnp.interfaces.NamingContext\n" +
            "\t\t\t\t\n" +
            "\t\t\t\t\tMEJB\n" +
            "\t\t\t\t\t$Proxy44\n" +
            "\t\t\t\t
\n" +
            "\t\t\t
\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tOIL2ConnectionFactory\n" +
            "\t\t\torg.jboss.mq.SpyConnectionFactory\n" +
            "\t\t
\n" +
            "\t\t\n" +
            "\t\t\tUUIDKeyGeneratorFactory\n" +
            "\t\t\torg.jboss.ejb.plugins.keygenerator.uuid.UUIDKeyGeneratorFactory\n" +
            "\t\t
\n" +
            "\t
\n" +
            "
";
      String html = null;
      try
      {
         html = XMLToHTMLTreeBuilder.convertJNDIXML(xml);
      }
      catch (DocumentException e)
      {
         e.printStackTrace();
      }
      System.out.println("HTML output:\n\n" + html);
   }
}