Example usage for org.w3c.dom Node removeChild

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

Introduction

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

Prototype

public Node removeChild(Node oldChild) throws DOMException;

Source Link

Document

Removes the child node indicated by oldChild from the list of children, and returns it.

Usage

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);
    }/*www .  j  a v a 2s  .  c  o  m*/

    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();//from  ww w.  j  a  va  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();/*w  w w  .j ava  2s  .  co 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;
}

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

private Node importNode(final Node node, final boolean deep, final boolean removeParentFromSourceNode)
        throws DOMException {

    if (node instanceof DOMNode) {

        final DOMNode domNode = (DOMNode) node;

        // step 1: use type-specific import impl.
        Node importedNode = domNode.doImport(Page.this);

        // step 2: do recursive import?
        if (deep && domNode.hasChildNodes()) {

            // FIXME: is it really a good idea to do the
            // recursion inside of a transaction?
            Node child = domNode.getFirstChild();

            while (child != null) {

                // do not remove parent for child nodes
                importNode(child, deep, false);
                child = child.getNextSibling();

                logger.log(Level.INFO, "sibling is {0}", child);
            }/* w w  w . j  a va 2 s  . c om*/

        }

        // step 3: remove node from its current parent
        // (Note that this step needs to be done last in
        // (order for the child to be able to find its
        // siblings.)
        if (removeParentFromSourceNode) {

            // only do this for the actual source node, do not remove
            // child nodes from its parents
            Node _parent = domNode.getParentNode();
            if (_parent != null) {
                _parent.removeChild(domNode);
            }
        }

        return importedNode;

    }

    return null;
}

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

private Node adoptNode(final Node node, final boolean removeParentFromSourceNode) throws DOMException {

    if (node instanceof DOMNode) {

        final DOMNode domNode = (DOMNode) node;

        // step 2: do recursive import?
        if (domNode.hasChildNodes()) {

            Node child = domNode.getFirstChild();
            while (child != null) {

                // do not remove parent for child nodes
                adoptNode(child, false);
                child = child.getNextSibling();
            }//from   w  w w. j  av a  2  s  . c  o  m

        }

        // step 3: remove node from its current parent
        // (Note that this step needs to be done last in
        // (order for the child to be able to find its
        // siblings.)
        if (removeParentFromSourceNode) {

            // only do this for the actual source node, do not remove
            // child nodes from its parents
            Node _parent = domNode.getParentNode();
            if (_parent != null) {
                _parent.removeChild(domNode);
            }
        }

        // step 1: use type-specific adopt impl.
        Node adoptedNode = domNode.doAdopt(Page.this);

        return adoptedNode;

    }

    return null;
}

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

/**
 * Remove duplicate Head element from import process.
 * @param page//from   www  . ja  v a2 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.wso2.carbon.bpmn.core.types.datatypes.xml.api.XMLDocument.java

/**
 * Function to set/replace/update an object (String / Element) to matching the xPath provided. (In case new element
 * is added, this api will clone it and merge the new node to the target location pointed by xPath and return the new cloned node)
 *
 * @param xPathStr xPath to the location object need to set
 * @param obj      String or Node/*from   w  w w.  j  av a2s . c o  m*/
 * @return returns the node get updated when the set object is String, or returns newly added Node in case object is Element
 * @throws XPathExpressionException If expression cannot be evaluated
 * @throws BPMNXmlException         is thrown due to : Provided XPath and object does not match, provided object is not a Node or String
 *                                  result is NodeList, not a Text node or Element
 */
public Node set(String xPathStr, Object obj) throws XPathExpressionException, BPMNXmlException {

    NodeList evalResult = (NodeList) Utils.evaluateXPath(this.doc, xPathStr, XPathConstants.NODESET);
    if (evalResult.getLength() == 1) {
        Node targetNode = evalResult.item(0);

        if (obj instanceof String && targetNode instanceof Text) { //if string is provided, assume that user
            //need to replace the node value
            targetNode.setNodeValue((String) obj);
            //return updated Text Node
            return targetNode;

        } else if ((obj instanceof Integer || obj instanceof Byte || obj instanceof Character
                || obj instanceof Short || obj instanceof Long || obj instanceof Float || obj instanceof Double
                || obj instanceof Boolean) && targetNode instanceof Text) {
            //need to replace the node value
            targetNode.setNodeValue(obj.toString());
            //return updated Text Node
            return targetNode;

        } else if (obj instanceof Element && targetNode instanceof Element
                && targetNode.getParentNode() != null) { //if the user provides Node object,
            // assume that need to replace the target node
            Node targetParent = targetNode.getParentNode();
            Node nextSibling = targetNode.getNextSibling();
            //remove the target node
            targetParent.removeChild(targetNode);
            //add new node
            Node newNode = doc.importNode((Node) obj, true);

            if (nextSibling != null) {
                //If next sibling exists we have to put the new node before it
                targetParent.insertBefore(newNode, nextSibling);
            } else {
                targetParent.appendChild(newNode);
            }
            //return new node
            return newNode;

        } else { //provided XPath and object to set does not match
            throw new BPMNXmlException("Provided XPath and provided object does not match");
        }

    } else if (evalResult.getLength() > 0) {

        throw new BPMNXmlException(
                "Error in provided xPath. Evaluation result is NodeList, not a Text node or Element");

    } else {
        throw new BPMNXmlException("Error in provided xPath. Evaluation result is not a Text node or Element");
    }

}

