Example usage for org.w3c.dom DocumentFragment getFirstChild

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

Introduction

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

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:org.structr.web.entity.dom.DOMNode.java

@Override
public Node replaceChild(final Node newChild, final Node oldChild) throws DOMException {

    checkWriteAccess();//from  w w w. ja  v  a  2  s  .co m

    checkSameDocument(newChild);
    checkSameDocument(oldChild);

    checkHierarchy(newChild);
    checkHierarchy(oldChild);

    if (newChild instanceof DocumentFragment) {

        // When inserting document fragments, we must take
        // care of the special case that the nodes already
        // have a NEXT_LIST_ENTRY relationship coming from
        // the document fragment, so we must first remove
        // the node from the document fragment and then
        // add it to the new parent.
        // replace indirectly using insertBefore and remove
        final DocumentFragment fragment = (DocumentFragment) newChild;
        Node currentChild = fragment.getFirstChild();

        while (currentChild != null) {

            // save next child in fragment list for later use
            final Node savedNextChild = currentChild.getNextSibling();

            // remove child from document fragment
            fragment.removeChild(currentChild);

            // add child to new parent
            insertBefore(currentChild, oldChild);

            // next
            currentChild = savedNextChild;
        }

        // finally, remove reference element
        removeChild(oldChild);

    } else {

        Node _parent = newChild.getParentNode();
        if (_parent != null && _parent instanceof DOMNode) {

            _parent.removeChild(newChild);
        }

        try {
            // replace directly
            treeReplaceChild((DOMNode) newChild, (DOMNode) oldChild);

        } catch (FrameworkException frex) {

            if (frex.getStatus() == 404) {

                throw new DOMException(DOMException.NOT_FOUND_ERR, frex.getMessage());

            } else {

                throw new DOMException(DOMException.INVALID_STATE_ERR, frex.getMessage());
            }
        }

        // allow parent to set properties in new child
        handleNewChild(newChild);
    }

    return oldChild;
}

From source file:org.structr.web.entity.dom.DOMNode.java

@Override
public Node appendChild(final Node newChild) throws DOMException {

    checkWriteAccess();/*from   w  ww.j  a  v a2s .c o  m*/
    checkSameDocument(newChild);
    checkHierarchy(newChild);

    try {

        if (newChild instanceof DocumentFragment) {

            // When inserting document fragments, we must take
            // care of the special case that the nodes already
            // have a NEXT_LIST_ENTRY relationship coming from
            // the document fragment, so we must first remove
            // the node from the document fragment and then
            // add it to the new parent.
            // replace indirectly using insertBefore and remove
            final DocumentFragment fragment = (DocumentFragment) newChild;
            Node currentChild = fragment.getFirstChild();

            while (currentChild != null) {

                // save next child in fragment list for later use
                final Node savedNextChild = currentChild.getNextSibling();

                // remove child from document fragment
                fragment.removeChild(currentChild);

                // append child to new parent
                appendChild(currentChild);

                // next
                currentChild = savedNextChild;
            }

        } else {

            final Node _parent = newChild.getParentNode();

            if (_parent != null && _parent instanceof DOMNode) {
                _parent.removeChild(newChild);
            }

            treeAppendChild((DOMNode) newChild);

            // allow parent to set properties in new child
            handleNewChild(newChild);
        }

    } catch (FrameworkException fex) {

        throw new DOMException(DOMException.INVALID_STATE_ERR, fex.toString());
    }

    return newChild;
}