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:com.edgenius.wiki.rss.RSSServiceImpl.java

/**
 * @param spaceUid/*  w w w . ja  v  a2 s  .  com*/
 * @param spaceUname
 * @param viewer
 * @param skipSecurityCheck 
 * @return
 * @throws FeedException
 */
private Document getFeedDom(Integer spaceUid, String spaceUname, User viewer, boolean skipSecurityCheck)
        throws FeedException {
    ReentrantReadWriteLock lock = null;
    Document dom;
    try {

        File feedFile = new File(FileUtil.getFullPath(rssRoot.getFile().getAbsolutePath(), spaceUid + ".xml"));
        if (!feedFile.exists()) {
            createFeed(spaceUname);
        }

        //do read lock! must after createFeed possibility, otherwise, deadlock 
        lock = lockMap.get(spaceUid.toString());
        if (lock == null) {
            lock = new ReentrantReadWriteLock();
            lockMap.put(spaceUid.toString(), lock);
        }
        lock.readLock().lock();
        //!!! DON'T USE JDOM - although I test DOM, JDOM, DOM4J: JDOM is fastest. And DOM and DOM4J almost 2 time slow. But
        //!!! JDOM is not thread safe, an infinite looping can happen while this method reading different XML source, 
        //in different thread, and SAXBuild are different instance! The problem is mostly caused by NameSpace.getNamespace(), 
        //which using static HashMap and cause HashMap dead lock!!!
        DocumentBuilder builder = xmlBuilderPool.poll();
        if (builder == null) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setCoalescing(true);
            factory.setIgnoringComments(true);
            builder = factory.newDocumentBuilder();
        }
        dom = builder.parse(feedFile);
        xmlBuilderPool.add(builder);

    } catch (Exception e) {
        log.error("Unable get feed " + spaceUname + " with excpetion ", e);
        throw new FeedException("Unable get feed " + spaceUname + " with excpetion " + e);
    } finally {
        if (lock != null)
            lock.readLock().unlock();
    }
    if (dom == null) {
        log.error("Unable get feed " + spaceUname);
        throw new FeedException("Unable get feed " + spaceUname);
    }

    //~~~~~~~~~~~~ Security filter
    if (!skipSecurityCheck) {
        //need filter out the page that viewer has not permission to read.  
        List<Node> forbidPageUuidList = new ArrayList<Node>();
        String pageUuid;
        Node ele;
        NodeList list = dom.getElementsByTagName(PageRSSModule.NS_PREFIX + ":" + PageRSSModule.PAGE_UUID);
        int len = list.getLength();
        for (int idx = 0; idx < len; idx++) {
            ele = list.item(idx);
            pageUuid = ele.getTextContent();
            if (!securityService.isAllowPageReading(spaceUname, pageUuid, viewer)) {
                log.info("User " + (viewer == null ? "anonymous" : viewer.getUsername())
                        + "  has not reading permission for pageUuid " + pageUuid + " on space " + spaceUname
                        + ". Feed item of this page is removed from RSS output.");
                forbidPageUuidList.add(ele.getParentNode());
            }

        }
        if (forbidPageUuidList.size() > 0) {
            NodeList cl = dom.getElementsByTagName(PageRSSModule.CHANNEL);
            if (cl.getLength() > 0) {
                //only one channel tag!
                Node channel = cl.item(0);
                for (Node element : forbidPageUuidList) {
                    channel.removeChild(element);
                }
            }
        }
    }
    return dom;
}

From source file:com.photon.phresco.impl.ConfigManagerImpl.java

@Override
public void deleteConfiguration(String envName, Configuration configuration) throws ConfigurationException {
    Node environment = getNode(getXpathEnv(envName).toString());
    Node configNode = getNode(getXpathConfigByEnv(envName, configuration.getName()));
    environment.removeChild(configNode);
    try {//from   www  . j a v a2  s  .  co  m
        writeXml(new FileOutputStream(configFile));
    } catch (FileNotFoundException e) {
        throw new ConfigurationException(e);
    }
}

