From 4af209f564ee4c3b8408f089a8f23723a99a6704 Mon Sep 17 00:00:00 2001 From: Steve Loughran Date: Mon, 10 Oct 2005 21:17:56 +0000 Subject: [PATCH] echoXML does property expansion, does not print the XML header when appending. There is some defect here wherein some tailing spaces get into every printed node, so value goes to value ; I havent tracked it down yet. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@312750 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/EchoXML.java | 5 ++-- .../tools/ant/util/DOMElementWriter.java | 29 +++++++++++++++++-- .../apache/tools/ant/util/XMLFragment.java | 11 +++++-- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/EchoXML.java b/src/main/org/apache/tools/ant/taskdefs/EchoXML.java index 38798761f..68830d429 100755 --- a/src/main/org/apache/tools/ant/taskdefs/EchoXML.java +++ b/src/main/org/apache/tools/ant/taskdefs/EchoXML.java @@ -34,10 +34,10 @@ import org.w3c.dom.Element; */ public class EchoXML extends XMLFragment { - private static final DOMElementWriter writer = new DOMElementWriter(); private File file; private boolean append; + public static final String ERROR_NO_XML = "No nested XML specified"; /** * Set the output file. @@ -59,6 +59,7 @@ public class EchoXML extends XMLFragment { * Execute the task. */ public void execute() { + DOMElementWriter writer = new DOMElementWriter(!append); try { OutputStream os = null; if (file != null) { @@ -68,7 +69,7 @@ public class EchoXML extends XMLFragment { } Node n = getFragment().getFirstChild(); if (n == null) { - throw new BuildException("No nested XML specified"); + throw new BuildException(ERROR_NO_XML); } writer.write((Element) n, os); } catch (Exception e) { diff --git a/src/main/org/apache/tools/ant/util/DOMElementWriter.java b/src/main/org/apache/tools/ant/util/DOMElementWriter.java index ed9c3bb37..4795e7523 100644 --- a/src/main/org/apache/tools/ant/util/DOMElementWriter.java +++ b/src/main/org/apache/tools/ant/util/DOMElementWriter.java @@ -30,7 +30,7 @@ import org.w3c.dom.Text; /** * Writes a DOM tree to a given Writer. - * + * warning: this utility currently does not declare XML Namespaces. *

Utility class used by {@link org.apache.tools.ant.XmlLogger * XmlLogger} and * org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter @@ -39,6 +39,26 @@ import org.w3c.dom.Text; */ public class DOMElementWriter { + /** xml declaration is on by default */ + private boolean xmlDeclaration=true; + + /** + * Create an element writer. + * The ?xml? declaration will be included. + */ + public DOMElementWriter() { + } + + /** + * Create an element writer + * @param xmlDeclaration flag to indicate whether the ?xml? declaration + * should be included. + * @since Ant1.7 + */ + public DOMElementWriter(boolean xmlDeclaration) { + this.xmlDeclaration = xmlDeclaration; + } + private static String lSep = System.getProperty("line.separator"); /** @@ -50,7 +70,8 @@ public class DOMElementWriter { /** * Writes a DOM tree to a stream in UTF8 encoding. Note that - * it prepends the <?xml version='1.0' encoding='UTF-8'?>. + * it prepends the <?xml version='1.0' encoding='UTF-8'?> if + * the xmlDeclaration field is true. * The indent number is set to 0 and a 2-space indent. * @param root the root element of the DOM tree. * @param out the outputstream to write to. @@ -58,7 +79,9 @@ public class DOMElementWriter { */ public void write(Element root, OutputStream out) throws IOException { Writer wri = new OutputStreamWriter(out, "UTF8"); - wri.write("\n"); + if(xmlDeclaration) { + wri.write("\n"); + } write(root, wri, 0, " "); wri.flush(); } diff --git a/src/main/org/apache/tools/ant/util/XMLFragment.java b/src/main/org/apache/tools/ant/util/XMLFragment.java index 5d50c2871..5b1e9a8a4 100644 --- a/src/main/org/apache/tools/ant/util/XMLFragment.java +++ b/src/main/org/apache/tools/ant/util/XMLFragment.java @@ -60,7 +60,7 @@ public class XMLFragment extends ProjectComponent implements DynamicElementNS { } /** - * Add nested text. + * Add nested text, expanding properties as we go * @param s the text to add */ public void addText(String s) { @@ -80,9 +80,16 @@ public class XMLFragment extends ProjectComponent implements DynamicElementNS { return new Child(e); } + /** + * Add text to a node. + * @param n node + * @param s value + */ private void addText(Node n, String s) { + s = getProject().replaceProperties(s); + //only text nodes that are non null after property expansion are added if (s != null && !s.trim().equals("")) { - Text t = doc.createTextNode(s); + Text t = doc.createTextNode(s.trim()); n.appendChild(t); } }