Java tutorial
//package com.java2s; /* * JBoss, Home of Professional Open Source * Copyright 2005-2008, Red Hat Middleware LLC, and individual contributors * by the @authors tag. See the copyright.txt in the distribution for a * full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ import java.util.ArrayList; import java.util.List; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { public static void assertEquivalent(Node node, Node node2) { if (node == null) { throw new IllegalArgumentException("the first node to be compared is null"); } if (node2 == null) { throw new IllegalArgumentException("the second node to be compared is null"); } if (!node.getNodeName().equals(node2.getNodeName())) { throw new IllegalArgumentException("nodes have different node names"); } int attrCount = 0; NamedNodeMap attrs = node.getAttributes(); if (attrs != null) { attrCount = attrs.getLength(); } int attrCount2 = 0; NamedNodeMap attrs2 = node2.getAttributes(); if (attrs2 != null) { attrCount2 = attrs2.getLength(); } if (attrCount != attrCount2) { throw new IllegalArgumentException("nodes hava a different number of attributes"); } outer: for (int i = 0; i < attrCount; i++) { Node n = attrs.item(i); String name = n.getNodeName(); String value = n.getNodeValue(); for (int j = 0; j < attrCount; j++) { Node n2 = attrs2.item(j); String name2 = n2.getNodeName(); String value2 = n2.getNodeValue(); if (name.equals(name2) && value.equals(value2)) { continue outer; } } throw new IllegalArgumentException("attribute " + name + "=" + value + " doesn't match"); } boolean hasChildren = node.hasChildNodes(); if (hasChildren != node2.hasChildNodes()) { throw new IllegalArgumentException("one node has children and the other doesn't"); } if (hasChildren) { NodeList nl = node.getChildNodes(); NodeList nl2 = node2.getChildNodes(); short[] toFilter = new short[] { Node.TEXT_NODE, Node.ATTRIBUTE_NODE, Node.COMMENT_NODE }; List nodes = filter(nl, toFilter); List nodes2 = filter(nl2, toFilter); int length = nodes.size(); if (length != nodes2.size()) { throw new IllegalArgumentException("nodes hava a different number of children"); } for (int i = 0; i < length; i++) { Node n = (Node) nodes.get(i); Node n2 = (Node) nodes2.get(i); assertEquivalent(n, n2); } } } private static List filter(NodeList nl, short[] typesToFilter) { List nodes = new ArrayList(); outer: for (int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); short type = n.getNodeType(); for (int j = 0; j < typesToFilter.length; j++) { if (typesToFilter[j] == type) { continue outer; } } nodes.add(n); } return nodes; } }