From source file:com.adaptris.util.XmlUtils.java

/**
 * Convenience method which removes a Node from the children of a parent
 *
 * @param toBeRemoved the Node to be Removed
 * @param parent the parent Node//from www . j  a  v a 2s .c o m
 * @throws Exception on error.
 */
public void removeNode(Node toBeRemoved, Node parent) throws Exception {
    parent.removeChild(toBeRemoved);
}

From source file:com.photon.phresco.impl.ConfigManagerImpl.java

private void deleteConfiguration(String envName, List<String> configNames) throws ConfigurationException {
    Node environment = getNode(getXpathEnv(envName).toString());
    for (String configName : configNames) {
        Node configNode = getNode(getXpathConfigByEnv(envName, configName));
        environment.removeChild(configNode);
    }/*from w  ww.j  a  va 2s  .c om*/
}

From source file:de.betterform.session.DefaultSerializer.java

/**
 * inlines all instances from the processor into the output document. Eventually existent @src Attributes
 * have already been removed during the 'reset' transformation.
 *
 * @param out the output document for serialization
 */// ww  w. j  av  a 2s  .c  om
public void inlineInstances(Document out) throws XFormsException {
    //imlining instances
    NodeInfo context = getDocumentElementContext(out);
    //iterate all models to get all instances
    List models = this.processor.getContainer().getModels();
    for (int i = 0; i < models.size(); i++) {
        Model model = (Model) models.get(i);

        List instances = model.getInstances();
        for (int j = 0; j < instances.size(); j++) {
            Instance instance = (Instance) instances.get(j);
            String id = instance.getId();

            //get node from out
            String search = "//*[@id='" + id + "']";
            Node outInstance = XPathUtil.getAsNode(
                    XPathCache.getInstance().evaluate(context, search, Collections.EMPTY_MAP, null), 1);
            Node imported = out.adoptNode(instance.getInstanceDocument().getDocumentElement());
            if (imported == null) {
                throw new XFormsException("Root Element for Instance '" + instance.getId() + "' not found");
            }

            Node firstChild = DOMUtil.getFirstChildElement(outInstance);

            if (firstChild != null) {
                outInstance.removeChild(firstChild);
            }

            outInstance.appendChild(imported);
            //                    outInstance.replaceChild(imported,DOMUtil.getFirstChildElement(outInstance));

        }
    }
}

From source file:DomUtils.java

/**
 * Insert the supplied nodes before the supplied reference node (refNode).
 * @param newNodes Nodes to be inserted.
 * @param refNode Reference node before which the supplied nodes should
 * be inserted./*from w  w  w  . j a v a  2s  .c  o m*/
 */
public static void insertBefore(NodeList newNodes, Node refNode) {

    Node parentNode = refNode.getParentNode();

    if (parentNode == null) {
        System.out
                .println("Cannot insert a NodeList before [" + refNode + "]. [" + refNode + "] has no parent.");
        return;
    }

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

    if (nodeCount == 0) {
        return;
    }

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

        if (!elements.isEmpty()) {
            System.out.println(
                    "Request to insert a NodeList before the Document root node.  Will replace the root element with the 1st element node from the NodeList.");
            parentNode.removeChild(refNode);
            parentNode.appendChild((Node) elements.get(0));
        } else {
            System.out.println(
                    "Cannot insert beforen the document root element from a NodeList that doesn't contain an element node.");
        }

        for (int i = 0; i < nodeCount; i++) {
            Node node = (Node) nodeList.get(i);
            if (node.getNodeType() != Node.ELEMENT_NODE) {
                System.out.println("****" + node);
                parentNode.insertBefore(node, refNode);
            }
        }
    } else {
        for (int i = 0; i < nodeCount; i++) {
            parentNode.insertBefore((Node) nodeList.get(i), refNode);
        }
    }
}

