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