1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: Hashtree2Node.java,v 1.6 2004/02/17 04:21:14 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. import java.util.Enumeration;
  21. import java.util.Hashtable;
  22. import java.util.Vector;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.Element;
  25. import org.w3c.dom.Node;
  26. /**
  27. * Simple static utility to convert Hashtable to a Node.
  28. *
  29. * Please maintain JDK 1.1.x compatibility; no Collections!
  30. *
  31. * @see com.sun.org.apache.xalan.internal.xslt.EnvironmentCheck
  32. * @see com.sun.org.apache.xalan.internal.lib.Extensions
  33. * @author shane_curcuru@us.ibm.com
  34. * @version $Id: Hashtree2Node.java,v 1.6 2004/02/17 04:21:14 minchau Exp $
  35. * @xsl.usage general
  36. */
  37. public abstract class Hashtree2Node
  38. {
  39. /**
  40. * Convert a Hashtable into a Node tree.
  41. *
  42. * <p>The hash may have either Hashtables as values (in which
  43. * case we recurse) or other values, in which case we print them
  44. * as <item> elements, with a 'key' attribute with the value
  45. * of the key, and the element contents as the value.</p>
  46. *
  47. * <p>If args are null we simply return without doing anything.
  48. * If we encounter an error, we will attempt to add an 'ERROR'
  49. * Element with exception info; if that doesn't work we simply
  50. * return without doing anything else byt printStackTrace().</p>
  51. *
  52. * @param hash to get info from (may have sub-hashtables)
  53. * @param name to use as parent element for appended node
  54. * futurework could have namespace and prefix as well
  55. * @param container Node to append our report to
  56. * @param factory Document providing createElement, etc. services
  57. */
  58. public static void appendHashToNode(Hashtable hash, String name,
  59. Node container, Document factory)
  60. {
  61. // Required arguments must not be null
  62. if ((null == container) || (null == factory) || (null == hash))
  63. {
  64. return;
  65. }
  66. // name we will provide a default value for
  67. String elemName = null;
  68. if ((null == name) || ("".equals(name)))
  69. elemName = "appendHashToNode";
  70. else
  71. elemName = name;
  72. try
  73. {
  74. Element hashNode = factory.createElement(elemName);
  75. container.appendChild(hashNode);
  76. Enumeration keys = hash.keys();
  77. Vector v = new Vector();
  78. while (keys.hasMoreElements())
  79. {
  80. Object key = keys.nextElement();
  81. String keyStr = key.toString();
  82. Object item = hash.get(key);
  83. if (item instanceof Hashtable)
  84. {
  85. // Ensure a pre-order traversal; add this hashes
  86. // items before recursing to child hashes
  87. // Save name and hash in two steps
  88. v.addElement(keyStr);
  89. v.addElement((Hashtable) item);
  90. }
  91. else
  92. {
  93. try
  94. {
  95. // Add item to node
  96. Element node = factory.createElement("item");
  97. node.setAttribute("key", keyStr);
  98. node.appendChild(factory.createTextNode((String)item));
  99. hashNode.appendChild(node);
  100. }
  101. catch (Exception e)
  102. {
  103. Element node = factory.createElement("item");
  104. node.setAttribute("key", keyStr);
  105. node.appendChild(factory.createTextNode("ERROR: Reading " + key + " threw: " + e.toString()));
  106. hashNode.appendChild(node);
  107. }
  108. }
  109. }
  110. // Now go back and do the saved hashes
  111. keys = v.elements();
  112. while (keys.hasMoreElements())
  113. {
  114. // Retrieve name and hash in two steps
  115. String n = (String) keys.nextElement();
  116. Hashtable h = (Hashtable) keys.nextElement();
  117. appendHashToNode(h, n, hashNode, factory);
  118. }
  119. }
  120. catch (Exception e2)
  121. {
  122. // Ooops, just bail (suggestions for a safe thing
  123. // to do in this case appreciated)
  124. e2.printStackTrace();
  125. }
  126. }
  127. }