Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2015 www.seleniumtests.com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;

import java.util.List;

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

public class Main {
    /**
     * Gets Nodes identical to a given test Node for a given parent Node
     */

    public static List getSimilarChildNodes(Node parent, Node testNode, boolean ignoreWhitespace)

    {

        ArrayList nodes = new ArrayList();

        if (!parent.hasChildNodes())

            return nodes;

        NodeList childNodes = parent.getChildNodes();

        for (int i = 0; i < childNodes.getLength(); i++)

        {

            if (nodesSimilar(childNodes.item(i), testNode, ignoreWhitespace))

                nodes.add(childNodes.item(i));

        }

        return nodes;

    }

    /**
     * Checks if two Nodes are Similar<br>
     * <p/>
     * Compares Nodes just by their Name, Type, local name and Namespace generically
     */

    public static boolean nodesSimilar(Node node1, Node node2, boolean ignoreWhitespace)

    {

        if ((node1 == null) || (node2 == null))

            return false;

        /*   
            
        if (node1.getNodeType() == node2.getNodeType() &&              
            
           (areNullorEqual(node1.getLocalName(), node2.getLocalName(), ignoreWhitespace, false)) &&
            
           (areNullorEqual(node1.getNamespaceURI(), node2.getNamespaceURI(), ignoreWhitespace, false)) &&
            
           (areNullorEqual(node1.getNodeName(), node2.getNodeName(), ignoreWhitespace, false)))
            
            return true;
            
         */

        if (areNonNullAndEqual(node1.getNamespaceURI(), node2.getNamespaceURI()))

        {

            if (node1.getNodeType() == node2.getNodeType() &&

                    (areNullorEqual(node1.getLocalName(), node2.getLocalName(), ignoreWhitespace, false)))

                return true;

        } else if ((node1.getNamespaceURI() == null) && (node2.getNamespaceURI() == null))

        {

            if (node1.getNodeType() == node2.getNodeType() &&

                    (areNullorEqual(node1.getNodeName(), node2.getNodeName(), ignoreWhitespace, false)))

                return true;

        }

        return false;

    }

    /**
     * Checks if the input Objects are Non NULL and Equal
     */

    public static boolean areNonNullAndEqual(Object obj1, Object obj2)

    {

        if ((obj1 == null) || (obj2 == null))

            return false;

        return obj1.equals(obj2);

    }

    /**
     * Checks if both the Object arguments are equal (including if they are null)
     */

    public static boolean areNullorEqual(Object obj1, Object obj2, boolean ignoreWhitespace, boolean ignoreCase)

    {

        // if both are null, they are equal

        if ((obj1 == null) && (obj2 == null))

            return true;

        // if either one of them is null, they are not equal

        if ((obj1 == null) || (obj2 == null))

            return false;

        // if they are String type

        if ((obj1 instanceof String) && (obj2 instanceof String))

        {

            if (ignoreWhitespace)

            {

                if (ignoreCase)

                    return ((String) obj1).trim().equalsIgnoreCase(((String) obj2).trim());

                else

                    return ((String) obj1).trim().equals(((String) obj2).trim());

            } else

            {

                if (ignoreCase)

                    return ((String) obj1).equalsIgnoreCase((String) obj2);

                else

                    return obj1.equals(obj2);

            }

        }

        return (obj1.equals(obj2));

    }
}