Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.HashSet;
import java.util.LinkedHashSet;

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

public class Main {
    /**
     * Returns only the valid child nodes of a node. Eliminates the nodes from
     * the list of child nodes of the input node if the child node type is same
     * with any of <code>ignoreNodeTypes</code>
     * 
     * @param node
     * @param maintainOrder
     * @param ignoreNodeTypes
     * @return HashSet<Node>
     */
    public static HashSet<Node> getValidChildNodes(Node node, boolean maintainOrder, short... ignoreNodeTypes) {
        NodeList childNodes = node.getChildNodes();
        HashSet<Node> filteredChildNodes = new HashSet<Node>();
        if (maintainOrder) {
            filteredChildNodes = new LinkedHashSet<Node>();
        }
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node thisNode = childNodes.item(i);
            boolean allowAdd = true;
            for (int j = 0; j < ignoreNodeTypes.length; j++) {
                if (ignoreNodeTypes[j] == thisNode.getNodeType()) {
                    allowAdd = false;
                    break;
                }
            }
            if (allowAdd) {
                filteredChildNodes.add(thisNode);
            }
        }
        return filteredChildNodes;
    }
}