Java XML Node Remove removeJavascripts(Node node)

Here you can find the source of removeJavascripts(Node node)

Description

Recursive method that traverses text node and removes any markup that could possibly contain javascript code.

License

Apache License

Parameter

Parameter Description
node Node in text tree.

Return

Node in text tree.

Declaration

private static Node removeJavascripts(Node node) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.*;

public class Main {
    /**/*from   www.j a  va 2s  . c  om*/
     * List of event handler attributes that could contain javascript code.
     */
    private static final List<String> eventAttributeList = Arrays.asList(
            "onclick", "onmousedown", "onmouseup", "onmouseover",
            "onmouseout", "onchange", "onsubmit", "onreset");

    /**
     * Recursive method that traverses text node and removes any markup that could possibly contain javascript code.
     * @param node Node in text tree.
     * @return Node in text tree.
     */
    private static Node removeJavascripts(Node node) {

        if (node == null)
            return node;

        //check for <script> tags
        if ("script".equalsIgnoreCase(node.getNodeName())) {
            node.getParentNode().removeChild(node);
        }

        //check for event handlers in attributes (onclick, etc.)
        NamedNodeMap attributes = node.getAttributes();
        if (attributes != null) {
            for (int attrIdx = 0; attrIdx < attributes.getLength(); attrIdx++) {
                Node attr = attributes.item(attrIdx);

                for (String embedJsEvent : eventAttributeList) {
                    if (embedJsEvent.equalsIgnoreCase(attr.getNodeName())) {
                        attributes.removeNamedItem(attr.getNodeName());
                    }
                }
            }
        }

        if (node.hasChildNodes()) {
            NodeList children = node.getChildNodes();
            for (int i = 0; i < children.getLength(); i++) {
                removeJavascripts(children.item(i));
            }
        }

        return node;
    }
}

Related

  1. removeEmptyLines(Node node)
  2. removeEmptyNodes(Node node)
  3. removeEmptyParagraphs(Node root)
  4. removeEmptyTextNodes(Node parentNode)
  5. removeEmptyTextNodes(Node parentNode)
  6. removeLeafNode(Node node)
  7. removeNamedItem(final NamedNodeMap nodeMap, final String name)
  8. removeNamespaceDeclarations(Node node, String... namespaces)
  9. removeNode(final Node node)