FileDocCategorySizeDatePackage
DOMUtil.javaAPI DocApache Xerces 3.0.132780Fri Sep 14 20:33:54 BST 2007org.apache.xerces.util

DOMUtil

public class DOMUtil extends Object
Some useful utility methods. This class was modified in Xerces2 with a view to abstracting as much as possible away from the representation of the underlying parsed structure (i.e., the DOM). This was done so that, if Xerces ever adopts an in-memory representation more efficient than the DOM (such as a DTM), we should easily be able to convert our schema parsing to utilize it.
version
$Id: DOMUtil.java 447948 2006-09-19 20:20:53Z mrglavas $

Fields Summary
Constructors Summary
protected DOMUtil()
This class cannot be instantiated.

Methods Summary
public static voidcopyInto(org.w3c.dom.Node src, org.w3c.dom.Node dest)
Copies the source tree into the specified place in a destination tree. The source node and its children are appended as children of the destination node.

Note: This is an iterative implementation.

        
        // get node factory
        Document factory = dest.getOwnerDocument();
        boolean domimpl = factory instanceof DocumentImpl;
        
        // placement variables
        Node start  = src;
        Node parent = src;
        Node place  = src;
        
        // traverse source tree
        while (place != null) {
            
            // copy this node
            Node node = null;
            int  type = place.getNodeType();
            switch (type) {
            case Node.CDATA_SECTION_NODE: {
                node = factory.createCDATASection(place.getNodeValue());
                break;
            }
            case Node.COMMENT_NODE: {
                node = factory.createComment(place.getNodeValue());
                break;
            }
            case Node.ELEMENT_NODE: {
                Element element = factory.createElement(place.getNodeName());
                node = element;
                NamedNodeMap attrs  = place.getAttributes();
                int attrCount = attrs.getLength();
                for (int i = 0; i < attrCount; i++) {
                    Attr attr = (Attr)attrs.item(i);
                    String attrName = attr.getNodeName();
                    String attrValue = attr.getNodeValue();
                    element.setAttribute(attrName, attrValue);
                    if (domimpl && !attr.getSpecified()) {
                        ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                    }
                }
                break;
            }
            case Node.ENTITY_REFERENCE_NODE: {
                node = factory.createEntityReference(place.getNodeName());
                break;
            }
            case Node.PROCESSING_INSTRUCTION_NODE: {
                node = factory.createProcessingInstruction(place.getNodeName(),
                        place.getNodeValue());
                break;
            }
            case Node.TEXT_NODE: {
                node = factory.createTextNode(place.getNodeValue());
                break;
            }
            default: {
                throw new IllegalArgumentException("can't copy node type, "+
                        type+" ("+
                        node.getNodeName()+')");
            }
            }
            dest.appendChild(node);
            
            // iterate over children
            if (place.hasChildNodes()) {
                parent = place;
                place  = place.getFirstChild();
                dest   = node;
            }
            
            // advance
            else {
                place = place.getNextSibling();
                while (place == null && parent != start) {
                    place  = parent.getNextSibling();
                    parent = parent.getParentNode();
                    dest   = dest.getParentNode();
                }
            }
            
        }
        
    
public static org.w3c.dom.DOMExceptioncreateDOMException(short code, java.lang.Throwable cause)
Creates a DOMException. On J2SE 1.4 and above the cause for the exception will be set.

        DOMException de = new DOMException(code, cause != null ? cause.getMessage() : null);
        if (cause != null && ThrowableMethods.fgThrowableMethodsAvailable) {
            try {
                ThrowableMethods.fgThrowableInitCauseMethod.invoke(de, new Object [] {cause});
            }
            // Something went wrong. There's not much we can do about it.
            catch (Exception e) {}
        }
        return de;
    
public static org.w3c.dom.ls.LSExceptioncreateLSException(short code, java.lang.Throwable cause)
Creates an LSException. On J2SE 1.4 and above the cause for the exception will be set.

        LSException lse = new LSException(code, cause != null ? cause.getMessage() : null);
        if (cause != null && ThrowableMethods.fgThrowableMethodsAvailable) {
            try {
                ThrowableMethods.fgThrowableInitCauseMethod.invoke(lse, new Object [] {cause});
            }
            // Something went wrong. There's not much we can do about it.
            catch (Exception e) {}
        }
        return lse;
    
public static java.lang.StringgetAnnotation(org.w3c.dom.Node node)

        if (node instanceof ElementImpl) {
            return ((ElementImpl)node).getAnnotation();
        }
        return null;
    
public static org.w3c.dom.AttrgetAttr(org.w3c.dom.Element elem, java.lang.String name)

        return elem.getAttributeNode(name);
    
public static org.w3c.dom.AttrgetAttrNS(org.w3c.dom.Element elem, java.lang.String nsUri, java.lang.String localName)

        return elem.getAttributeNodeNS(nsUri, localName);
    
public static java.lang.StringgetAttrValue(org.w3c.dom.Element elem, java.lang.String name)

        return elem.getAttribute(name);
    
public static java.lang.StringgetAttrValueNS(org.w3c.dom.Element elem, java.lang.String nsUri, java.lang.String localName)

        return elem.getAttributeNS(nsUri, localName);
    
public static org.w3c.dom.Attr[]getAttrs(org.w3c.dom.Element elem)

        NamedNodeMap attrMap = elem.getAttributes();
        Attr [] attrArray = new Attr[attrMap.getLength()];
        for (int i=0; i<attrMap.getLength(); i++)
            attrArray[i] = (Attr)attrMap.item(i);
        return attrArray;
    
public static java.lang.StringgetChildText(org.w3c.dom.Node node)
Returns the concatenated child text of the specified node. This method only looks at the immediate children of type Node.TEXT_NODE or the children of any child node that is of type Node.CDATA_SECTION_NODE for the concatenation.

param
node The node to look at.

        
        // is there anything to do?
        if (node == null) {
            return null;
        }
        
        // concatenate children text
        StringBuffer str = new StringBuffer();
        Node child = node.getFirstChild();
        while (child != null) {
            short type = child.getNodeType();
            if (type == Node.TEXT_NODE) {
                str.append(child.getNodeValue());
            }
            else if (type == Node.CDATA_SECTION_NODE) {
                str.append(getChildText(child));
            }
            child = child.getNextSibling();
        }
        
        // return text value
        return str.toString();
        
    
public static org.w3c.dom.DocumentgetDocument(org.w3c.dom.Node node)

        return node.getOwnerDocument();
    
public static org.w3c.dom.ElementgetFirstChildElement(org.w3c.dom.Node parent, java.lang.String elemName)
Finds and returns the first child node with the given name.

        
        // search for node
        Node child = parent.getFirstChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                if (child.getNodeName().equals(elemName)) {
                    return (Element)child;
                }
            }
            child = child.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetFirstChildElement(org.w3c.dom.Node parent, java.lang.String[] elemNames)
