Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright IBM Corp. 2011
 * 
 * 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.Arrays;

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

public class Main {
    /**
     * This method will compare the attributes of a given src element with the attributes of a given dest element.
     * Both must contain the same attributes, but you can specify a String array of attribute names whose values
     * are ignored. Both elements must have the ignore attributes, their contents can be different and they will
     * still be considered equal.
     * @param src - the src element whose attributes are to be compared
     * @param dest - the dest element whose attributes are to be compared
     * @param ignore - the string array of attributes whose values are to be ignored during the compare.
     * @return true if the attributes of both nodes meet the criteria of being equal, false otherwise.
     */
    private static boolean compareAttributes(Element src, Element dest, String[] ignore) {
        NamedNodeMap srcAttrs = src.getAttributes();

        if (srcAttrs.getLength() != dest.getAttributes().getLength())
            return false;

        for (int ctr = 0; ctr < srcAttrs.getLength(); ctr++) {
            Node srcAttr = srcAttrs.item(ctr);

            String name = srcAttr.getNodeName();
            if (Arrays.binarySearch(ignore, name) < 0) {
                Node destAttr = dest.getAttributeNode(name);
                if (destAttr == null || !srcAttr.isEqualNode(destAttr)) {
                    return false;
                }
            }
        }

        return true;
    }
}