1 23 package org.objectweb.joram.client.connector.utils; 24 25 import java.io.*; 26 import java.util.*; 27 28 import javax.xml.transform.*; 29 import javax.xml.transform.dom.DOMSource ; 30 import javax.xml.transform.stream.StreamResult ; 31 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 35 import org.xml.sax.Attributes ; 36 import org.w3c.dom.*; 37 38 39 42 public class Wrapper { 43 private boolean debug = false; 44 private Map map = null; 45 46 public Wrapper(boolean debug) { 47 this.debug = debug; 48 } 49 50 55 public String parse(InputStream in) throws Exception { 56 StringBuffer buff = new StringBuffer (); 57 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 58 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 59 Document doc = docBuilder.parse(in); 60 61 NodeList nl = doc.getElementsByTagName("resourceadapter"); 62 for (int i = 0; i < nl.getLength(); i++) { 63 Node node = nl.item(i); 64 buff.append(explore(node)); 65 } 66 return buff.toString(); 67 } 68 69 public String explore(Node node) throws Exception { 70 73 StringBuffer buff = new StringBuffer (); 74 NodeList nl = node.getChildNodes(); 75 for (int i = 0; i < nl.getLength(); i++) { 76 Node n = nl.item(i); 77 if (n.getNodeName().equals("config-property")) { 78 buff.append(extractPropertyInfo(n.getChildNodes())); 79 } else { 80 if (n.getNodeName().equals("resourceadapter-class") 81 || n.getNodeName().equals("managedconnectionfactory-class")) { 82 String nodeName = n.getFirstChild().getNodeValue(); 83 buff.append("["); 84 buff.append(nodeName); 85 buff.append("]"); 86 buff.append("\n"); 87 } 88 buff.append(explore(n)); 89 } 90 } 91 return buff.toString(); 92 } 93 94 public String extractPropertyInfo(NodeList nodeList) throws Exception { 95 String name = null; 96 String value = null; 97 for (int i = 0; i < nodeList.getLength(); i++) { 98 Node n = nodeList.item(i); 99 if (n.getNodeName().equals("config-property-name")) { 100 name = n.getFirstChild().getNodeValue(); 101 } else if (n.getNodeName().equals("config-property-value")) { 102 value = n.getFirstChild().getNodeValue(); 103 } 104 } 105 106 StringBuffer buff = new StringBuffer (); 107 buff.append(" "); 108 buff.append(name); 109 buff.append("\t"); 110 if (value != null) 111 buff.append(value); 112 buff.append("\n"); 113 return buff.toString(); 114 } 115 116 117 123 public String update(InputStream in, Map map) throws Exception { 124 this.map = map; 125 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 126 DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 127 Document doc = docBuilder.parse(in); 128 129 NodeList nl = doc.getElementsByTagName("resourceadapter"); 130 for (int i = 0; i < nl.getLength(); i++) { 131 Node node = nl.item(i); 132 browse(node); 133 } 134 135 Transformer transformer = 136 TransformerFactory.newInstance().newTransformer(); 137 Source source = new DOMSource (doc); 138 139 CharArrayWriter writer = new CharArrayWriter(); 140 Result output = new StreamResult (writer); 141 transformer.transform(source, output); 142 writer.flush(); 143 144 return writer.toString(); 145 } 146 147 public void browse(Node node) throws Exception { 148 if (debug) 149 System.out.println("Wrapper.browse(" + node + ")"); 150 NodeList nl = node.getChildNodes(); 151 for (int i = 0; i < nl.getLength(); i++) { 152 Node n = nl.item(i); 153 String nodeValue = n.getNodeValue(); 154 if (nodeValue != null && map.containsKey(nodeValue)) { 155 if (debug) 156 System.out.println("NodeName=" + nodeValue); 157 explore(n.getParentNode().getParentNode(),nodeValue); 158 } else if (nodeValue != null 159 && !nodeValue.equals("config-property")) { 160 continue; 161 } else { 162 browse(n); 163 } 164 } 165 } 166 167 public void explore(Node node, String key) throws Exception { 168 if (debug) 169 System.out.println(">>>> Wrapper.explore(" + node + "," + key + ")"); 170 NodeList nl = node.getChildNodes(); 171 for (int i = 0; i < nl.getLength(); i++) { 172 Node n = nl.item(i); 173 String nodeName = n.getNodeName(); 174 if (nodeName.equals("config-property")) { 175 setPropertiesValue(n.getChildNodes(),key); 176 } else if (nodeName.equals("outbound-resourceadapter") 177 || nodeName.equals("inbound-resourceadapter")) { 178 continue; 179 } else { 180 explore(n,key); 181 } 182 } 183 if (debug) 184 System.out.println("<<<<<< Wrapper.explore node = " + node); 185 } 186 187 188 public void setPropertiesValue(NodeList nodeList, String key) throws Exception { 189 if (debug) 190 System.out.println("Wrapper.setPropertiesValue(" + nodeList + "," + key + ")"); 191 String name = null; 192 String value = null; 193 Map prop = (Map) map.get(key); 194 for (int i = 0; i < nodeList.getLength(); i++) { 195 Node n = nodeList.item(i); 196 if (n.getNodeName().equals("config-property-name")) { 197 name = n.getFirstChild().getNodeValue(); 198 } else if (n.getNodeName().equals("config-property-value") 199 && prop.containsKey(name)) { 200 n.getFirstChild().setNodeValue((String ) prop.get(name)); 201 } 202 } 203 if (debug) 204 System.out.println("Wrapper.setPropertiesValue name=" + name + ", value=" + value); 205 } 206 } 207 | Popular Tags |