Compare two DOM Nodes from JBoss
/**
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* @author <a href="mailto:ovidiu@feodorov.com">Ovidiu Feodorov</a>
* @version <tt>$Revision: 3282 $</tt> $Id: XMLUtil.java 3282 2007-11-01
* 15:32:29Z timfox $
*/
public class Utils {
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;
}
}
Related examples in the same category