FileDocCategorySizeDatePackage
XmlUtils.javaAPI DocGlassfish v2 API8885Fri May 04 22:34:50 BST 2007com.sun.enterprise.diagnostics.util

XmlUtils

public class XmlUtils extends Object
Collection of helper methods to manipulate XML files.
author
Manisha Umbarje

Fields Summary
Constructors Summary
Methods Summary
public static voidattrSearchReplace(org.w3c.dom.Document doc, java.lang.String srchStr, java.lang.String rplStr)
Search and replace attributes within the specified XmlDocument

param
doc XmlDocument in whicch to perform the search and replace.
param
srchStr String within the document to be replaced.
param
rplStr String to replace srchStr with.

	Node m_root = null;
	Node child = null;
	m_root = doc.getDocumentElement();
	child = m_root.getFirstChild();
	while (child != null) {
	    NamedNodeMap attrs = null;
	    attrs = child.getAttributes();
	    if (attrs != null)
		for (int j=0; j<attrs.getLength(); j++) {
		    Attr attr = (Attr)attrs.item(j);
		    if (attr.getName().toLowerCase().indexOf(srchStr) != -1)
			attr.setValue(rplStr);
		}
	    child = child.getNextSibling();
	}
    
public static voidattrSearchReplace(org.w3c.dom.Node element, java.lang.String srchStr, java.lang.String replaceString)
Search and replace attributes within the specified XmlDocument

param
doc XmlDocument in whicch to perform the search and replace.
param
srchStr String within the document to be replaced.
param
rplStr String to replace srchStr with.

	if (element != null) {
	    NodeList children = element.getChildNodes();
	    int noOfChildren = children.getLength();

	    for (int i = 0; i < noOfChildren; i++) {
		Node child = children.item(i);

		NamedNodeMap attrs = null;
		attrs = child.getAttributes();
		if (attrs != null) {
		    for (int j=0; j<attrs.getLength(); j++) {
			Attr attr = (Attr)attrs.item(j);
			if (attr.getName().toLowerCase().indexOf(srchStr) != -1)
			    attr.setValue(replaceString);
		    }//for
		}//if

		attrSearchReplace(child,srchStr,replaceString);
	    }//for
	}//if (element != null)
    
public static voidcopyXMLFile(org.w3c.dom.Document srcDoc, java.lang.String destFile)
Copy the specified Document to the specified file

param
srcDoc The source XMLDocument
param
destFile Absolute path of the destination file

	// Use a Transformer for output
	TransformerFactory tFactory =
		TransformerFactory.newInstance();
	Transformer transformer = tFactory.newTransformer();

	DOMSource source = new DOMSource(srcDoc);
        File destFileObj = new File(destFile);
        if(!destFileObj.getParentFile().exists())
            destFileObj.getParentFile().mkdirs();
	FileWriter destFileWriter = new FileWriter(destFile);
	StreamResult result = new StreamResult(destFileWriter);
	transformer.transform(source, result);
    
public static voidgetAttributes(org.w3c.dom.Node element, java.lang.String srchStr, java.util.List list)

    
        if(element != null && srchStr != null) {
            if(list == null)
                list = new ArrayList(5);
            NodeList children = element.getChildNodes();
            int noOfChildren = children.getLength();

	    for (int i = 0; i < noOfChildren; i++) {
		Node child = children.item(i);
                String childName = child.getLocalName();
		NamedNodeMap attrs = null;
		attrs = child.getAttributes();
		if (attrs != null) {
		    for (int j=0; j<attrs.getLength(); j++) {
			Attr attr = (Attr)attrs.item(j);
			if (attr.getName().toLowerCase().indexOf(srchStr) != -1) {
                            String attrName =getParentNodeName(child);
                            list.add( attrName +  File.separator + attr);
                        }
		    }//for
		}//if

		getAttributes(child,srchStr,list);
            }
        }
    
private static java.lang.StringgetParentNodeName(org.w3c.dom.Node child)

        if(child != null) {
            String name = child.getNodeName() ;
            //String value = child.getNodeValue();
            Node parent = child.getParentNode();
            if(parent != null &&  (parent.getNodeType() == Node.ELEMENT_NODE))
                name = getParentNodeName(parent) + File.separator+ name ;
            NamedNodeMap attrs =  child.getAttributes();
            if (attrs != null) {
                String nameAttribute = null;
                for (int j=0; j<attrs.getLength(); j++) {
                    Attr attr = (Attr)attrs.item(j);
                    if(attr.getName().toLowerCase().indexOf("name") != -1) {
                        nameAttribute = attr.getValue();
                        continue;
                    }    
                }
                if (nameAttribute != null)
                    name = name + "=" + nameAttribute ;
            }
            return name;
        }
        return "";
    
public static org.w3c.dom.DocumentloadXML(java.lang.String srcXmlFile, java.lang.String dtdFileName)
Load an XML file from disk

param
srcXmlFile Absolute path of the XML file to be loaded
return
XmlDocument representation of the source XML file

	Document doc = null;
	FileInputStream in = null;
      
	in = new FileInputStream(srcXmlFile);
        DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().
                newDocumentBuilder();
        docBuilder.setEntityResolver(new NOOPHandler(dtdFileName));
	doc = docBuilder.parse( in );
	if(in!=null)
	    in.close();
	return doc;