From source file:org.wso2.carbon.es.migration.client.ProviderMigrationClient.java

private void migrateProvider(Collection root, Registry registry) throws RegistryException, SAXException,
        TransformerException, ParserConfigurationException, IOException {
    String[] childrenPaths = root.getChildren();
    for (String child : childrenPaths) {
        Resource childResource = registry.get(child);
        if (childResource instanceof Collection) {
            migrateProvider((Collection) childResource, registry);
        } else {//from   ww w . java2  s.c  om
            String path = childResource.getPath();
            byte[] configContent = (byte[]) childResource.getContent();
            String contentString = RegistryUtils.decodeBytes(configContent);
            Document dom = stringToDocument(contentString);
            if (dom.getElementsByTagName(Constants.OVERVIEW).getLength() > 0) {
                Node overview = dom.getElementsByTagName(Constants.OVERVIEW).item(0);
                NodeList childrenList = overview.getChildNodes();
                for (int j = 0; j < childrenList.getLength(); j++) {
                    Node node = childrenList.item(j);
                    if (Constants.PROVIDER.equals(node.getNodeName())) {
                        overview.removeChild(node);
                    }
                }
                String newContentString = documentToString(dom);
                byte[] newContentObject = RegistryUtils.encodeString(newContentString);
                childResource.setContent(newContentObject);
                registry.put(path, childResource);
            }
        }
    }
}

From source file:org.wso2.carbon.greg.migration.client.ProviderMigrationClient.java

private void migrateProvider(Collection root, Registry registry) throws RegistryException, SAXException,
        TransformerException, ParserConfigurationException, IOException {
    String[] childrenPaths = root.getChildren();
    for (String child : childrenPaths) {
        Resource childResource = registry.get(child);
        if (childResource instanceof Collection) {
            migrateProvider((Collection) childResource, registry);
        } else {/*w  ww.j a v  a  2  s.co m*/
            String path = childResource.getPath();
            byte[] configContent = (byte[]) childResource.getContent();
            String contentString = RegistryUtils.decodeBytes(configContent);
            Document dom = stringToDocument(contentString);
            if (dom.getElementsByTagName(Constants.OVERVIEW).getLength() > 0) {
                Node overview = dom.getElementsByTagName(Constants.OVERVIEW).item(0);
                NodeList childrenList = overview.getChildNodes();
                for (int j = 0; j < childrenList.getLength(); j++) {
                    Node node = childrenList.item(j);
                    if (Constants.PROVIDER.equals(node.getNodeName())) {
                        overview.removeChild(node);
                        String newContentString = documentToString(dom);
                        byte[] newContentObject = RegistryUtils.encodeString(newContentString);
                        childResource.setContent(newContentObject);
                        registry.put(path, childResource);
                    }
                }

            }
        }
    }
}

From source file:org.xwiki.xml.html.HTMLUtils.java

/**
 * Remove the first element inside a parent element and copy the element's children in the parent.
 *
 * @param document the w3c document from which to remove the top level paragraph
 * @param parentTagName the name of the parent tag to look under
 * @param elementTagName the name of the first element to remove
 *///from www . j a v  a 2  s  . c  om
public static void stripFirstElementInside(Document document, String parentTagName, String elementTagName) {
    NodeList parentNodes = document.getElementsByTagName(parentTagName);
    if (parentNodes.getLength() > 0) {
        Node parentNode = parentNodes.item(0);
        // Look for a p element below the first parent element
        Node pNode = parentNode.getFirstChild();
        if (elementTagName.equalsIgnoreCase(pNode.getNodeName())) {
            // Move all children of p node under the root element
            NodeList pChildrenNodes = pNode.getChildNodes();
            while (pChildrenNodes.getLength() > 0) {
                parentNode.insertBefore(pChildrenNodes.item(0), null);
            }
            parentNode.removeChild(pNode);
        }
    }
}