Finds and returns the first child node with the given name.

        
        // search for node
        Node child = parent.getFirstChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                for (int i = 0; i < elemNames.length; i++) {
                    if (child.getNodeName().equals(elemNames[i])) {
                        return (Element)child;
                    }
                }
            }
            child = child.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetFirstChildElement(org.w3c.dom.Node parent)
Finds and returns the first child element node.

        
        // search for node
        Node child = parent.getFirstChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                return (Element)child;
            }
            child = child.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetFirstChildElement(org.w3c.dom.Node parent, java.lang.String elemName, java.lang.String attrName, java.lang.String attrValue)
Finds and returns the first child node with the given name and attribute name, value pair.

        
        // search for node
        Node child = parent.getFirstChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element)child;
                if (element.getNodeName().equals(elemName) &&
                        element.getAttribute(attrName).equals(attrValue)) {
                    return element;
                }
            }
            child = child.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetFirstChildElementNS(org.w3c.dom.Node parent, java.lang.String uri, java.lang.String localpart)
Finds and returns the first child node with the given qualified name.

        
        // search for node
        Node child = parent.getFirstChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                String childURI = child.getNamespaceURI();
                if (childURI != null && childURI.equals(uri) &&
                        child.getLocalName().equals(localpart)) {
                    return (Element)child;
                }
            }
            child = child.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetFirstChildElementNS(org.w3c.dom.Node parent, java.lang.String[][] elemNames)
