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.apache.shindig.gadgets.rewrite.TemplateRewriter.java

/**
 * Post-processes the gadget content after rendering templates.
 * /* w ww .j  av a  2s.c om*/
 * @param templateContext TemplateContext to operate on
 * @param needsFeature Should the templates feature be made available to 
 * client?
 * @param head Head element of the gadget's document
 * @param libraries Keeps track of all libraries, and which got used
 * @param allTemplates A list of all the template nodes
 * @param libraries A list of all registered libraries
 */
private void postProcess(TemplateContext templateContext, boolean needsFeature, Element head,
        List<Element> allTemplates, List<TemplateLibrary> libraries) {
    // Inject all the needed library assets.
    // TODO: inject library assets that aren't used on the server, but will
    // be needed on the client
    for (TemplateResource resource : templateContext.getResources()) {
        injectTemplateLibraryAssets(resource, head);
    }

    // If we don't need the feature, remove it and all templates from the gadget
    if (!needsFeature) {
        templateContext.getGadget().removeFeature(TEMPLATES_FEATURE_NAME);
        for (Element template : allTemplates) {
            Node parent = template.getParentNode();
            if (parent != null) {
                parent.removeChild(template);
            }
        }
    } else {
        // If the feature is to be kept, inject the libraries.
        // Library assets will be generated on the client.
        // TODO: only inject the templates, not the full scripts/styles
        for (TemplateLibrary library : libraries) {
            injectTemplateLibrary(library, head);
        }
    }
}

From source file:org.apache.ws.security.message.TestMessageTransformer.java

public static Element duplicateEncryptedDataInWsseHeader(Element saaj, boolean moveReferenceList) {
    if (moveReferenceList) {
        moveReferenceList(saaj);/*  w ww  .  java2s  .c om*/
    }
    Element body = getFirstChildElement(saaj, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"),
            true);
    Element encData = getFirstChildElement(body,
            new QName("http://www.w3.org/2001/04/xmlenc#", "EncryptedData"), true);
    Element newEncData = createNewEncryptedData(encData);
    Element sh = getFirstChildElement(saaj, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Header"),
            true);
    Element wsseHeader = getFirstChildElement(sh,
            new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
                    "Security"),
            true);

    Node newWsseHeader = wsseHeader.cloneNode(false);
    Node cur = wsseHeader.getFirstChild();

    String newId = newEncData.getAttributeNS(null, "Id");
    while (cur != null) {
        cur = copyHeadersAndUpdateRefList(cur, newWsseHeader, newId);
    }
    newWsseHeader.appendChild(newEncData);

    if (!moveReferenceList) {
        updateEncryptedKeyRefList(newWsseHeader, newId);
    }

    Node parent = wsseHeader.getParentNode();
    parent.removeChild(wsseHeader);
    parent.appendChild(newWsseHeader);
    print(saaj.getOwnerDocument());
    return newEncData;
}

From source file:org.apache.ws.security.message.TestMessageTransformer.java

public static Element duplicateEncryptedDataInWsseWrapperHeader(Element saaj, boolean moveReferenceList) {
    if (moveReferenceList) {
        moveReferenceList(saaj);//w w w . jav  a  2 s  .c  o  m
    }
    Element body = getFirstChildElement(saaj, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"),
            true);
    Element encData = getFirstChildElement(body,
            new QName("http://www.w3.org/2001/04/xmlenc#", "EncryptedData"), true);
    Element newEncData = createNewEncryptedData(encData);
    Element sh = getFirstChildElement(saaj, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Header"),
            true);
    Element signature = getFirstChildElement(sh, new QName("http://www.w3.org/2000/09/xmldsig#", "Signature"),
            true);

    Node wsseHeader = signature.getParentNode();
    Node newWsseHeader = wsseHeader.cloneNode(false);
    Node cur = wsseHeader.getFirstChild();
    String newId = newEncData.getAttributeNS(null, "Id");
    while (!cur.isSameNode(signature)) {
        cur = copyHeadersAndUpdateRefList(cur, newWsseHeader, newId);
    }
    Element wrapper = encData.getOwnerDocument().createElementNS(null, "a");
    wrapper.appendChild(newEncData);
    newWsseHeader.appendChild(wrapper);
    while (cur != null) {
        cur = copyHeadersAndUpdateRefList(cur, newWsseHeader, newId);
    }

    if (!moveReferenceList) {
        updateEncryptedKeyRefList(newWsseHeader, newId);
    }

    Node parent = wsseHeader.getParentNode();
    parent.removeChild(wsseHeader);
    parent.appendChild(newWsseHeader);
    print(saaj.getOwnerDocument());
    return newEncData;
}

