1 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 ; 30 31 import javax.xml.parsers.*; 32 import javax.xml.transform.*; 33 import javax.xml.transform.dom.DOMSource ; 34 import javax.xml.transform.stream.StreamResult ; 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 ; 41 import java.io.OutputStreamWriter ; 42 import java.io.PrintWriter ; 43 import java.util.HashMap ; 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 OUTPUT_ENCODING = "UTF-8"; 50 private Document doc; 51 private static HashMap nodeClasses = new HashMap (); 52 53 public TreeReader(InputStream is, String dtdURLBase) throws Exception { 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 dbf.setExpandEntityReferences(true); 62 63 DocumentBuilder db = dbf.newDocumentBuilder(); 64 db.setEntityResolver(new ViewXMLEntityResolver()); 65 66 OutputStreamWriter errorWriter = new OutputStreamWriter (System.err, OUTPUT_ENCODING); 67 db.setErrorHandler(new MyErrorHandler(new PrintWriter (errorWriter, true))); 68 doc = db.parse(is, dtdURLBase); 69 } 70 71 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 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 name = treeNodeType.getAttribute("name"); 96 String clazz = treeNodeType.getAttribute("treeNodeClass"); 97 nodeClasses.put(name, clazz); 98 } 99 100 public static String getClass(String nodeClassType) { 101 return (String ) nodeClasses.get(nodeClassType); 102 } 103 104 private static IndexTreeNode addToTree(Element node, IndexTreeModel treeModel, IndexTreeNode parent) { 105 String name = node.getAttribute("name"); if (name.startsWith("$")) { 107 int i = name.indexOf('#'); 108 if (i > 0) { 109 String mbeanName = name.substring(1,i); 110 String attrName = name.substring(i+1, name.length()); 111 name = (String )MBeanUtil.getAttribute(mbeanName, attrName); 112 } 113 } 117 String 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 ("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 { 133 for (Node child = doc.getFirstChild(); child != null; 134 child = child.getNextSibling()) { 135 String nodeName = child.getNodeName(); 136 if (nodeName.equalsIgnoreCase("Tree")) 137 process(child, treeModel, null); 138 } 139 } 140 141 private static class MyErrorHandler implements ErrorHandler { 143 144 private PrintWriter out; 145 146 MyErrorHandler(PrintWriter out) { 147 this.out = out; 148 } 149 150 153 private String getParseExceptionInfo(SAXParseException spe) { 154 String systemId = spe.getSystemId(); 155 if (systemId == null) { 156 systemId = "null"; 157 } 158 String info = "URI=" + systemId + " Line=" + spe.getLineNumber() + ": " + spe.getMessage(); 159 return info; 160 } 161 162 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 message = "Error: " + getParseExceptionInfo(spe); 171 throw new SAXException(message); 172 } 173 174 public void fatalError(SAXParseException spe) throws SAXException { 175 String message = "Fatal Error: " + getParseExceptionInfo(spe); 176 throw new SAXException(message); 177 } 178 } 179 } 180 | Popular Tags |