diff --git a/src/main/org/apache/tools/ant/util/DOMElementWriter.java b/src/main/org/apache/tools/ant/util/DOMElementWriter.java index 8f7803695..ed9c3bb37 100644 --- a/src/main/org/apache/tools/ant/util/DOMElementWriter.java +++ b/src/main/org/apache/tools/ant/util/DOMElementWriter.java @@ -77,67 +77,67 @@ public class DOMElementWriter { String indentWith) throws IOException { - openElement(element, out, indent, indentWith); - // Write child elements and text - boolean hasChildren = false; NodeList children = element.getChildNodes(); - for (int i = 0; i < children.getLength(); i++) { - Node child = children.item(i); - - switch (child.getNodeType()) { - - case Node.ELEMENT_NODE: - if (!hasChildren) { - out.write(lSep); - hasChildren = true; - } - write((Element) child, out, indent + 1, indentWith); - break; - - case Node.TEXT_NODE: - out.write(encode(child.getNodeValue())); - break; - - case Node.COMMENT_NODE: - out.write(""); - break; + boolean hasChildren = (children.getLength() > 0); + openElement(element, out, indent, indentWith, hasChildren); - case Node.CDATA_SECTION_NODE: - out.write(""); - break; - - case Node.ENTITY_REFERENCE_NODE: - out.write('&'); - out.write(child.getNodeName()); - out.write(';'); - break; - - case Node.PROCESSING_INSTRUCTION_NODE: - out.write(" 0) { - out.write(' '); - out.write(data); + if (hasChildren) { + for (int i = 0; i < children.getLength(); i++) { + Node child = children.item(i); + + switch (child.getNodeType()) { + + case Node.ELEMENT_NODE: + if (i == 0) { + out.write(lSep); + } + write((Element) child, out, indent + 1, indentWith); + break; + + case Node.TEXT_NODE: + out.write(encode(child.getNodeValue())); + break; + + case Node.COMMENT_NODE: + out.write(""); + break; + + case Node.CDATA_SECTION_NODE: + out.write(""); + break; + + case Node.ENTITY_REFERENCE_NODE: + out.write('&'); + out.write(child.getNodeName()); + out.write(';'); + break; + + case Node.PROCESSING_INSTRUCTION_NODE: + out.write(" 0) { + out.write(' '); + out.write(data); + } + out.write("?>"); + break; + default: + // Do nothing } - out.write("?>"); - break; - default: - // Do nothing } + closeElement(element, out, indent, indentWith, true); } - - closeElement(element, out, indent, indentWith, hasChildren); } /** * Writes the opening tag - including all attributes - - * correspondong to a DOM element. + * corresponding to a DOM element. * * @param element the DOM element to write * @param out where to send the output @@ -149,6 +149,25 @@ public class DOMElementWriter { public void openElement(Element element, Writer out, int indent, String indentWith) throws IOException { + openElement(element, out, indent, indentWith, true); + } + + /** + * Writes the opening tag - including all attributes - + * corresponding to a DOM element. + * + * @param element the DOM element to write + * @param out where to send the output + * @param indent number of + * @param indentWith string that should be used to indent the + * corresponding tag. + * @param hasChildren whether this element has children. + * @throws IOException if an error happens while writing to the stream. + * @since Ant 1.7 + */ + public void openElement(Element element, Writer out, int indent, + String indentWith, boolean hasChildren) + throws IOException { // Write indent characters for (int i = 0; i < indent; i++) { out.write(indentWith); @@ -168,7 +187,13 @@ public class DOMElementWriter { out.write(encode(attr.getValue())); out.write("\""); } - out.write(">"); + if (hasChildren) { + out.write(">"); + } else { + out.write(" />"); + out.write(lSep); + out.flush(); + } } /** diff --git a/src/testcases/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java b/src/testcases/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java index 1ccdaff7d..817d3c857 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/optional/EchoPropertiesTest.java @@ -114,7 +114,7 @@ public class EchoPropertiesTest extends BuildFileTest { BufferedReader br = new BufferedReader( fr ); String read = null; while ( (read = br.readLine()) != null) { - if (read.indexOf("") >= 0) { + if (read.indexOf("") >= 0) { // found the property we set - it's good. return; }