FileDocCategorySizeDatePackage
DomUtil.javaAPI DocApache Tomcat 6.0.149547Fri Jul 20 04:20:32 BST 2007org.apache.tomcat.util

DomUtil

public class DomUtil extends Object
Few simple utils to read DOM
author
Costin Manolache

Fields Summary
private static org.apache.juli.logging.Log
log
Constructors Summary
Methods Summary
public static org.w3c.dom.NodefindChildWithAtt(org.w3c.dom.Node parent, java.lang.String elemName, java.lang.String attName, java.lang.String attVal)
Find the first direct child with a given attribute.

param
parent
param
elemName name of the element, or null for any
param
attName attribute we're looking for
param
attVal attribute value or null if we just want any

        
        Node child=DomUtil.getChild(parent, Node.ELEMENT_NODE);
        if( attVal== null ) {
            while( child!= null &&
                    ( elemName==null || elemName.equals( child.getNodeName())) && 
                    DomUtil.getAttribute(child, attName) != null ) {
                child=getNext(child, elemName, Node.ELEMENT_NODE );
            }
        } else {
            while( child!= null && 
                    ( elemName==null || elemName.equals( child.getNodeName())) && 
                    ! attVal.equals( DomUtil.getAttribute(child, attName)) ) {
                child=getNext(child, elemName, Node.ELEMENT_NODE );
            }
        }
        return child;        
    
public static java.lang.StringgetAttribute(org.w3c.dom.Node element, java.lang.String attName)

        NamedNodeMap attrs=element.getAttributes();
        if( attrs==null ) return null;
        Node attN=attrs.getNamedItem(attName);
        if( attN==null ) return null;
        return attN.getNodeValue();
    
public static org.w3c.dom.NodegetChild(org.w3c.dom.Node parent, java.lang.String name)
Get the first element child.

param
parent lookup direct childs
param
name name of the element. If null return the first element.

        if( parent==null ) return null;
        Node first=parent.getFirstChild();
        if( first==null ) return null;

        for (Node node = first; node != null;
             node = node.getNextSibling()) {
            //System.out.println("getNode: " + name + " " + node.getNodeName());
            if( node.getNodeType()!=Node.ELEMENT_NODE)
                continue;
            if( name != null &&
                name.equals( node.getNodeName() ) ) {
                return node;
            }
            if( name == null ) {
                return node;
            }
        }
        return null;
    
public static org.w3c.dom.NodegetChild(org.w3c.dom.Node parent, int type)
Get the first direct child with a given type

        Node n=parent.getFirstChild();
        while( n!=null && type != n.getNodeType() ) {
            n=n.getNextSibling();
        }
        if( n==null ) return null;
        return n;
    
public static java.lang.StringgetChildContent(org.w3c.dom.Node parent, java.lang.String name)
Get the first child's content ( ie it's included TEXT node ).

        Node first=parent.getFirstChild();
        if( first==null ) return null;
        for (Node node = first; node != null;
             node = node.getNextSibling()) {
            //System.out.println("getNode: " + name + " " + node.getNodeName());
            if( name.equals( node.getNodeName() ) ) {
                return getContent( node );
            }
        }
        return null;
    
public static java.lang.StringgetContent(org.w3c.dom.Node n)
Get the trimed text content of a node or null if there is no text


    // -------------------- DOM utils --------------------

                        
          
        if( n==null ) return null;
        Node n1=DomUtil.getChild(n, Node.TEXT_NODE);

        if( n1==null ) return null;

        String s1=n1.getNodeValue();
        return s1.trim();
    
public static org.w3c.dom.NodegetNext(org.w3c.dom.Node current)
Get the next sibling with the same name and type

        String name=current.getNodeName();
        int type=current.getNodeType();
        return getNext( current, name, type);
    
public static org.w3c.dom.NodegetNext(org.w3c.dom.Node current, java.lang.String name, int type)
Return the next sibling with a given name and type

        Node first=current.getNextSibling();
        if( first==null ) return null;

        for (Node node = first; node != null;
             node = node.getNextSibling()) {
            
            if( type >= 0 && node.getNodeType() != type ) continue;
            //System.out.println("getNode: " + name + " " + node.getNodeName());
            if( name==null )
                return node;
            if( name.equals( node.getNodeName() ) ) {
                return node;
            }
        }
        return null;
    
public static org.w3c.dom.DocumentreadXml(java.io.InputStream is)
Read XML as DOM.

        DocumentBuilderFactory dbf =
            DocumentBuilderFactory.newInstance();

        dbf.setValidating(false);
        dbf.setIgnoringComments(false);
        dbf.setIgnoringElementContentWhitespace(true);
        //dbf.setCoalescing(true);
        //dbf.setExpandEntityReferences(true);

        DocumentBuilder db = null;
        db = dbf.newDocumentBuilder();
        db.setEntityResolver( new NullResolver() );

        // db.setErrorHandler( new MyErrorHandler());

        Document doc = db.parse(is);
        return doc;
    
public static voidremoveAttribute(org.w3c.dom.Node node, java.lang.String attName)

        NamedNodeMap attributes=node.getAttributes();
        attributes.removeNamedItem(attName);                
    
public static voidsetAttribute(org.w3c.dom.Node node, java.lang.String attName, java.lang.String val)

        NamedNodeMap attributes=node.getAttributes();
        Node attNode=node.getOwnerDocument().createAttribute(attName);
        attNode.setNodeValue( val );
        attributes.setNamedItem(attNode);
    
public static voidsetAttributes(java.lang.Object o, org.w3c.dom.Node parent)

        NamedNodeMap attrs=parent.getAttributes();
        if( attrs==null ) return;

        for (int i=0; i<attrs.getLength(); i++ ) {
            Node n=attrs.item(i);
            String name=n.getNodeName();
            String value=n.getNodeValue();

            if( log.isTraceEnabled() )
                log.trace("Attribute " + parent.getNodeName() + " " +
                            name + "=" + value);
            try {
                IntrospectionUtils.setProperty(o, name, value);
            } catch( Exception ex ) {
                ex.printStackTrace();
            }
        }
    
public static voidsetText(org.w3c.dom.Node node, java.lang.String val)
Set or replace the text value

        Node chld=DomUtil.getChild(node, Node.TEXT_NODE);
        if( chld == null ) {
            Node textN=node.getOwnerDocument().createTextNode(val);
            node.appendChild(textN);
            return;
        }
        // change the value
        chld.setNodeValue(val);           
    
public static voidwriteXml(org.w3c.dom.Node n, java.io.OutputStream os)

        TransformerFactory tf=TransformerFactory.newInstance();
        //identity
        Transformer t=tf.newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.transform(new DOMSource( n ), new StreamResult( os ));