1 23 package com.sun.enterprise.diagnostics.util; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileWriter ; 28 import java.io.IOException ; 29 import java.util.List ; 30 import java.util.ArrayList ; 31 32 import javax.xml.parsers.DocumentBuilderFactory ; 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.ParserConfigurationException ; 35 import javax.xml.transform.Transformer ; 36 import javax.xml.transform.TransformerFactory ; 37 import javax.xml.transform.TransformerException ; 38 import javax.xml.transform.TransformerConfigurationException ; 39 import javax.xml.transform.dom.DOMSource ; 40 import javax.xml.transform.stream.StreamResult ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 import org.w3c.dom.*; 45 import org.xml.sax.SAXException ; 46 import org.xml.sax.helpers.DefaultHandler ; 47 import org.xml.sax.InputSource ; 48 49 import com.sun.logging.LogDomains; 50 51 55 public class XmlUtils { 56 57 64 public static Document loadXML(String srcXmlFile, String dtdFileName) 65 throws SAXException , IOException , ParserConfigurationException { 66 Document doc = null; 67 FileInputStream in = null; 68 69 in = new FileInputStream (srcXmlFile); 70 DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance(). 71 newDocumentBuilder(); 72 docBuilder.setEntityResolver(new NOOPHandler(dtdFileName)); 73 doc = docBuilder.parse( in ); 74 if(in!=null) 75 in.close(); 76 return doc; 77 } 78 79 public static void getAttributes(Node element, String srchStr, 80 List list) { 81 82 if(element != null && srchStr != null) { 83 if(list == null) 84 list = new ArrayList (5); 85 NodeList children = element.getChildNodes(); 86 int noOfChildren = children.getLength(); 87 88 for (int i = 0; i < noOfChildren; i++) { 89 Node child = children.item(i); 90 String childName = child.getLocalName(); 91 NamedNodeMap attrs = null; 92 attrs = child.getAttributes(); 93 if (attrs != null) { 94 for (int j=0; j<attrs.getLength(); j++) { 95 Attr attr = (Attr)attrs.item(j); 96 if (attr.getName().toLowerCase().indexOf(srchStr) != -1) { 97 String attrName =getParentNodeName(child); 98 list.add( attrName + File.separator + attr); 99 } 100 } } 103 getAttributes(child,srchStr,list); 104 } 105 } 106 } 107 108 private static String getParentNodeName(Node child) { 109 if(child != null) { 110 String name = child.getNodeName() ; 111 Node parent = child.getParentNode(); 113 if(parent != null && (parent.getNodeType() == Node.ELEMENT_NODE)) 114 name = getParentNodeName(parent) + File.separator+ name ; 115 NamedNodeMap attrs = child.getAttributes(); 116 if (attrs != null) { 117 String nameAttribute = null; 118 for (int j=0; j<attrs.getLength(); j++) { 119 Attr attr = (Attr)attrs.item(j); 120 if(attr.getName().toLowerCase().indexOf("name") != -1) { 121 nameAttribute = attr.getValue(); 122 continue; 123 } 124 } 125 if (nameAttribute != null) 126 name = name + "=" + nameAttribute ; 127 } 128 return name; 129 } 130 return ""; 131 } 132 140 public static void attrSearchReplace(Document doc, String srchStr, 141 String rplStr){ 142 Node m_root = null; 143 Node child = null; 144 m_root = doc.getDocumentElement(); 145 child = m_root.getFirstChild(); 146 while (child != null) { 147 NamedNodeMap attrs = null; 148 attrs = child.getAttributes(); 149 if (attrs != null) 150 for (int j=0; j<attrs.getLength(); j++) { 151 Attr attr = (Attr)attrs.item(j); 152 if (attr.getName().toLowerCase().indexOf(srchStr) != -1) 153 attr.setValue(rplStr); 154 } 155 child = child.getNextSibling(); 156 } 157 } 158 159 160 168 public static void attrSearchReplace(Node element, String srchStr, 169 String replaceString) { 170 if (element != null) { 171 NodeList children = element.getChildNodes(); 172 int noOfChildren = children.getLength(); 173 174 for (int i = 0; i < noOfChildren; i++) { 175 Node child = children.item(i); 176 177 NamedNodeMap attrs = null; 178 attrs = child.getAttributes(); 179 if (attrs != null) { 180 for (int j=0; j<attrs.getLength(); j++) { 181 Attr attr = (Attr)attrs.item(j); 182 if (attr.getName().toLowerCase().indexOf(srchStr) != -1) 183 attr.setValue(replaceString); 184 } } 187 attrSearchReplace(child,srchStr,replaceString); 188 } } } 191 192 193 200 public static void copyXMLFile(Document srcDoc, String destFile) 201 throws IOException , TransformerConfigurationException , TransformerException { 202 TransformerFactory tFactory = 204 TransformerFactory.newInstance(); 205 Transformer transformer = tFactory.newTransformer(); 206 207 DOMSource source = new DOMSource (srcDoc); 208 File destFileObj = new File (destFile); 209 if(!destFileObj.getParentFile().exists()) 210 destFileObj.getParentFile().mkdirs(); 211 FileWriter destFileWriter = new FileWriter (destFile); 212 StreamResult result = new StreamResult (destFileWriter); 213 transformer.transform(source, result); 214 } 215 } 216 217 class NOOPHandler extends DefaultHandler { 218 219 static String _dtdFileName; 220 private static Logger logger = 221 LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 222 NOOPHandler(String dtdFileName) { 223 super(); 224 _dtdFileName = dtdFileName; 225 } 228 229 public InputSource resolveEntity(String publicId, 230 String systemId) throws SAXException 231 { 232 InputSource is = null; 233 try { 234 is = new InputSource (new FileInputStream (_dtdFileName)); 235 } catch(Exception e) { 236 throw new SAXException ("cannot resolve dtd", e); 237 } 238 return is; 239 } 240 241 } 242 243 244 | Popular Tags |