From source file:DomUtils.java

/**
 * Remove the supplied element from its containing document.
 * <p/>/*w w  w.jav  a2  s .com*/
 * Tries to manage scenarios where a request is made to remove the root element.
 * Cannot remove the root element in any of the following situations:
 * <ul>
 *  <li>"keepChildren" parameter is false.</li>
 *  <li>root element is empty of {@link Node#ELEMENT_NODE} nodes.</li>
 * </ul>
 * @param element Element to be removed.
 * @param keepChildren Keep child content.
 */
public static void removeElement(Element element, boolean keepChildren) {

    Node parent = element.getParentNode();
    if (parent == null) {
        System.out.println("Cannot remove element [" + element + "]. [" + element + "] has no parent.");
        return;
    }

    NodeList children = element.getChildNodes();

    if (parent instanceof Document) {
        List childElements = null;

        if (!keepChildren) {
            System.out.println("Cannot remove document root element [" + DomUtils.getName(element)
                    + "] without keeping child content.");
        } else {
            if (children != null && children.getLength() > 0) {
                childElements = DomUtils.getElements(element, "*", null);
            }

            if (childElements != null && !childElements.isEmpty()) {
                parent.removeChild(element);
                parent.appendChild((Element) childElements.get(0));
            } else {
                System.out.println(
                        "Cannot remove empty document root element [" + DomUtils.getName(element) + "].");
            }
        }
    } else {
        if (keepChildren && children != null) {
            DomUtils.insertBefore(children, element);
        }
        parent.removeChild(element);
    }
}

From source file:com.stratelia.webactiv.util.XMLConfigurationStore.java

public void replaceValue(Node n, String key, String value) {
    Node entry = findNode(n, key);
    if (entry != null) {
        n.removeChild(entry);
    }//from   w  w w  .j av a  2 s  .  c  o  m
    Node newElement = xmlConfigDOMDoc.createElement(key);
    Node newValue = xmlConfigDOMDoc.createTextNode(value);
    newElement.appendChild(newValue);
    n.appendChild(newElement);
}

From source file:com.stratelia.webactiv.util.XMLConfigurationStore.java

public void put(String key, String values[]) {
    Node paramNode = getXMLParamNode(key);
    if (paramNode != null) {
        Node paramNodeParent = paramNode.getParentNode();
        if (paramNodeParent != null) {
            paramNodeParent.removeChild(paramNode);
        }//w  w  w .  ja va2s  . c o m
    }
    Element param = xmlConfigDOMDoc.createElement("param");
    Node name = xmlConfigDOMDoc.createElement("param-name");
    Node description = xmlConfigDOMDoc.createElement("param-description");
    Node nameValue = xmlConfigDOMDoc.createTextNode(key);
    for (String value : values) {
        Node vValue = xmlConfigDOMDoc.createTextNode(value);
        Node v = xmlConfigDOMDoc.createElement("param-value");
        param.appendChild(v);
        v.appendChild(vValue);
    }
    rootNode.appendChild(param);
    param.appendChild(name);
    name.appendChild(nameValue);
    param.appendChild(description);
    xmlConfigDOMDoc.getDocumentElement().normalize();
}

From source file:com.photon.phresco.impl.ConfigManagerImpl.java

@Override
public void deleteConfigurations(List<Configuration> configurations) throws ConfigurationException {
    try {/*from www.  ja  v a  2  s.  co m*/
        for (Configuration configuration : configurations) {
            Node environment = getNode(getXpathEnv(configuration.getEnvName()).toString());
            Node configNode = getNode(getXpathConfigByEnv(configuration.getEnvName(), configuration.getName()));
            if (environment != null) {
                environment.removeChild(configNode);
            }
        }
        writeXml(new FileOutputStream(configFile));
    } catch (FileNotFoundException e) {
        throw new ConfigurationException(e);
    }
}