1 22 package org.jboss.console.util; 23 24 import org.dom4j.Document; 25 import org.dom4j.DocumentException; 26 import org.dom4j.DocumentHelper; 27 import org.dom4j.Element; 28 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 37 public class XMLToHTMLTreeBuilder 38 { 39 48 public static String convertJNDIXML(String xml) throws DocumentException 49 { 50 Document jndiXml = DocumentHelper.parseText(xml); 51 52 String scriptHead = "<div class=\"dtree\"><script type=\"text/javascript\" SRC=\"dtree.js\"></script>\n"; 53 54 String ejbTree = convertEjbModule(jndiXml); 55 56 String contextTree = convertContext(jndiXml); 57 58 return scriptHead + ejbTree + "<p>" + contextTree; 59 } 60 61 private static String convertContext(Document jndiXml) 62 { 63 StringBuffer html = new StringBuffer (); 64 65 Element jndiRoot = jndiXml.getRootElement(); 66 67 List contextRoot = jndiRoot.elements("context"); 68 int globalCounter = 0; 69 if (contextRoot.size() > 0) 70 { 71 html.append(createTreeCommandLinks("contextTree")); 72 73 html.append("<script type=\"text/javascript\">\n<!--\n"); 75 html.append("contextTree = new dTree('contextTree');\n"); 76 html.append("contextTree.add(" + globalCounter++ + ",-1,'JNDI Root Context');\n"); 77 78 79 int parentId = 0; 80 81 Iterator itr = contextRoot.iterator(); 82 while (itr.hasNext()) 83 { 84 Element contextElm = (Element) itr.next(); 85 String contextElmName = contextElm.getName(); 86 Element nameSub = contextElm.element("name"); 87 if (nameSub != null) 88 { 89 contextElmName = nameSub.getText(); 90 } 91 html.append(add("contextTree", globalCounter++, parentId, contextElmName)); 93 95 String [] searchNames = new String []{"context", "leaf"}; 96 97 globalCounter = buildTree(contextElm, searchNames, html, globalCounter, globalCounter - 1, "contextTree"); 98 99 } 100 101 html.append("document.write(contextTree);"); 102 html.append("\n//-->\n</script>"); 103 html.append("\n</div>\n"); 104 } 105 106 107 return html.toString(); 108 } 109 110 private static String createTreeCommandLinks(String treeName) 111 { 112 return "<p><a HREF=\"javascript: " + treeName + ".openAll();\">Expands all</a> | <a HREF=\"javascript: " + treeName + ".closeAll();\">Collapse all</a></p>"; 113 } 114 115 private static String convertEjbModule(Document jndiXml) 116 { 117 StringBuffer html = new StringBuffer (); 118 119 Element jndiRoot = jndiXml.getRootElement(); 120 121 List ejbModules = jndiRoot.elements("ejbmodule"); 122 int globalCounter = 0; 123 if (ejbModules.size() > 0) 124 { 125 html.append(createTreeCommandLinks("ejbTree")); 126 127 html.append("<script type=\"text/javascript\">\n<!--\n"); 129 html.append("ejbTree = new dTree('ejbTree');\n"); 130 html.append("ejbTree.add(" + globalCounter++ + ",-1,'EJB Modules');\n"); 131 132 133 int parentId = 0; 134 135 Iterator itr = ejbModules.iterator(); 136 while (itr.hasNext()) 137 { 138 Element ejbElm = (Element) itr.next(); 139 String ejbElmName = ejbElm.getName(); 140 html.append(add("ejbTree", globalCounter++, parentId, ejbElmName)); 142 parentId++; 143 144 String [] searchNames = new String []{"context", "leaf"}; 145 146 globalCounter = buildTree(ejbElm, searchNames, html, globalCounter, parentId, "ejbTree"); 147 148 149 } 150 151 html.append("document.write(ejbTree);"); 152 html.append("\n//-->\n</script>"); 153 } 154 155 156 return html.toString(); 157 } 158 159 private static int buildTree(Element ejbElm, String [] searchNames, StringBuffer html, 160 int globalCounter, int parentId, String treeName) 161 { 162 if (searchNames != null) 163 { 164 for (int x = 0; x < searchNames.length; x++) 165 { 166 String searchName = searchNames[x]; 167 168 List contextElms = ejbElm.elements(searchName); 169 Iterator elmItr = contextElms.iterator(); 170 while (elmItr.hasNext()) 171 { 172 Element contextElm = (Element) elmItr.next(); 173 174 String name = ""; 175 String type = ""; 176 String typeValue = ""; 177 178 Element nameElm = contextElm.element("name"); 180 if (nameElm != null) 181 { 182 name = nameElm.getText(); 183 } 184 Element attrElem = contextElm.element("attribute"); 185 if (attrElem != null) 186 { 187 type = attrElem.attributeValue("name"); 188 typeValue = attrElem.getText(); 189 } 190 191 html.append(add(treeName, globalCounter++, parentId, name + " -- " + type + "[" + typeValue + "]")); 193 194 globalCounter = buildTree(contextElm, searchNames, html, globalCounter, globalCounter - 1, treeName); 196 197 } 198 } 199 } 200 return globalCounter; 201 } 202 203 private static String add(String tree, int global, int parent, String name) 204 { 205 return tree + ".add(" + global + ", " + parent + ", '" + name + "');\n"; 206 } 207 208 209 public static void main(String [] args) 210 { 211 String xml = "<jndi>\n" + 212 "\t<ejbmodule>\n" + 213 "\t\t<file>null</file>\n" + 214 "\t\t<context>\n" + 215 "\t\t\t<name>java:comp</name>\n" + 216 "\t\t\t<attribute name=\"bean\">MEJB</attribute>\n" + 217 "\t\t\t<context>\n" + 218 "\t\t\t\t<name>env</name>\n" + 219 "\t\t\t\t<attribute name=\"class\">org.jnp.interfaces.NamingContext</attribute>\n" + 220 "\t\t\t\t<leaf>\n" + 221 "\t\t\t\t\t<name>Server-Name</name>\n" + 222 "\t\t\t\t\t<attribute name=\"class\">java.lang.String</attribute>\n" + 223 "\t\t\t\t</leaf>\n" + 224 "\t\t\t</context>\n" + 225 "\t\t</context>\n" + 226 "\t</ejbmodule>\n" + 227 "\t<context>\n" + 228 "\t\t<name>java:</name>\n" + 229 "\t\t<leaf>\n" + 230 "\t\t\t<name>XAConnectionFactory</name>\n" + 231 "\t\t\t<attribute name=\"class\">org.jboss.mq.SpyXAConnectionFactory</attribute>\n" + 232 "\t\t</leaf>\n" + 233 "\t\t<leaf>\n" + 234 "\t\t\t<name>DefaultDS</name>\n" + 235 "\t\t\t<attribute name=\"class\">javax.sql.DataSource</attribute>\n" + 236 "\t\t</leaf>\n" + 237 "\t\t<leaf>\n" + 238 "\t\t\t<name>SecurityProxyFactory</name>\n" + 239 "\t\t\t<attribute name=\"class\">org.jboss.security.SubjectSecurityProxyFactory</attribute>\n" + 240 "\t\t</leaf>\n" + 241 "\t\t<leaf>\n" + 242 "\t\t\t<name>DefaultJMSProvider</name>\n" + 243 "\t\t\t<attribute name=\"class\">org.jboss.jms.jndi.JBossMQProvider</attribute>\n" + 244 "\t\t</leaf>\n" + 245 "\t\t<context>\n" + 246 "\t\t\t<name>comp</name>\n" + 247 "\t\t\t<attribute name=\"class\">javax.naming.Context</attribute>\n" + 248 "\t\t</context>\n" + 249 "\t\t<leaf>\n" + 250 "\t\t\t<name>ConnectionFactory</name>\n" + 251 "\t\t\t<attribute name=\"class\">org.jboss.mq.SpyConnectionFactory</attribute>\n" + 252 "\t\t</leaf>\n" + 253 "\t\t<leaf>\n" + 254 "\t\t\t<name>JmsXA</name>\n" + 255 "\t\t\t<attribute name=\"class\">org.jboss.resource.adapter.jms.JmsConnectionFactoryImpl</attribute>\n" + 256 "\t\t</leaf>\n" + 257 "\t\t<context>\n" + 258 "\t\t\t<name>jaas</name>\n" + 259 "\t\t\t<attribute name=\"class\">javax.naming.Context</attribute>\n" + 260 "\t\t\t<leaf>\n" + 261 "\t\t\t\t<name>JmsXARealm</name>\n" + 262 "\t\t\t\t<attribute name=\"class\">org.jboss.security.plugins.SecurityDomainContext</attribute>\n" + 263 "\t\t\t</leaf>\n" + 264 "\t\t\t<leaf>\n" + 265 "\t\t\t\t<name>jbossmq</name>\n" + 266 "\t\t\t\t<attribute name=\"class\">org.jboss.security.plugins.SecurityDomainContext</attribute>\n" + 267 "\t\t\t</leaf>\n" + 268 "\t\t\t<leaf>\n" + 269 "\t\t\t\t<name>HsqlDbRealm</name>\n" + 270 "\t\t\t\t<attribute name=\"class\">org.jboss.security.plugins.SecurityDomainContext</attribute>\n" + 271 "\t\t\t</leaf>\n" + 272 "\t\t</context>\n" + 273 "\t\t<context>\n" + 274 "\t\t\t<name>timedCacheFactory</name>\n" + 275 "\t\t\t<attribute name=\"class\">javax.naming.Context</attribute>\n" + 276 "\t\t\t<error>\n" + 277 "\t\t\t\t<message>Failed to list contents of: timedCacheFactory, errmsg=null</message>\n" + 278 "\t\t\t</error>\n" + 279 "\t\t</context>\n" + 280 "\t\t<leaf>\n" + 281 "\t\t\t<name>TransactionPropagationContextExporter</name>\n" + 282 "\t\t\t<attribute name=\"class\">org.jboss.tm.TransactionPropagationContextFactory</attribute>\n" + 283 "\t\t</leaf>\n" + 284 "\t\t<leaf>\n" + 285 "\t\t\t<name>Mail</name>\n" + 286 "\t\t\t<attribute name=\"class\">javax.mail.Session</attribute>\n" + 287 "\t\t</leaf>\n" + 288 "\t\t<leaf>\n" + 289 "\t\t\t<name>StdJMSPool</name>\n" + 290 "\t\t\t<attribute name=\"class\">org.jboss.jms.asf.StdServerSessionPoolFactory</attribute>\n" + 291 "\t\t</leaf>\n" + 292 "\t\t<leaf>\n" + 293 "\t\t\t<name>TransactionPropagationContextImporter</name>\n" + 294 "\t\t\t<attribute name=\"class\">org.jboss.tm.TransactionPropagationContextImporter</attribute>\n" + 295 "\t\t</leaf>\n" + 296 "\t\t<leaf>\n" + 297 "\t\t\t<name>TransactionManager</name>\n" + 298 "\t\t\t<attribute name=\"class\">org.jboss.tm.TxManager</attribute>\n" + 299 "\t\t</leaf>\n" + 300 "\t</context>\n" + 301 "\t<context>\n" + 302 "\t\t<name>Global</name>\n" + 303 "\t\t<context>\n" + 304 "\t\t\t<name>jmx</name>\n" + 305 "\t\t\t<attribute name=\"class\">org.jnp.interfaces.NamingContext</attribute>\n" + 306 "\t\t\t<context>\n" + 307 "\t\t\t\t<name>invoker</name>\n" + 308 "\t\t\t\t<attribute name=\"class\">org.jnp.interfaces.NamingContext</attribute>\n" + 309 "\t\t\t\t<leaf>\n" + 310 "\t\t\t\t\t<name>RMIAdaptor</name>\n" + 311 "\t\t\t\t\t<attribute name=\"class\">$Proxy38</attribute>\n" + 312 "\t\t\t\t</leaf>\n" + 313 "\t\t\t</context>\n" + 314 "\t\t\t<context>\n" + 315 "\t\t\t\t<name>rmi</name>\n" + 316 "\t\t\t\t<attribute name=\"class\">org.jnp.interfaces.NamingContext</attribute>\n" + 317 "\t\t\t\t<link-ref>\n" + 318 "\t\t\t\t\t<name>RMIAdaptor</name>\n" + 319 "\t\t\t\t\t<link>jmx/invoker/RMIAdaptor</link>\n" + 320 "\t\t\t\t\t<attribute name=\"class\">javax.naming.LinkRef</attribute>\n" + 321 "\t\t\t\t</link-ref>\n" + 322 "\t\t\t</context>\n" + 323 "\t\t</context>\n" + 324 "\t\t<leaf>\n" + 325 "\t\t\t<name>OIL2XAConnectionFactory</name>\n" + 326 "\t\t\t<attribute name=\"class\">org.jboss.mq.SpyXAConnectionFactory</attribute>\n" + 327 "\t\t</leaf>\n" + 328 "\t\t<leaf>\n" + 329 "\t\t\t<name>HTTPXAConnectionFactory</name>\n" + 330 "\t\t\t<attribute name=\"class\">org.jboss.mq.SpyXAConnectionFactory</attribute>\n" + 331 "\t\t</leaf>\n" + 332 "\t\t<leaf>\n" + 333 "\t\t\t<name>ConnectionFactory</name>\n" + 334 "\t\t\t<attribute name=\"class\">org.jboss.mq.SpyConnectionFactory</attribute>\n" + 335 "\t\t</leaf>\n" + 336 "\t\t<leaf>\n" + 337 "\t\t\t<name>UserTransactionSessionFactory</name>\n" + 338 "\t\t\t<attribute name=\"class\">$Proxy13</attribute>\n" + 339 "\t\t</leaf>\n" + 340 "\t\t<leaf>\n" + 341 "\t\t\t<name>HTTPConnectionFactory</name>\n" + 342 "\t\t\t<attribute name=\"class\">org.jboss.mq.SpyConnectionFactory</attribute>\n" + 343 "\t\t</leaf>\n" + 344 "\t\t<leaf>\n" + 345 "\t\t\t<name>XAConnectionFactory</name>\n" + 346 "\t\t\t<attribute name=\"class\">org.jboss.mq.SpyXAConnectionFactory</attribute>\n" + 347 "\t\t</leaf>\n" + 348 "\t\t<context>\n" + 349 "\t\t\t<name>invokers</name>\n" + 350 "\t\t\t<attribute name=\"class\">org.jnp.interfaces.NamingContext</attribute>\n" + 351 "\t\t\t<context>\n" + 352 "\t\t\t\t<name>TCK3</name>\n" + 353 "\t\t\t\t<attribute name=\"class\">org.jnp.interfaces.NamingContext</attribute>\n" + 354 "\t\t\t\t<leaf>\n" + 355 "\t\t\t\t\t<name>pooled</name>\n" + 356 "\t\t\t\t\t<attribute name=\"class\">org.jboss.invocation.pooled.interfaces.PooledInvokerProxy</attribute>\n" + 357 "\t\t\t\t</leaf>\n" + 358 "\t\t\t\t<leaf>\n" + 359 "\t\t\t\t\t<name>jrmp</name>\n" + 360 "\t\t\t\t\t<attribute name=\"class\">org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxy</attribute>\n" + 361 "\t\t\t\t</leaf>\n" + 362 "\t\t\t\t<leaf>\n" + 363 "\t\t\t\t\t<name>http</name>\n" + 364 "\t\t\t\t\t<attribute name=\"class\">org.jboss.invocation.http.interfaces.HttpInvokerProxy</attribute>\n" + 365 "\t\t\t\t</leaf>\n" + 366 "\t\t\t</context>\n" + 367 "\t\t</context>\n" + 368 "\t\t<leaf>\n" + 369 "\t\t\t<name>UserTransaction</name>\n" + 370 "\t\t\t<attribute name=\"class\">org.jboss.tm.usertx.client.ClientUserTransaction</attribute>\n" + 371 "\t\t</leaf>\n" + 372 "\t\t<leaf>\n" + 373 "\t\t\t<name>RMIXAConnectionFactory</name>\n" + 374 "\t\t\t<attribute name=\"class\">org.jboss.mq.SpyXAConnectionFactory</attribute>\n" + 375 "\t\t</leaf>\n" + 376 "\t\t<leaf>\n" + 377 "\t\t\t<name>UIL2XAConnectionFactory</name>\n" + 378 "\t\t\t<attribute name=\"class\">org.jboss.mq.SpyXAConnectionFactory</attribute>\n" + 379 "\t\t</leaf>\n" + 380 "\t\t<context>\n" + 381 "\t\t\t<name>queue</name>\n" + 382 "\t\t\t<attribute name=\"class\">org.jnp.interfaces.NamingContext</attribute>\n" + 383 "\t\t\t<leaf>\n" + 384 "\t\t\t\t<name>A</name>\n" + 385 "\t\t\t\t<attribute name=\"class\">org.jboss.mq.SpyQueue</attribute>\n" + 386 "\t\t\t</leaf>\n" + 387 "\t\t\t<leaf>\n" + 388 "\t\t\t\t<name>testQueue</name>\n" + 389 "\t\t\t\t<attribute name=\"class\">org.jboss.mq.SpyQueue</attribute>\n" + 390 "\t\t\t</leaf>\n" + 391 "\t\t\t<leaf>\n" + 392 "\t\t\t\t<name>ex</name>\n" + 393 "\t\t\t\t<attribute name=\"class\">org.jboss.mq.SpyQueue</attribute>\n" + 394 "\t\t\t</leaf>\n" + 395 "\t\t\t<leaf>\n" + 396 "\t\t\t\t<name>DLQ</name>\n" + 397 "\t\t\t\t<attribute name=\"class\">org.jboss.mq.SpyQueue</attribute>\n" + 398 "\t\t\t</leaf>\n" + 399 "\t\t\t<leaf>\n" + 400 "\t\t\t\t<name>D</name>\n" + 401 "\t\t\t\t<attribute name=\"class\">org.jboss.mq.SpyQueue</attribute>\n" + 402 "\t\t\t</leaf>\n" + 403 "\t\t\t<leaf>\n" + 404 "\t\t\t\t<name>C</name>\n" + 405 "\t\t\t\t<attribute name=\"class\">org.jboss.mq.SpyQueue</attribute>\n" + 406 "\t\t\t</leaf>\n" + 407 "\t\t\t<leaf>\n" + 408 "\t\t\t\t<name>B</name>\n" + 409 "\t\t\t\t<attribute name=\"class\">org.jboss.mq.SpyQueue</attribute>\n" + 410 "\t\t\t</leaf>\n" + 411 "\t\t</context>\n" + 412 "\t\t<context>\n" + 413 "\t\t\t<name>topic</name>\n" + 414 "\t\t\t<attribute name=\"class\">org.jnp.interfaces.NamingContext</attribute>\n" + 415 "\t\t\t<leaf>\n" + 416 "\t\t\t\t<name>testDurableTopic</name>\n" + 417 "\t\t\t\t<attribute name=\"class\">org.jboss.mq.SpyTopic</attribute>\n" + 418 "\t\t\t</leaf>\n" + 419 "\t\t\t<leaf>\n" + 420 "\t\t\t\t<name>testTopic</name>\n" + 421 "\t\t\t\t<attribute name=\"class\">org.jboss.mq.SpyTopic</attribute>\n" + 422 "\t\t\t</leaf>\n" + 423 "\t\t\t<leaf>\n" + 424 "\t\t\t\t<name>securedTopic</name>\n" + 425 "\t\t\t\t<attribute name=\"class\">org.jboss.mq.SpyTopic</attribute>\n" + 426 "\t\t\t</leaf>\n" + 427 "\t\t</context>\n" + 428 "\t\t<context>\n" + 429 "\t\t\t<name>console</name>\n" + 430 "\t\t\t<attribute name=\"class\">org.jnp.interfaces.NamingContext</attribute>\n" + 431 "\t\t\t<leaf>\n" + 432 "\t\t\t\t<name>PluginManager</name>\n" + 433 "\t\t\t\t<attribute name=\"class\">$Proxy39</attribute>\n" + 434 "\t\t\t</leaf>\n" + 435 "\t\t</context>\n" + 436 "\t\t<leaf>\n" + 437 "\t\t\t<name>UIL2ConnectionFactory</name>\n" + 438 "\t\t\t<attribute name=\"class\">org.jboss.mq.SpyConnectionFactory</attribute>\n" + 439 "\t\t</leaf>\n" + 440 "\t\t<leaf>\n" + 441 "\t\t\t<name>HiLoKeyGeneratorFactory</name>\n" + 442 "\t\t\t<attribute name=\"class\">org.jboss.ejb.plugins.keygenerator.hilo.HiLoKeyGeneratorFactory</attribute>\n" + 443 "\t\t</leaf>\n" + 444 "\t\t<leaf>\n" + 445 "\t\t\t<name>RMIConnectionFactory</name>\n" + 446 "\t\t\t<attribute name=\"class\">org.jboss.mq.SpyConnectionFactory</attribute>\n" + 447 "\t\t</leaf>\n" + 448 "\t\t<context>\n" + 449 "\t\t\t<name>ejb</name>\n" + 450 "\t\t\t<attribute name=\"class\">org.jnp.interfaces.NamingContext</attribute>\n" + 451 "\t\t\t<context>\n" + 452 "\t\t\t\t<name>mgmt</name>\n" + 453 "\t\t\t\t<attribute name=\"class\">org.jnp.interfaces.NamingContext</attribute>\n" + 454 "\t\t\t\t<leaf>\n" + 455 "\t\t\t\t\t<name>MEJB</name>\n" + 456 "\t\t\t\t\t<attribute name=\"class\">$Proxy44</attribute>\n" + 457 "\t\t\t\t</leaf>\n" + 458 "\t\t\t</context>\n" + 459 "\t\t</context>\n" + 460 "\t\t<leaf>\n" + 461 "\t\t\t<name>OIL2ConnectionFactory</name>\n" + 462 "\t\t\t<attribute name=\"class\">org.jboss.mq.SpyConnectionFactory</attribute>\n" + 463 "\t\t</leaf>\n" + 464 "\t\t<leaf>\n" + 465 "\t\t\t<name>UUIDKeyGeneratorFactory</name>\n" + 466 "\t\t\t<attribute name=\"class\">org.jboss.ejb.plugins.keygenerator.uuid.UUIDKeyGeneratorFactory</attribute>\n" + 467 "\t\t</leaf>\n" + 468 "\t</context>\n" + 469 "</jndi>"; 470 471 String html = null; 472 try 473 { 474 html = XMLToHTMLTreeBuilder.convertJNDIXML(xml); 475 } 476 catch (DocumentException e) 477 { 478 e.printStackTrace(); 479 } 480 System.out.println("HTML output:\n\n" + html); 481 } 482 483 } | Popular Tags |