Browse Source

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 <e>value</e> goes to <e>value </e>

; I havent tracked it down yet.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@312750 13f79535-47bb-0310-9956-ffa450edef68
master
Steve Loughran 19 years ago
parent
commit
4af209f564
3 changed files with 38 additions and 7 deletions
  1. +3
    -2
      src/main/org/apache/tools/ant/taskdefs/EchoXML.java
  2. +26
    -3
      src/main/org/apache/tools/ant/util/DOMElementWriter.java
  3. +9
    -2
      src/main/org/apache/tools/ant/util/XMLFragment.java

+ 3
- 2
src/main/org/apache/tools/ant/taskdefs/EchoXML.java View File

@@ -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) {


+ 26
- 3
src/main/org/apache/tools/ant/util/DOMElementWriter.java View File

@@ -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.
* <p>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 &lt;?xml version='1.0' encoding='UTF-8'?&gt;.
* it prepends the &lt;?xml version='1.0' encoding='UTF-8'?&gt; 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("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
if(xmlDeclaration) {
wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
}
write(root, wri, 0, " ");
wri.flush();
}


+ 9
- 2
src/main/org/apache/tools/ant/util/XMLFragment.java View File

@@ -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);
}
}


Loading…
Cancel
Save