Example usage for org.w3c.dom Node getParentNode

List of usage examples for org.w3c.dom Node getParentNode

Introduction

In this page you can find the example usage for org.w3c.dom Node getParentNode.

Prototype

public Node getParentNode();

Source Link

Document

The parent of this node.

Usage

From source file:org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.java

private boolean isCandidateElement(Node node) {
    return (node instanceof Element && (isDefaultNamespace(node) || !isDefaultNamespace(node.getParentNode())));
}

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

/**
 * Get all ancestors of this node/*from  w  ww . j  a va  2  s .  co m*/
 *
 * @return list of ancestors
 */
private List<Node> getAncestors() {

    List<Node> ancestors = new ArrayList();

    Node _parent = getParentNode();
    while (_parent != null) {

        ancestors.add(_parent);
        _parent = _parent.getParentNode();
    }

    return ancestors;

}

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

@Override
public Node insertBefore(final Node newChild, final Node refChild) throws DOMException {

    // according to DOM spec, insertBefore with null refChild equals appendChild
    if (refChild == null) {

        return appendChild(newChild);
    }/*from  w w  w  .j ava 2  s  . c  om*/

    checkWriteAccess();

    checkSameDocument(newChild);
    checkSameDocument(refChild);

    checkHierarchy(newChild);
    checkHierarchy(refChild);

    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.
        final DocumentFragment fragment = (DocumentFragment) newChild;
        Node currentChild = fragment.getFirstChild();

        while (currentChild != null) {

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

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

            // insert child into new parent
            insertBefore(currentChild, refChild);

            // next
            currentChild = savedNextChild;
        }

    } else {

        final Node _parent = newChild.getParentNode();
        if (_parent != null) {

            _parent.removeChild(newChild);
        }

        try {

            // do actual tree insertion here
            treeInsertBefore((DOMNode) newChild, (DOMNode) refChild);

        } 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 refChild;
}

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

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

    checkWriteAccess();//  ww w  .java  2  s. c  o 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 w  w.  j av a 2 s .c  om
    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;
}

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

public void testAdoptNodes() {

    try {/*from   w w w  .  ja  va  2 s . c  o  m*/
        try (final Tx tx = app.tx()) {

            Page srcPage = Page.createNewPage(securityContext, "srcPage");

            assertTrue(srcPage != null);
            assertTrue(srcPage instanceof Page);

            Node html = srcPage.createElement("html");
            Node head = srcPage.createElement("head");
            Node body = srcPage.createElement("body");
            Node title = srcPage.createElement("title");
            Node h1 = srcPage.createElement("h1");
            Node div = srcPage.createElement("div");
            Node p = srcPage.createElement("p");

            // add HTML element to page
            srcPage.appendChild(html);

            // add HEAD and BODY elements to HTML
            html.appendChild(head);
            html.appendChild(body);

            // add TITLE element to HEAD
            head.appendChild(title);

            // add H1 element to BODY
            body.appendChild(h1);

            // add DIV element to BODY
            body.appendChild(div);
            div.appendChild(p);

            // add text element to P
            p.appendChild(srcPage.createTextNode("First Paragraph"));

            Page dstPage = Page.createNewPage(securityContext, "dstPage");
            assertNotNull(dstPage);

            // test adopt method
            dstPage.adoptNode(html);

            // has parent been removed for the source element?
            assertNull(html.getParentNode());

            // has owner changed for all elements?
            assertEquals(dstPage, html.getOwnerDocument());
            assertEquals(dstPage, head.getOwnerDocument());
            assertEquals(dstPage, body.getOwnerDocument());
            assertEquals(dstPage, title.getOwnerDocument());
            assertEquals(dstPage, h1.getOwnerDocument());
            assertEquals(dstPage, div.getOwnerDocument());
            assertEquals(dstPage, p.getOwnerDocument());

            // have parents been kept for all other elements?
            assertEquals(html, head.getParentNode());
            assertEquals(html, body.getParentNode());
            assertEquals(head, title.getParentNode());
            assertEquals(body, h1.getParentNode());
            assertEquals(body, div.getParentNode());
            assertEquals(div, p.getParentNode());

            // srcPage should not have a document element any more
            assertNull(srcPage.getDocumentElement());

            // srcPage should have exactly one child element
            assertEquals(1, srcPage.getChildNodes().getLength());

            tx.success();

        } catch (DOMException dex) {

            throw new FrameworkException(422, dex.getMessage());
        }

    } catch (FrameworkException ex) {

        fail("Unexpected exception");
    }
}

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

