geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r684184 - /geronimo/server/trunk/testsupport/testsupport-common/src/main/java/org/apache/geronimo/testsupport/DOMUtils.java
Date Sat, 09 Aug 2008 06:07:18 GMT
Author: gawor
Date: Fri Aug  8 23:07:17 2008
New Revision: 684184

URL: http://svn.apache.org/viewvc?rev=684184&view=rev
Log:
trim empty text nodes

Modified:
    geronimo/server/trunk/testsupport/testsupport-common/src/main/java/org/apache/geronimo/testsupport/DOMUtils.java

Modified: geronimo/server/trunk/testsupport/testsupport-common/src/main/java/org/apache/geronimo/testsupport/DOMUtils.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/testsupport/testsupport-common/src/main/java/org/apache/geronimo/testsupport/DOMUtils.java?rev=684184&r1=684183&r2=684184&view=diff
==============================================================================
--- geronimo/server/trunk/testsupport/testsupport-common/src/main/java/org/apache/geronimo/testsupport/DOMUtils.java
(original)
+++ geronimo/server/trunk/testsupport/testsupport-common/src/main/java/org/apache/geronimo/testsupport/DOMUtils.java
Fri Aug  8 23:07:17 2008
@@ -20,6 +20,8 @@
 package org.apache.geronimo.testsupport;
 
 import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
 
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -52,6 +54,43 @@
         return builder;
     }
     
+    private static void trimEmptyTextNodes(Node node) {
+        Element element = null;
+        if (node instanceof Document) {
+            element = ((Document)node).getDocumentElement();
+        } else if (node instanceof Element) {
+            element = (Element)node;
+        } else {
+            return;
+        }
+        
+        List<Node> nodesToRemove = new ArrayList<Node>();
+        NodeList children = element.getChildNodes();
+        for (int i = 0; i < children.getLength(); i++) {
+            Node child = children.item(i);
+            if (child instanceof Element) {
+                trimEmptyTextNodes(child);
+            } else if (child instanceof Text) {
+                Text t = (Text)child;
+                if (t.getData().trim().length() == 0) {
+                    nodesToRemove.add(child);
+                }
+            }
+        }
+        
+        for (Node n : nodesToRemove) {
+            element.removeChild(n);
+        }
+    }
+    
+    public static void compareNodes(Node expected, Node actual, boolean trimEmptyTextNodes)
throws Exception {
+        if (trimEmptyTextNodes) {
+            trimEmptyTextNodes(expected);
+            trimEmptyTextNodes(actual);
+        }
+        compareNodes(expected, actual);
+    }
+    
     public static void compareNodes(Node expected, Node actual) throws Exception {
         if (expected.getNodeType() != actual.getNodeType()) {
             throw new Exception("Different types of nodes: " + expected + " " + actual);



Mime
View raw message