1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 2000, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xml.utils;
  58. import java.util.Enumeration;
  59. import java.util.Hashtable;
  60. import java.util.StringTokenizer;
  61. import java.util.Vector;
  62. import org.w3c.dom.Document;
  63. import org.w3c.dom.Element;
  64. import org.w3c.dom.Node;
  65. /**
  66. * <meta name="usage" content="general"/>
  67. * Simple static utility to convert Hashtable to a Node.
  68. *
  69. * Please maintain JDK 1.1.x compatibility; no Collections!
  70. *
  71. * @see org.apache.xalan.xslt.EnvironmentCheck
  72. * @see org.apache.xalan.lib.Extensions
  73. * @author shane_curcuru@us.ibm.com
  74. * @version $Id: Hashtree2Node.java,v 1.1 2002/06/21 15:39:34 curcuru Exp $
  75. */
  76. public abstract class Hashtree2Node
  77. {
  78. /**
  79. * Convert a Hashtable into a Node tree.
  80. *
  81. * <p>The hash may have either Hashtables as values (in which
  82. * case we recurse) or other values, in which case we print them
  83. * as <item> elements, with a 'key' attribute with the value
  84. * of the key, and the element contents as the value.</p>
  85. *
  86. * <p>If args are null we simply return without doing anything.
  87. * If we encounter an error, we will attempt to add an 'ERROR'
  88. * Element with exception info; if that doesn't work we simply
  89. * return without doing anything else byt printStackTrace().</p>
  90. *
  91. * @param hash to get info from (may have sub-hashtables)
  92. * @param name to use as parent element for appended node
  93. * futurework could have namespace and prefix as well
  94. * @param container Node to append our report to
  95. * @param factory Document providing createElement, etc. services
  96. */
  97. public static void appendHashToNode(Hashtable hash, String name,
  98. Node container, Document factory)
  99. {
  100. // Required arguments must not be null
  101. if ((null == container) || (null == factory) || (null == hash))
  102. {
  103. return;
  104. }
  105. // name we will provide a default value for
  106. String elemName = null;
  107. if ((null == name) || ("".equals(name)))
  108. elemName = "appendHashToNode";
  109. else
  110. elemName = name;
  111. try
  112. {
  113. Element hashNode = factory.createElement(elemName);
  114. container.appendChild(hashNode);
  115. Enumeration enum = hash.keys();
  116. Vector v = new Vector();
  117. while (enum.hasMoreElements())
  118. {
  119. Object key = enum.nextElement();
  120. String keyStr = key.toString();
  121. Object item = hash.get(key);
  122. if (item instanceof Hashtable)
  123. {
  124. // Ensure a pre-order traversal; add this hashes
  125. // items before recursing to child hashes
  126. // Save name and hash in two steps
  127. v.addElement(keyStr);
  128. v.addElement((Hashtable) item);
  129. }
  130. else
  131. {
  132. try
  133. {
  134. // Add item to node
  135. Element node = factory.createElement("item");
  136. node.setAttribute("key", keyStr);
  137. node.appendChild(factory.createTextNode((String)item));
  138. hashNode.appendChild(node);
  139. }
  140. catch (Exception e)
  141. {
  142. Element node = factory.createElement("item");
  143. node.setAttribute("key", keyStr);
  144. node.appendChild(factory.createTextNode("ERROR: Reading " + key + " threw: " + e.toString()));
  145. hashNode.appendChild(node);
  146. }
  147. }
  148. }
  149. // Now go back and do the saved hashes
  150. enum = v.elements();
  151. while (enum.hasMoreElements())
  152. {
  153. // Retrieve name and hash in two steps
  154. String n = (String) enum.nextElement();
  155. Hashtable h = (Hashtable) enum.nextElement();
  156. appendHashToNode(h, n, hashNode, factory);
  157. }
  158. }
  159. catch (Exception e2)
  160. {
  161. // Ooops, just bail (suggestions for a safe thing
  162. // to do in this case appreciated)
  163. e2.printStackTrace();
  164. }
  165. }
  166. }