diff --git a/src/main/org/apache/tools/ant/util/DOMElementWriter.java b/src/main/org/apache/tools/ant/util/DOMElementWriter.java
index 4795e7523..69b5798b5 100644
--- a/src/main/org/apache/tools/ant/util/DOMElementWriter.java
+++ b/src/main/org/apache/tools/ant/util/DOMElementWriter.java
@@ -103,6 +103,7 @@ public class DOMElementWriter {
// Write child elements and text
NodeList children = element.getChildNodes();
boolean hasChildren = (children.getLength() > 0);
+ boolean hasChildElements = false;
openElement(element, out, indent, indentWith, hasChildren);
if (hasChildren) {
@@ -112,6 +113,7 @@ public class DOMElementWriter {
switch (child.getNodeType()) {
case Node.ELEMENT_NODE:
+ hasChildElements = true;
if (i == 0) {
out.write(lSep);
}
@@ -154,7 +156,7 @@ public class DOMElementWriter {
// Do nothing
}
}
- closeElement(element, out, indent, indentWith, true);
+ closeElement(element, out, indent, indentWith, hasChildElements);
}
}
diff --git a/src/testcases/org/apache/tools/ant/util/DOMElementWriterTest.java b/src/testcases/org/apache/tools/ant/util/DOMElementWriterTest.java
index 72039347e..d5241bd25 100644
--- a/src/testcases/org/apache/tools/ant/util/DOMElementWriterTest.java
+++ b/src/testcases/org/apache/tools/ant/util/DOMElementWriterTest.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2000-2004 The Apache Software Foundation
+ * Copyright 2000-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,9 +17,13 @@
package org.apache.tools.ant.util;
+import java.io.IOException;
+import java.io.StringWriter;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
/**
* Tests for org.apache.tools.ant.util.DOMElementWriter.
@@ -92,4 +96,51 @@ public class DOMElementWriterTest extends TestCase {
assertEquals("A]]>B]]>C",
w.encodedata("A]]>B]]>C"));
}
+
+ public void testNoAdditionalWhiteSpaceForText() throws IOException {
+ Document d = DOMUtils.newDocument();
+ Element root = d.createElement("root");
+ DOMUtils.appendTextElement(root, "textElement", "content");
+
+ StringWriter sw = new StringWriter();
+ DOMElementWriter w = new DOMElementWriter();
+ w.write(root, sw, 0, " ");
+ assertEquals("" + StringUtils.LINE_SEP
+ + " content"
+ + StringUtils.LINE_SEP
+ + "" + StringUtils.LINE_SEP,
+ sw.toString());
+ }
+
+ public void testNoAdditionalWhiteSpaceForCDATA() throws IOException {
+ Document d = DOMUtils.newDocument();
+ Element root = d.createElement("root");
+ DOMUtils.appendCDATAElement(root, "cdataElement", "content");
+
+ StringWriter sw = new StringWriter();
+ DOMElementWriter w = new DOMElementWriter();
+ w.write(root, sw, 0, " ");
+ assertEquals("" + StringUtils.LINE_SEP
+ + " "
+ + StringUtils.LINE_SEP
+ + "" + StringUtils.LINE_SEP,
+ sw.toString());
+ }
+
+ public void testNoAdditionalWhiteSpaceForEmptyElement() throws IOException {
+ Document d = DOMUtils.newDocument();
+ Element root = d.createElement("root");
+ DOMUtils.createChildElement(root, "emptyElement");
+
+ StringWriter sw = new StringWriter();
+ DOMElementWriter w = new DOMElementWriter();
+ w.write(root, sw, 0, " ");
+ assertEquals("" + StringUtils.LINE_SEP
+ // + " "
+ + " "
+ + StringUtils.LINE_SEP
+ + "" + StringUtils.LINE_SEP,
+ sw.toString());
+ }
+
}