public void testImportNodesDeep() {

    try (final Tx tx = app.tx()) {

        Page srcPage = Page.createNewPage(securityContext, "srcPage");

        assertTrue(srcPage != null);/*from w w  w. j  a  v  a 2 s .c o m*/
        assertTrue(srcPage instanceof Page);

        Node html = srcPage.createElement("html");
        Node head = srcPage.createElement("head");
        Node body = srcPage.createElement("body");
        Node title = srcPage.createElement("title");
        Node h1 = srcPage.createElement("h1");
        Node div = srcPage.createElement("div");
        Node p = srcPage.createElement("p");

        // add HTML element to page
        srcPage.appendChild(html);

        // add HEAD and BODY elements to HTML
        html.appendChild(head);
        html.appendChild(body);

        // add TITLE element to HEAD
        head.appendChild(title);

        // add H1 element to BODY
        body.appendChild(h1);

        // add DIV element to BODY
        body.appendChild(div);
        div.appendChild(p);

        // add text element to P
        p.appendChild(srcPage.createTextNode("First Paragraph"));

        Page dstPage = Page.createNewPage(securityContext, "dstPage");
        assertNotNull(dstPage);

        // test
        assertEquals(srcPage, html.getOwnerDocument());

        makePublic(srcPage, dstPage, html, head, body, title, h1, div, p);

        // test import method
        dstPage.importNode(html, true);

        // has parent been removed for the source element?
        assertNull(html.getParentNode());

        // same owner for all elements?
        assertEquals(srcPage, html.getOwnerDocument());
        assertEquals(srcPage, head.getOwnerDocument());
        assertEquals(srcPage, body.getOwnerDocument());
        assertEquals(srcPage, title.getOwnerDocument());
        assertEquals(srcPage, h1.getOwnerDocument());
        assertEquals(srcPage, div.getOwnerDocument());
        assertEquals(srcPage, p.getOwnerDocument());

        // have parents been kept for all other elements?
        assertEquals(html, head.getParentNode());
        assertEquals(html, body.getParentNode());
        assertEquals(head, title.getParentNode());
        assertEquals(body, h1.getParentNode());
        assertEquals(body, div.getParentNode());
        assertEquals(div, p.getParentNode());

        tx.success();

    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Unexpected exception");
    }

    try (final Tx tx = app.tx()) {

        Document srcDoc = Jsoup.connect(baseUri + "srcPage").get();
        Document dstDoc = Jsoup.connect(baseUri + "dstPage").get();

        // pages should render exactly identical
        assertEquals(srcDoc.outerHtml(), dstDoc.outerHtml());

        tx.success();

    } catch (Exception ex) {
        ex.printStackTrace();
        fail("Unexpected exception");
    }
}

From source file:org.structr.web.maintenance.deploy.PageImportVisitor.java

/**
 * Remove duplicate Head element from import process.
 * @param page//  ww w . j av  a  2 s .com
 */
private void fixDocumentElements(final Page page) {

    final NodeList heads = page.getElementsByTagName("head");
    if (heads.getLength() > 1) {

        final Node head1 = heads.item(0);
        final Node head2 = heads.item(1);
        final Node parent = head1.getParentNode();

        final boolean h1 = head1.hasChildNodes();
        final boolean h2 = head2.hasChildNodes();

        if (h1 && h2) {

            // merge
            for (Node child = head2.getFirstChild(); child != null; child = child.getNextSibling()) {

                head2.removeChild(child);
                head1.appendChild(child);
            }

            parent.removeChild(head2);

        } else if (h1 && !h2) {

            // remove head2
            parent.removeChild(head2);

        } else if (!h1 && h2) {

            // remove head1
            parent.removeChild(head1);

        } else {

            // remove first, doesn't matter
            parent.removeChild(head1);
        }
    }
}

From source file:org.talend.dataprofiler.core.migration.impl.CheckAndUpdateAnalysisDependencyTask.java

private File removeNotUsedModels(File file) {
    // remove not used any more: where aide rule
    DocumentBuilder db;//w  w  w.  j  a v a 2s  .c  o m
    try {
        boolean needSave = false;
        db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document document = db.parse(file);
        Element root = document.getDocumentElement();
        NodeList list = root.getElementsByTagName("indicators"); //$NON-NLS-1$
        for (int i = 0; i < list.getLength(); i++) {
            Node item = list.item(i);
            NamedNodeMap attributes = item.getAttributes();
            Node typeItem = attributes.getNamedItem("xsi:type"); //$NON-NLS-1$
            if (StringUtils.equals("dataquality.indicators.sql:WhereRuleAideIndicator", //$NON-NLS-1$
                    typeItem.getNodeValue())) {
                item.getParentNode().removeChild(item);
                needSave = true;
            }
        }
        if (needSave) {
            saveFile(file, document);
        }

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        return null;
    } catch (SAXException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return file;
}