Example usage for org.w3c.dom DocumentFragment insertBefore

List of usage examples for org.w3c.dom DocumentFragment insertBefore

Introduction

In this page you can find the example usage for org.w3c.dom DocumentFragment insertBefore.

Prototype

public Node insertBefore(Node newChild, Node refChild) throws DOMException;

Source Link

Document

Inserts the node newChild before the existing child node refChild.

Usage

From source file:com.netspective.sparx.util.xml.XmlSource.java

public void inheritElement(Element srcElement, Element destElem, Set excludeElems, String inheritedFromNode) {
    NamedNodeMap inhAttrs = srcElement.getAttributes();
    for (int i = 0; i < inhAttrs.getLength(); i++) {
        Node attrNode = inhAttrs.item(i);
        final String nodeName = attrNode.getNodeName();
        if (!excludeElems.contains(nodeName) && destElem.getAttribute(nodeName).equals(""))
            destElem.setAttribute(nodeName, attrNode.getNodeValue());
    }/*from  w ww.  j a  va2s .  co m*/

    DocumentFragment inheritFragment = xmlDoc.createDocumentFragment();
    NodeList inhChildren = srcElement.getChildNodes();
    for (int i = inhChildren.getLength() - 1; i >= 0; i--) {
        Node childNode = inhChildren.item(i);

        // only add if there isn't an attribute overriding this element
        final String nodeName = childNode.getNodeName();
        if (destElem.getAttribute(nodeName).length() == 0 && (!excludeElems.contains(nodeName))) {
            Node cloned = childNode.cloneNode(true);
            if (inheritedFromNode != null && cloned.getNodeType() == Node.ELEMENT_NODE)
                ((Element) cloned).setAttribute("_inherited-from", inheritedFromNode);
            inheritFragment.insertBefore(cloned, inheritFragment.getFirstChild());
        }
    }

    destElem.insertBefore(inheritFragment, destElem.getFirstChild());
}