From source file:org.apache.ws.security.message.TestMessageTransformer.java

public static Element addEncryptedDataWithEmbeddedEncryptedKeyInWsseHeader(Element saaj) {
    moveReferenceList(saaj);/*from  ww w . j ava2 s .c  o  m*/
    Element body = getFirstChildElement(saaj, new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"),
            true);
    Element encData = getFirstChildElement(body,
            new QName("http://www.w3.org/2001/04/xmlenc#", "EncryptedData"), true);

    Element newEncData = (Element) encData.cloneNode(true);
    String newId = newEncData.getAttributeNS(null, "Id") + "b";
    newEncData.setAttributeNS(null, "Id", newId);

    Element encKey = getFirstChildElement(saaj, new QName("http://www.w3.org/2001/04/xmlenc#", "EncryptedKey"),
            true);
    Element newEncKey = (Element) encKey.cloneNode(true);
    String newEcId = newEncKey.getAttributeNS(null, "Id") + "b";
    newEncKey.setAttributeNS(null, "Id", newEcId);

    Element keyInfo = getFirstChildElement(newEncData,
            new QName("http://www.w3.org/2000/09/xmldsig#", "KeyInfo"), true);
    Element str = getFirstChildElement(newEncData,
            new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
                    "SecurityTokenReference"),
            true);
    keyInfo.replaceChild(newEncKey, str);

    Element wsseHeader = getFirstChildElement(saaj,
            new QName("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
                    "Security"),
            true);

    Node newWsseHeader = wsseHeader.cloneNode(false);
    Node cur = wsseHeader.getFirstChild();

    while (cur != null) {
        cur = copyHeadersAndUpdateRefList(cur, newWsseHeader, newId);
    }
    newWsseHeader.appendChild(newEncData);

    Node parent = wsseHeader.getParentNode();
    parent.removeChild(wsseHeader);
    parent.appendChild(newWsseHeader);
    print(saaj.getOwnerDocument());
    return newEncData;

}

From source file:org.apache.xml.security.encryption.XMLCipher.java

/**
 * Removes the contents of a <code>Node</code>.
 *
 * @param node the <code>Node</code> to clear.
 *///from w w  w. j av a  2 s . co m
private static void removeContent(Node node) {
    while (node.hasChildNodes()) {
        node.removeChild(node.getFirstChild());
    }
}

From source file:org.apereo.portal.layout.dlm.DistributedLayoutManager.java

public boolean deleteNode(String nodeId) throws PortalException {
    if (canDeleteNode(nodeId)) {
        IUserLayoutNodeDescription nodeDescription = this.getNode(nodeId);
        String parentNodeId = this.getParentId(nodeId);

        Document uld = this.getUserLayoutDOM();
        Element ilfNode = uld.getElementById(nodeId);
        Node parent = ilfNode.getParentNode();
        if (parent != null) {
            parent.removeChild(ilfNode);
        } else {//from ww w.java2s .c om
            throw new PortalException("Node \"" + nodeId + "\" has a NULL parent for layout of "
                    + owner.getAttribute(IPerson.USERNAME) + ".");
        }
        this.updateCacheKey();

        // now push into the PLF
        TabColumnPrefsHandler.deleteNode(ilfNode, (Element) parent, owner);
        // inform the listeners
        final int layoutId = this.getLayoutId();
        if (nodeDescription instanceof IUserLayoutChannelDescription) {
            final IUserLayoutChannelDescription userLayoutChannelDescription = (IUserLayoutChannelDescription) nodeDescription;
            this.portalEventFactory.publishPortletDeletedFromLayoutPortalEvent(this, this.owner, layoutId,
                    parentNodeId, userLayoutChannelDescription.getFunctionalName());
        } else {
            this.portalEventFactory.publishFolderDeletedFromLayoutPortalEvent(this, this.owner, layoutId,
                    parentNodeId, nodeDescription.getId(), nodeDescription.getName());
        }

        return true;
    }
    return false;
}