Finds and returns the first child node with the given qualified name.

        
        // search for node
        Node child = parent.getFirstChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                for (int i = 0; i < elemNames.length; i++) {
                    String uri = child.getNamespaceURI();
                    if (uri != null && uri.equals(elemNames[i][0]) &&
                            child.getLocalName().equals(elemNames[i][1])) {
                        return (Element)child;
                    }
                }
            }
            child = child.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetFirstVisibleChildElement(org.w3c.dom.Node parent)
Finds and returns the first visible child element node.

        
        // search for node
        Node child = parent.getFirstChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE &&
                    !isHidden(child)) {
                return (Element)child;
            }
            child = child.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetFirstVisibleChildElement(org.w3c.dom.Node parent, java.util.Hashtable hiddenNodes)
Finds and returns the first visible child element node.

        
        // search for node
        Node child = parent.getFirstChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE &&
                    !isHidden(child, hiddenNodes)) {
                return (Element)child;
            }
            child = child.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetLastChildElement(org.w3c.dom.Node parent, java.lang.String elemName)
Finds and returns the last child node with the given name.

        
        // search for node
        Node child = parent.getLastChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                if (child.getNodeName().equals(elemName)) {
                    return (Element)child;
                }
            }
            child = child.getPreviousSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetLastChildElement(org.w3c.dom.Node parent, java.lang.String[] elemNames)
Finds and returns the last child node with the given name.

        
        // search for node
        Node child = parent.getLastChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                for (int i = 0; i < elemNames.length; i++) {
                    if (child.getNodeName().equals(elemNames[i])) {
                        return (Element)child;
                    }
                }
            }
            child = child.getPreviousSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetLastChildElement(org.w3c.dom.Node parent, java.lang.String elemName, java.lang.String attrName, java.lang.String attrValue)
Finds and returns the last child node with the given name and attribute name, value pair.

        
        // search for node
        Node child = parent.getLastChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element)child;
                if (element.getNodeName().equals(elemName) &&
                        element.getAttribute(attrName).equals(attrValue)) {
                    return element;
                }
            }
            child = child.getPreviousSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetLastChildElement(org.w3c.dom.Node parent)
Finds and returns the last child element node. Overload previous method for non-Xerces node impl.

        
        // search for node
        Node child = parent.getLastChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                return (Element)child;
            }
            child = child.getPreviousSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetLastChildElementNS(org.w3c.dom.Node parent, java.lang.String uri, java.lang.String localpart)
Finds and returns the last child node with the given qualified name.

        
        // search for node
        Node child = parent.getLastChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                String childURI = child.getNamespaceURI();
                if (childURI != null && childURI.equals(uri) &&
                        child.getLocalName().equals(localpart)) {
                    return (Element)child;
                }
            }
            child = child.getPreviousSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetLastChildElementNS(org.w3c.dom.Node parent, java.lang.String[][] elemNames)
Finds and returns the last child node with the given qualified name.

        
        // search for node
        Node child = parent.getLastChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                for (int i = 0; i < elemNames.length; i++) {
                    String uri = child.getNamespaceURI();
                    if (uri != null && uri.equals(elemNames[i][0]) &&
                            child.getLocalName().equals(elemNames[i][1])) {
                        return (Element)child;
                    }
                }
            }
            child = child.getPreviousSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetLastVisibleChildElement(org.w3c.dom.Node parent)
