KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > tree > TreeReader


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.admingui.tree;
25
26 import org.xml.sax.*;
27 import org.xml.sax.helpers.*;
28 import org.w3c.dom.*;
29 import org.w3c.dom.Document JavaDoc;
30
31 import javax.xml.parsers.*;
32 import javax.xml.transform.*;
33 import javax.xml.transform.dom.DOMSource JavaDoc;
34 import javax.xml.transform.stream.StreamResult JavaDoc;
35 import javax.servlet.*;
36
37 import com.iplanet.jato.RequestContext;
38 import com.sun.enterprise.tools.guiframework.view.ViewXMLEntityResolver;
39
40 import java.io.InputStream JavaDoc;
41 import java.io.OutputStreamWriter JavaDoc;
42 import java.io.PrintWriter JavaDoc;
43 import java.util.HashMap JavaDoc;
44
45 import com.sun.enterprise.tools.admingui.util.Util;
46 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
47
48 public class TreeReader {
49     private static final String JavaDoc OUTPUT_ENCODING = "UTF-8";
50     private Document JavaDoc doc;
51     private static HashMap JavaDoc nodeClasses = new HashMap JavaDoc();
52     
53     public TreeReader(InputStream JavaDoc is, String JavaDoc dtdURLBase) throws Exception JavaDoc {
54         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
55         dbf.setNamespaceAware(true);
56         dbf.setValidating(false);
57         dbf.setIgnoringComments(false);
58         dbf.setIgnoringElementContentWhitespace(false);
59         dbf.setCoalescing(false);
60         // The opposite of creating entity ref nodes is expanding them inline
61
dbf.setExpandEntityReferences(true);
62         
63         DocumentBuilder db = dbf.newDocumentBuilder();
64         db.setEntityResolver(new ViewXMLEntityResolver());
65
66         OutputStreamWriter JavaDoc errorWriter = new OutputStreamWriter JavaDoc(System.err, OUTPUT_ENCODING);
67         db.setErrorHandler(new MyErrorHandler(new PrintWriter JavaDoc(errorWriter, true)));
68     doc = db.parse(is, dtdURLBase);
69     }
70     
71     /**
72      * Recursive routine to process DOM tree nodes
73      */

74     public static boolean process(Node n, IndexTreeModel treeModel, IndexTreeNode parent) {
75         boolean hasChildren = false;
76         for (Node child = n.getFirstChild(); child != null; child = child.getNextSibling()) {
77             String JavaDoc nodeName = child.getNodeName();
78             if (nodeName.equalsIgnoreCase("Node")) {
79                 IndexTreeNode treeNode = addToTree((Element)child, treeModel, parent);
80                 process(child, treeModel, treeNode);
81                 hasChildren = true;
82             }
83             else if (nodeName.equalsIgnoreCase("DynamicNode")) {
84                 parent.setDynamicChild((Element)child);
85                 hasChildren = true;
86             }
87             else if (nodeName.equalsIgnoreCase("TreeNodeType")) {
88                 process((Element)child);
89             }
90         }
91         return hasChildren;
92     }
93     
94     private static void process(Element treeNodeType) {
95         String JavaDoc name = treeNodeType.getAttribute("name");
96         String JavaDoc clazz = treeNodeType.getAttribute("treeNodeClass");
97         nodeClasses.put(name, clazz);
98     }
99     
100     public static String JavaDoc getClass(String JavaDoc nodeClassType) {
101         return (String JavaDoc) nodeClasses.get(nodeClassType);
102     }
103     
104     private static IndexTreeNode addToTree(Element node, IndexTreeModel treeModel, IndexTreeNode parent) {
105         String JavaDoc name = node.getAttribute("name"); // name is required attribute
106
if (name.startsWith("$")) {
107             int i = name.indexOf('#');
108             if (i > 0) {
109                 String JavaDoc mbeanName = name.substring(1,i);
110                 String JavaDoc attrName = name.substring(i+1, name.length());
111                 name = (String JavaDoc)MBeanUtil.getAttribute(mbeanName, attrName);
112             }
113         } /*else {
114             
115             name = Util.getMessage(name); // get the I18N value.
116         } */

117         String JavaDoc nodeType = node.getAttribute("type");
118         if (nodeType == null)
119             nodeType = treeModel.CONTAINER;
120
121         if ((parent == null && nodeType.equalsIgnoreCase(treeModel.ROOT) == false) ||
122             (parent != null && nodeType.equalsIgnoreCase(treeModel.ROOT))) {
123             throw new RuntimeException JavaDoc("The first node declared must be of type \"root\" and\n" +
124                                        "there can be only one root node.");
125         }
126
127         IndexTreeNode treeNode =
128             IndexTreeNode.createNode(node, parent, nodeType, name, treeModel);
129         return treeNode;
130     }
131     
132     public void populate(IndexTreeModel treeModel) throws Exception JavaDoc {
133         for (Node child = doc.getFirstChild(); child != null;
134                     child = child.getNextSibling()) {
135             String JavaDoc nodeName = child.getNodeName();
136             if (nodeName.equalsIgnoreCase("Tree"))
137                 process(child, treeModel, null);
138         }
139     }
140     
141     // Error handler to report errors and warnings
142
private static class MyErrorHandler implements ErrorHandler {
143         /** Error handler output goes here */
144         private PrintWriter JavaDoc out;
145         
146         MyErrorHandler(PrintWriter JavaDoc out) {
147             this.out = out;
148         }
149         
150         /**
151          * Returns a string describing parse exception details
152          */

153         private String JavaDoc getParseExceptionInfo(SAXParseException spe) {
154             String JavaDoc systemId = spe.getSystemId();
155             if (systemId == null) {
156                 systemId = "null";
157             }
158             String JavaDoc info = "URI=" + systemId + " Line=" + spe.getLineNumber() + ": " + spe.getMessage();
159             return info;
160         }
161         
162         // The following methods are standard SAX ErrorHandler methods.
163
// See SAX documentation for more info.
164

165         public void warning(SAXParseException spe) throws SAXException {
166             out.println("Warning: " + getParseExceptionInfo(spe));
167         }
168         
169         public void error(SAXParseException spe) throws SAXException {
170             String JavaDoc message = "Error: " + getParseExceptionInfo(spe);
171             throw new SAXException(message);
172         }
173         
174         public void fatalError(SAXParseException spe) throws SAXException {
175             String JavaDoc message = "Fatal Error: " + getParseExceptionInfo(spe);
176             throw new SAXException(message);
177         }
178     }
179 }
180
Popular Tags