KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > utils > Hashtree2Node


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
20 package com.sun.org.apache.xml.internal.utils;
21
22 import java.util.Enumeration JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29
30 /**
31  * Simple static utility to convert Hashtable to a Node.
32  *
33  * Please maintain JDK 1.1.x compatibility; no Collections!
34  *
35  * @see com.sun.org.apache.xalan.internal.xslt.EnvironmentCheck
36  * @see com.sun.org.apache.xalan.internal.lib.Extensions
37  * @author shane_curcuru@us.ibm.com
38  * @version $Id: Hashtree2Node.java,v 1.6 2004/02/17 04:21:14 minchau Exp $
39  * @xsl.usage general
40  */

41 public abstract class Hashtree2Node
42 {
43
44     /**
45      * Convert a Hashtable into a Node tree.
46      *
47      * <p>The hash may have either Hashtables as values (in which
48      * case we recurse) or other values, in which case we print them
49      * as &lt;item> elements, with a 'key' attribute with the value
50      * of the key, and the element contents as the value.</p>
51      *
52      * <p>If args are null we simply return without doing anything.
53      * If we encounter an error, we will attempt to add an 'ERROR'
54      * Element with exception info; if that doesn't work we simply
55      * return without doing anything else byt printStackTrace().</p>
56      *
57      * @param hash to get info from (may have sub-hashtables)
58      * @param name to use as parent element for appended node
59      * futurework could have namespace and prefix as well
60      * @param container Node to append our report to
61      * @param factory Document providing createElement, etc. services
62      */

63     public static void appendHashToNode(Hashtable JavaDoc hash, String JavaDoc name,
64             Node JavaDoc container, Document JavaDoc factory)
65     {
66         // Required arguments must not be null
67
if ((null == container) || (null == factory) || (null == hash))
68         {
69             return;
70         }
71
72         // name we will provide a default value for
73
String JavaDoc elemName = null;
74         if ((null == name) || ("".equals(name)))
75             elemName = "appendHashToNode";
76         else
77             elemName = name;
78
79         try
80         {
81             Element JavaDoc hashNode = factory.createElement(elemName);
82             container.appendChild(hashNode);
83
84             Enumeration JavaDoc keys = hash.keys();
85             Vector JavaDoc v = new Vector JavaDoc();
86
87             while (keys.hasMoreElements())
88             {
89                 Object JavaDoc key = keys.nextElement();
90                 String JavaDoc keyStr = key.toString();
91                 Object JavaDoc item = hash.get(key);
92
93                 if (item instanceof Hashtable JavaDoc)
94                 {
95                     // Ensure a pre-order traversal; add this hashes
96
// items before recursing to child hashes
97
// Save name and hash in two steps
98
v.addElement(keyStr);
99                     v.addElement((Hashtable JavaDoc) item);
100                 }
101                 else
102                 {
103                     try
104                     {
105                         // Add item to node
106
Element JavaDoc node = factory.createElement("item");
107                         node.setAttribute("key", keyStr);
108                         node.appendChild(factory.createTextNode((String JavaDoc)item));
109                         hashNode.appendChild(node);
110                     }
111                     catch (Exception JavaDoc e)
112                     {
113                         Element JavaDoc node = factory.createElement("item");
114                         node.setAttribute("key", keyStr);
115                         node.appendChild(factory.createTextNode("ERROR: Reading " + key + " threw: " + e.toString()));
116                         hashNode.appendChild(node);
117                     }
118                 }
119             }
120
121             // Now go back and do the saved hashes
122
keys = v.elements();
123             while (keys.hasMoreElements())
124             {
125                 // Retrieve name and hash in two steps
126
String JavaDoc n = (String JavaDoc) keys.nextElement();
127                 Hashtable JavaDoc h = (Hashtable JavaDoc) keys.nextElement();
128
129                 appendHashToNode(h, n, hashNode, factory);
130             }
131         }
132         catch (Exception JavaDoc e2)
133         {
134             // Ooops, just bail (suggestions for a safe thing
135
// to do in this case appreciated)
136
e2.printStackTrace();
137         }
138     }
139 }
140
Popular Tags