Finds and returns the last visible child element node.

        
        // search for node
        Node child = parent.getLastChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE &&
                    !isHidden(child)) {
                return (Element)child;
            }
            child = child.getPreviousSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetLastVisibleChildElement(org.w3c.dom.Node parent, java.util.Hashtable hiddenNodes)
Finds and returns the last visible child element node. Overload previous method for non-Xerces node impl

        
        // search for node
        Node child = parent.getLastChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE &&
                    !isHidden(child, hiddenNodes)) {
                return (Element)child;
            }
            child = child.getPreviousSibling();
        }
        
        // not found
        return null;
        
    
public static java.lang.StringgetLocalName(org.w3c.dom.Node node)
returns local name of this element if not null, otherwise returns the name of the node

        String name = node.getLocalName();
        return (name!=null)? name:node.getNodeName();
    
public static java.lang.StringgetName(org.w3c.dom.Node node)

        return node.getNodeName();
    
public static java.lang.StringgetNamespaceURI(org.w3c.dom.Node node)

        return node.getNamespaceURI();
    
public static org.w3c.dom.ElementgetNextSiblingElement(org.w3c.dom.Node node, java.lang.String elemName)
Finds and returns the next sibling node with the given name.

        
        // search for node
        Node sibling = node.getNextSibling();
        while (sibling != null) {
            if (sibling.getNodeType() == Node.ELEMENT_NODE) {
                if (sibling.getNodeName().equals(elemName)) {
                    return (Element)sibling;
                }
            }
            sibling = sibling.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetNextSiblingElement(org.w3c.dom.Node node, java.lang.String[] elemNames)
Finds and returns the next sibling node with the given name.

        
        // search for node
        Node sibling = node.getNextSibling();
        while (sibling != null) {
            if (sibling.getNodeType() == Node.ELEMENT_NODE) {
                for (int i = 0; i < elemNames.length; i++) {
                    if (sibling.getNodeName().equals(elemNames[i])) {
                        return (Element)sibling;
                    }
                }
            }
            sibling = sibling.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetNextSiblingElement(org.w3c.dom.Node node, java.lang.String elemName, java.lang.String attrName, java.lang.String attrValue)
Finds and returns the next sibling node with the given name and attribute name, value pair. Since only elements have attributes, the node returned will be of type Node.ELEMENT_NODE.

        
        // search for node
        Node sibling = node.getNextSibling();
        while (sibling != null) {
            if (sibling.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element)sibling;
                if (element.getNodeName().equals(elemName) &&
                        element.getAttribute(attrName).equals(attrValue)) {
                    return element;
                }
            }
            sibling = sibling.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetNextSiblingElement(org.w3c.dom.Node node)
Finds and returns the next sibling element node.

        
        // search for node
        Node sibling = node.getNextSibling();
        while (sibling != null) {
            if (sibling.getNodeType() == Node.ELEMENT_NODE) {
                return (Element)sibling;
            }
            sibling = sibling.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetNextSiblingElementNS(org.w3c.dom.Node node, java.lang.String uri, java.lang.String localpart)
Finds and returns the next sibling node with the given qualified name.

        
        // search for node
        Node sibling = node.getNextSibling();
        while (sibling != null) {
            if (sibling.getNodeType() == Node.ELEMENT_NODE) {
                String siblingURI = sibling.getNamespaceURI();
                if (siblingURI != null && siblingURI.equals(uri) &&
                        sibling.getLocalName().equals(localpart)) {
                    return (Element)sibling;
                }
            }
            sibling = sibling.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetNextSiblingElementNS(org.w3c.dom.Node node, java.lang.String[][] elemNames)
Finds and returns the next sibling node with the given qualified name.

        
        // search for node
        Node sibling = node.getNextSibling();
        while (sibling != null) {
            if (sibling.getNodeType() == Node.ELEMENT_NODE) {
                for (int i = 0; i < elemNames.length; i++) {
                    String uri = sibling.getNamespaceURI();
                    if (uri != null && uri.equals(elemNames[i][0]) &&
                            sibling.getLocalName().equals(elemNames[i][1])) {
                        return (Element)sibling;
                    }
                }
            }
            sibling = sibling.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetNextVisibleSiblingElement(org.w3c.dom.Node node)

        
        // search for node
        Node sibling = node.getNextSibling();
        while (sibling != null) {
            if (sibling.getNodeType() == Node.ELEMENT_NODE &&
                    !isHidden(sibling)) {
                return (Element)sibling;
            }
            sibling = sibling.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetNextVisibleSiblingElement(org.w3c.dom.Node node, java.util.Hashtable hiddenNodes)

        
        // search for node
        Node sibling = node.getNextSibling();
        while (sibling != null) {
            if (sibling.getNodeType() == Node.ELEMENT_NODE &&
                    !isHidden(sibling, hiddenNodes)) {
                return (Element)sibling;
            }
            sibling = sibling.getNextSibling();
        }
        
        // not found
        return null;
        
    
public static org.w3c.dom.ElementgetParent(org.w3c.dom.Element elem)

        Node parent = elem.getParentNode();
        if (parent instanceof Element)
            return (Element)parent;
        return null;
    
public static java.lang.StringgetPrefix(org.w3c.dom.Node node)

        return node.getPrefix();
    
public static org.w3c.dom.ElementgetRoot(org.w3c.dom.Document doc)

        return doc.getDocumentElement();
    
public static java.lang.StringgetSyntheticAnnotation(org.w3c.dom.Node node)

        if (node instanceof ElementImpl) {
            return ((ElementImpl)node).getSyntheticAnnotation();
        }
        return null;
    
public static java.lang.StringgetValue(org.w3c.dom.Attr attribute)

        return attribute.getValue();
    
public static booleanisHidden(org.w3c.dom.Node node)

        if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl)
            return ((org.apache.xerces.impl.xs.opti.NodeImpl)node).getReadOnly();
        else if (node instanceof org.apache.xerces.dom.NodeImpl)
            return ((org.apache.xerces.dom.NodeImpl)node).getReadOnly();
        return false;
    
public static booleanisHidden(org.w3c.dom.Node node, java.util.Hashtable hiddenNodes)

        if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl) {
            return ((org.apache.xerces.impl.xs.opti.NodeImpl)node).getReadOnly();
        }
        else {
            return hiddenNodes.containsKey(node);
        }
    
public static voidsetHidden(org.w3c.dom.Node node)

        if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl)
            ((org.apache.xerces.impl.xs.opti.NodeImpl)node).setReadOnly(true, false);
        else if (node instanceof org.apache.xerces.dom.NodeImpl)
            ((org.apache.xerces.dom.NodeImpl)node).setReadOnly(true, false);
    
public static voidsetHidden(org.w3c.dom.Node node, java.util.Hashtable hiddenNodes)

        if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl) {
            ((org.apache.xerces.impl.xs.opti.NodeImpl)node).setReadOnly(true, false);
        }
        else {
        	hiddenNodes.put(node, "");
        }
    
public static voidsetVisible(org.w3c.dom.Node node)

        if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl)
            ((org.apache.xerces.impl.xs.opti.NodeImpl)node).setReadOnly(false, false);
        else if (node instanceof org.apache.xerces.dom.NodeImpl)
            ((org.apache.xerces.dom.NodeImpl)node).setReadOnly(false, false);
    
public static voidsetVisible(org.w3c.dom.Node node, java.util.Hashtable hiddenNodes)

        if (node instanceof org.apache.xerces.impl.xs.opti.NodeImpl) {
            ((org.apache.xerces.impl.xs.opti.NodeImpl)node).setReadOnly(false, false);   
        }
        else {
            hiddenNodes.remove(node);
        }