001: /*
002: * Copyright 2002-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: Hashtree2Node.java,v 1.6 2004/02/17 04:21:14 minchau Exp $
018: */
019:
020: package org.apache.xml.utils;
021:
022: import java.util.Enumeration;
023: import java.util.Hashtable;
024: import java.util.Vector;
025:
026: import org.w3c.dom.Document;
027: import org.w3c.dom.Element;
028: import org.w3c.dom.Node;
029:
030: /**
031: * Simple static utility to convert Hashtable to a Node.
032: *
033: * Please maintain JDK 1.1.x compatibility; no Collections!
034: *
035: * @see org.apache.xalan.xslt.EnvironmentCheck
036: * @see org.apache.xalan.lib.Extensions
037: * @author shane_curcuru@us.ibm.com
038: * @version $Id: Hashtree2Node.java,v 1.6 2004/02/17 04:21:14 minchau Exp $
039: * @xsl.usage general
040: */
041: public abstract class Hashtree2Node {
042:
043: /**
044: * Convert a Hashtable into a Node tree.
045: *
046: * <p>The hash may have either Hashtables as values (in which
047: * case we recurse) or other values, in which case we print them
048: * as <item> elements, with a 'key' attribute with the value
049: * of the key, and the element contents as the value.</p>
050: *
051: * <p>If args are null we simply return without doing anything.
052: * If we encounter an error, we will attempt to add an 'ERROR'
053: * Element with exception info; if that doesn't work we simply
054: * return without doing anything else byt printStackTrace().</p>
055: *
056: * @param hash to get info from (may have sub-hashtables)
057: * @param name to use as parent element for appended node
058: * futurework could have namespace and prefix as well
059: * @param container Node to append our report to
060: * @param factory Document providing createElement, etc. services
061: */
062: public static void appendHashToNode(Hashtable hash, String name,
063: Node container, Document factory) {
064: // Required arguments must not be null
065: if ((null == container) || (null == factory) || (null == hash)) {
066: return;
067: }
068:
069: // name we will provide a default value for
070: String elemName = null;
071: if ((null == name) || ("".equals(name)))
072: elemName = "appendHashToNode";
073: else
074: elemName = name;
075:
076: try {
077: Element hashNode = factory.createElement(elemName);
078: container.appendChild(hashNode);
079:
080: Enumeration keys = hash.keys();
081: Vector v = new Vector();
082:
083: while (keys.hasMoreElements()) {
084: Object key = keys.nextElement();
085: String keyStr = key.toString();
086: Object item = hash.get(key);
087:
088: if (item instanceof Hashtable) {
089: // Ensure a pre-order traversal; add this hashes
090: // items before recursing to child hashes
091: // Save name and hash in two steps
092: v.addElement(keyStr);
093: v.addElement((Hashtable) item);
094: } else {
095: try {
096: // Add item to node
097: Element node = factory.createElement("item");
098: node.setAttribute("key", keyStr);
099: node.appendChild(factory
100: .createTextNode((String) item));
101: hashNode.appendChild(node);
102: } catch (Exception e) {
103: Element node = factory.createElement("item");
104: node.setAttribute("key", keyStr);
105: node.appendChild(factory
106: .createTextNode("ERROR: Reading " + key
107: + " threw: " + e.toString()));
108: hashNode.appendChild(node);
109: }
110: }
111: }
112:
113: // Now go back and do the saved hashes
114: keys = v.elements();
115: while (keys.hasMoreElements()) {
116: // Retrieve name and hash in two steps
117: String n = (String) keys.nextElement();
118: Hashtable h = (Hashtable) keys.nextElement();
119:
120: appendHashToNode(h, n, hashNode, factory);
121: }
122: } catch (Exception e2) {
123: // Ooops, just bail (suggestions for a safe thing
124: // to do in this case appreciated)
125: e2.printStackTrace();
126: }
127: }
128: }
|