From source file:org.apereo.portal.layout.dlm.ParameterEditManager.java

/**
 * Remove a parameter edit directive from the parameter edits set for
 * applying user specified values to a named parameter of an incorporated
 * channel represented by the passed-in target id. If one doesn't exists
 * for that node and that name then this call returns without any effects.
 *///from w  w w  .  ja v a2s . c  o m
public static void removeParmEditDirective(String targetId, String name, IPerson person)
        throws PortalException {
    Document plf = (Document) person.getAttribute(Constants.PLF);
    Element parmSet = getParmEditSet(plf, person, false);

    if (parmSet == null)
        return; // no set so no edit to remove

    NodeList edits = parmSet.getChildNodes();

    for (int i = 0; i < edits.getLength(); i++) {
        Element edit = (Element) edits.item(i);
        if (edit.getAttribute(Constants.ATT_TARGET).equals(targetId)
                && edit.getAttribute(Constants.ATT_NAME).equals(name)) {
            parmSet.removeChild(edit);
            break;
        }
    }
    if (parmSet.getChildNodes().getLength() == 0) // no more edits, remove
    {
        Node parent = parmSet.getParentNode();
        parent.removeChild(parmSet);
    }
}

From source file:org.automagic.deps.doctor.editor.PomWriterImpl.java

private void clearDummyComment(Node node) {
    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node item = childNodes.item(i);
        if (Node.COMMENT_NODE == item.getNodeType()) {
            node.removeChild(item);
            break;
        }//from   w  w  w  . j  av a 2s.  c  o  m
        if (Node.TEXT_NODE == item.getNodeType()) {
            item.setNodeValue(EMPTY);
        }
    }
}

From source file:org.codelibs.fess.transformer.FessXpathTransformer.java

protected Node pruneNode(final Node node) {
    final NodeList nodeList = node.getChildNodes();
    final List<Node> childNodeList = new ArrayList<Node>();
    final List<Node> removedNodeList = new ArrayList<Node>();
    for (int i = 0; i < nodeList.getLength(); i++) {
        final Node childNode = nodeList.item(i);
        if (isPrunedTag(childNode.getNodeName())) {
            removedNodeList.add(childNode);
        } else {// w w w  .j  a va  2  s  .c om
            childNodeList.add(childNode);
        }
    }

    for (final Node childNode : removedNodeList) {
        node.removeChild(childNode);
    }

    for (final Node childNode : childNodeList) {
        pruneNode(childNode);
    }

    return node;
}

From source file:org.dhatim.xml.DomUtils.java

/**
 * Replace one node with a list of nodes.
 * @param newNodes New nodes - added in same location as oldNode.
 * @param oldNode Old node - removed./*from   w  w w .  ja v  a  2s  . co  m*/
 * @param clone Clone Nodelist Nodes.
 */
public static void replaceNode(NodeList newNodes, Node oldNode, boolean clone) {
    AssertArgument.isNotNull(newNodes, "newNodes");
    AssertArgument.isNotNull(oldNode, "oldNode");

    Node parentNode = oldNode.getParentNode();

    if (parentNode == null) {
        logger.debug("Cannot replace [" + oldNode + "] with a NodeList. [" + oldNode + "] has no parent.");
        return;
    }

    int nodeCount = newNodes.getLength();
    List nodeList = DomUtils.copyNodeList(newNodes);

    if (nodeCount == 0) {
        if (!(parentNode instanceof Document)) {
            parentNode.removeChild(oldNode);
        }
        return;
    }

    if (parentNode instanceof Document) {
        List elements = DomUtils.getElements(newNodes, "*", null);

        if (!elements.isEmpty()) {
            logger.debug(
                    "Request to replace the Document root node with a 1+ in length NodeList.  Replacing root node with the first element node from the NodeList.");
            parentNode.removeChild(oldNode);
            parentNode.appendChild((Node) elements.get(0));
        } else {
            logger.debug(
                    "Cannot replace document root element with a NodeList that doesn't contain an element node.");
        }
    } else {
        for (int i = 0; i < nodeCount; i++) {
            if (clone) {
                parentNode.insertBefore(((Node) nodeList.get(i)).cloneNode(true), oldNode);
            } else {
                parentNode.insertBefore((Node) nodeList.get(i), oldNode);
            }
        }
        parentNode.removeChild(oldNode);
    }
}