Browse Source

Add namespace support to echoxml. Based on patch by Joey Richey. PR 36804.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@688091 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 17 years ago
parent
commit
ff1c8616d3
6 changed files with 90 additions and 5 deletions
  1. +1
    -0
      CONTRIBUTORS
  2. +3
    -0
      WHATSNEW
  3. +4
    -0
      contributors.xml
  4. +8
    -0
      docs/manual/CoreTasks/echoxml.html
  5. +46
    -2
      src/main/org/apache/tools/ant/taskdefs/EchoXML.java
  6. +28
    -3
      src/tests/antunit/taskdefs/echoxml-test.xml

+ 1
- 0
CONTRIBUTORS View File

@@ -138,6 +138,7 @@ Jesse Glick
Jesse Stockall
Jim Allers
Joerg Wassmer
Joey Richey
John Sisson
Jon Dickinson
Jon S. Stevens


+ 3
- 0
WHATSNEW View File

@@ -282,6 +282,9 @@ Other changes:
an URL.
Bugzilla Report 28881

* <echoxml> now supports XML namespaces.
Bugzilla Report 36804.

Changes from Ant 1.7.0 TO Ant 1.7.1
=============================================



+ 4
- 0
contributors.xml View File

@@ -580,6 +580,10 @@
<first>Joerg</first>
<last>Wassmer</last>
</name>
<name>
<first>Joey</first>
<last>Richey</last>
</name>
<name>
<first>Jon</first>
<last>Dickinson</last>


+ 8
- 0
docs/manual/CoreTasks/echoxml.html View File

@@ -45,6 +45,14 @@
<td valign="top">Whether to append <code>file</code>, if specified.</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">namespacePolicy</td>
<td valign="top">Sets the namespace policy as defined
by <code>org.apache.tools.ant.util.DOMElementWriter.XmlNamespacePolicy</code>. Valid
values are "ignore," "elementsOnly," or "all." Default is
"ignore".</td>
<td valign="top" align="center">No</td>
</tr>
</table>
<h3>Parameters specified as nested elements</h3>
Nested XML content is required.


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

@@ -23,6 +23,7 @@ import java.io.FileOutputStream;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.util.XMLFragment;
import org.apache.tools.ant.util.DOMElementWriter;
import org.apache.tools.ant.util.FileUtils;
@@ -35,7 +36,6 @@ import org.w3c.dom.Element;
*
* Known limitations:
* <ol>
* <li>Currently no XMLNS support</li>
* <li>Processing Instructions get ignored</li>
* <li>Encoding is always UTF-8</li>
* </ol>
@@ -46,6 +46,7 @@ public class EchoXML extends XMLFragment {

private File file;
private boolean append;
private NamespacePolicy namespacePolicy = NamespacePolicy.DEFAULT;
private static final String ERROR_NO_XML = "No nested XML specified";

/**
@@ -56,6 +57,16 @@ public class EchoXML extends XMLFragment {
file = f;
}

/**
* Set the namespace policy for the xml file
* @param s namespace policy: "ignore," "elementsOnly," or "all"
* @see
* org.apache.tools.ant.util.DOMElementWriter.XmlNamespacePolicy
*/
public void setNamespacePolicy(NamespacePolicy n) {
namespacePolicy = n;
}
/**
* Set whether to append the output file.
* @param b boolean append flag.
@@ -68,7 +79,8 @@ public class EchoXML extends XMLFragment {
* Execute the task.
*/
public void execute() {
DOMElementWriter writer = new DOMElementWriter(!append);
DOMElementWriter writer =
new DOMElementWriter(!append, namespacePolicy.getPolicy());
OutputStream os = null;
try {
if (file != null) {
@@ -90,4 +102,36 @@ public class EchoXML extends XMLFragment {
}
}

public static class NamespacePolicy extends EnumeratedAttribute {
private static final String IGNORE = "ignore";
private static final String ELEMENTS = "elementsOnly";
private static final String ALL = "all";

public static final NamespacePolicy DEFAULT
= new NamespacePolicy(IGNORE);

public NamespacePolicy() {}

public NamespacePolicy(String s) {
setValue(s);
}
/** {@inheritDoc}. */
public String[] getValues() {
return new String[] {IGNORE, ELEMENTS, ALL};
}

public DOMElementWriter.XmlNamespacePolicy getPolicy() {
String s = getValue();
if (IGNORE.equalsIgnoreCase(s)) {
return DOMElementWriter.XmlNamespacePolicy.IGNORE;
} else if (ELEMENTS.equalsIgnoreCase(s)) {
return
DOMElementWriter.XmlNamespacePolicy.ONLY_QUALIFY_ELEMENTS;
} else if (ALL.equalsIgnoreCase(s)) {
return DOMElementWriter.XmlNamespacePolicy.QUALIFY_ALL;
} else {
throw new BuildException("Invalid namespace policy: " + s);
}
}
}
}

+ 28
- 3
src/tests/antunit/taskdefs/echoxml-test.xml View File

@@ -56,10 +56,35 @@
</au:expectfailure>
</target>
<target name="test-ns"> <!-- comment this if you don't have the svn trunk of antunit -->
<echoxml file="${file}" xmlns:a="antlib:a">
<a:something />
<!-- comment this and the next targets if you don't have the svn
trunk of antunit -->
<target name="test-ns-all">
<echoxml file="${file}" xmlns:a="antlib:a"
namespacepolicy="all">
<a:something a:foo="bar"/>
</echoxml>
<au:assertResourceContains resource="${file}" value="a:something"/>
<au:assertResourceContains resource="${file}" value="antlib:a"/>
</target>
<target name="test-ns-elementsOnly">
<echoxml file="${file}" xmlns:a="antlib:a"
namespacepolicy="elementsOnly">
<a:something a:foo="bar"/>
</echoxml>
<au:assertResourceContains resource="${file}" value="a:something"/>
<au:assertResourceContains resource="${file}" value="antlib:a"/>
</target>
<target name="test-ns-ignore">
<echoxml file="${file}" xmlns:a="antlib:a"
namespacepolicy="ignore">
<a:something a:foo="bar"/>
</echoxml>
<au:assertResourceContains resource="${file}" value="a:something"/>
<au:assertFalse message="Didn't expecte ${file} to contain antlib:a">
<resourcecontains resource="${file}" substring="antlib:a"/>
</au:assertFalse>
</target>
</project>

Loading…
Cancel
Save