Also, echoxml closes its file handles. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@356356 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -300,7 +300,8 @@ Other changes: | |||||
| to destination strings. | to destination strings. | ||||
| * Add the echoxml task. This will echo nested XML to a file, with | * Add the echoxml task. This will echo nested XML to a file, with | ||||
| the normal <?xml ?> processor instruction. UTF-8 encoding only. | |||||
| the normal <?xml ?> processor instruction. UTF-8 encoding only; no-namespace | |||||
| support. | |||||
| * Try to make subprojects of custom Project subclasses instances of the | * Try to make subprojects of custom Project subclasses instances of the | ||||
| same type. Bugzilla report 17901. | same type. Bugzilla report 17901. | ||||
| @@ -325,9 +326,10 @@ Other changes: | |||||
| * <xslt> now accepts nested FileNameMappers e.g. <globmapper>. | * <xslt> now accepts nested FileNameMappers e.g. <globmapper>. | ||||
| Bugzilla report 37604. | Bugzilla report 37604. | ||||
| * New task loadresource that accompanies loadfile for non file resources. | * New task loadresource that accompanies loadfile for non file resources. | ||||
| * <echo> now supports an encoding when saving to a file | |||||
| Changes from Ant 1.6.4 to Ant 1.6.5 | Changes from Ant 1.6.4 to Ant 1.6.5 | ||||
| =================================== | =================================== | ||||
| @@ -50,6 +50,11 @@ ignored</p> | |||||
| One of "error", "warning", "info", "verbose", "debug" (decreasing order)</td> | One of "error", "warning", "info", "verbose", "debug" (decreasing order)</td> | ||||
| <td valign="top" align="center">No - default is "warning".</td> | <td valign="top" align="center">No - default is "warning".</td> | ||||
| </tr> | </tr> | ||||
| <tr> | |||||
| <td valign="top">encoding</td> | |||||
| <td valign="top">encoding to use, default is ""; the local system encoding.</td> | |||||
| <td valign="top" align="center">No</td> | |||||
| </tr> | |||||
| </table> | </table> | ||||
| <h3>Examples</h3> | <h3>Examples</h3> | ||||
| @@ -2,6 +2,16 @@ | |||||
| <project name="echo-test" basedir="." default="test1"> | <project name="echo-test" basedir="." default="test1"> | ||||
| <property name="dest.dir" location="echo.dest"/> | |||||
| <target name="init"> | |||||
| <mkdir dir="${dest.dir}" /> | |||||
| </target> | |||||
| <target name="clean"> | |||||
| <delete dir="${dest.dir}"/> | |||||
| </target> | |||||
| <target name="test1"> | <target name="test1"> | ||||
| <echo/> | <echo/> | ||||
| </target> | </target> | ||||
| @@ -20,4 +30,55 @@ | |||||
| </echo> | </echo> | ||||
| </target> | </target> | ||||
| <macrodef name="assertContains"> | |||||
| <attribute name="expected" /> | |||||
| <attribute name="actual" /> | |||||
| <sequential> | |||||
| <fail> | |||||
| <condition> | |||||
| <not> | |||||
| <contains string="@{actual}" substring="@{expected}"></contains> | |||||
| </not> | |||||
| </condition> | |||||
| Did not find @{expected} in @{actual} | |||||
| </fail> | |||||
| </sequential> | |||||
| </macrodef> | |||||
| <target name="testFile" depends="init"> | |||||
| <echo file="${dest.dir}/echo.txt">Simple text</echo> | |||||
| <loadfile srcfile="${dest.dir}/echo.txt" property="echo" /> | |||||
| <assertContains actual="${echo}" expected="Simple text" /> | |||||
| </target> | |||||
| <target name="testAppend" depends="init"> | |||||
| <echo file="${dest.dir}/echo.txt">Simple text</echo> | |||||
| <echo file="${dest.dir}/echo.txt" append="true">Appended</echo> | |||||
| <loadfile srcfile="${dest.dir}/echo.txt" property="echo"/> | |||||
| <assertContains actual="${echo}" expected="Simple text"/> | |||||
| <assertContains actual="${echo}" expected="Appended"/> | |||||
| </target> | |||||
| <target name="testEmptyEncoding" depends="init"> | |||||
| <echo file="${dest.dir}/echo.txt" encoding="">Simple text</echo> | |||||
| <loadfile srcfile="${dest.dir}/echo.txt" property="echo"/> | |||||
| <assertContains actual="${echo}" expected="Simple text"/> | |||||
| </target> | |||||
| <target name="testUTF16Encoding" depends="init"> | |||||
| <property name="char" value="©" /> | |||||
| <echo file="${dest.dir}/echo16.txt" encoding="UTF-16">${char}</echo> | |||||
| <loadfile srcfile="${dest.dir}/echo16.txt" property="echo16" encoding="UTF16"/> | |||||
| <assertContains actual="${echo16}" expected="${char}"/> | |||||
| </target> | |||||
| <target name="testUTF8Encoding" depends="init"> | |||||
| <property name="char" value="©" /> | |||||
| <echo file="${dest.dir}/echo8.txt" encoding="UTF8">${char}</echo> | |||||
| <loadfile srcfile="${dest.dir}/echo8.txt" property="echo" encoding="UTF8"/> | |||||
| <assertContains actual="${echo}" expected="${char}"/> | |||||
| </target> | |||||
| </project> | </project> | ||||
| @@ -444,6 +444,14 @@ public class Checksum extends MatchingTask implements Condition { | |||||
| if (todir != null) { | if (todir != null) { | ||||
| // A separate directory was explicitly declared | // A separate directory was explicitly declared | ||||
| String path = (String) relativeFilePaths.get(file); | String path = (String) relativeFilePaths.get(file); | ||||
| if(path==null) { | |||||
| //bug 37386. this should not occur, but it has, once. | |||||
| throw new BuildException("Internal error: " + | |||||
| "relativeFilePaths could not match file"+ | |||||
| file+ | |||||
| "\n" + | |||||
| "please file a bug report on this"); | |||||
| } | |||||
| directory = new File(todir, path).getParentFile(); | directory = new File(todir, path).getParentFile(); | ||||
| // Create the directory, as it might not exist. | // Create the directory, as it might not exist. | ||||
| directory.mkdirs(); | directory.mkdirs(); | ||||
| @@ -20,6 +20,10 @@ package org.apache.tools.ant.taskdefs; | |||||
| import java.io.File; | import java.io.File; | ||||
| import java.io.FileWriter; | import java.io.FileWriter; | ||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.Writer; | |||||
| import java.io.BufferedWriter; | |||||
| import java.io.OutputStreamWriter; | |||||
| import java.io.FileOutputStream; | |||||
| import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
| import org.apache.tools.ant.Project; | import org.apache.tools.ant.Project; | ||||
| @@ -37,6 +41,8 @@ public class Echo extends Task { | |||||
| protected String message = ""; // required | protected String message = ""; // required | ||||
| protected File file = null; | protected File file = null; | ||||
| protected boolean append = false; | protected boolean append = false; | ||||
| /** encoding; set to null or empty means 'default' */ | |||||
| private String encoding = ""; | |||||
| // by default, messages are always displayed | // by default, messages are always displayed | ||||
| protected int logLevel = Project.MSG_WARN; | protected int logLevel = Project.MSG_WARN; | ||||
| @@ -50,9 +56,16 @@ public class Echo extends Task { | |||||
| if (file == null) { | if (file == null) { | ||||
| log(message, logLevel); | log(message, logLevel); | ||||
| } else { | } else { | ||||
| FileWriter out = null; | |||||
| Writer out = null; | |||||
| try { | try { | ||||
| out = new FileWriter(file.getAbsolutePath(), append); | |||||
| String filename = file.getAbsolutePath(); | |||||
| if(encoding==null || encoding.length()==0) { | |||||
| out = new FileWriter(filename, append); | |||||
| } else { | |||||
| out = new BufferedWriter( | |||||
| new OutputStreamWriter( | |||||
| new FileOutputStream(filename, append),encoding)); | |||||
| } | |||||
| out.write(message, 0, message.length()); | out.write(message, 0, message.length()); | ||||
| } catch (IOException ioe) { | } catch (IOException ioe) { | ||||
| throw new BuildException(ioe, getLocation()); | throw new BuildException(ioe, getLocation()); | ||||
| @@ -114,6 +127,15 @@ public class Echo extends Task { | |||||
| logLevel = echoLevel.getLevel(); | logLevel = echoLevel.getLevel(); | ||||
| } | } | ||||
| /** | |||||
| * Declare the encoding to use when outputting to a file; | |||||
| * Use "" for the platform's default encoding. | |||||
| * @param encoding | |||||
| */ | |||||
| public void setEncoding(String encoding) { | |||||
| this.encoding = encoding; | |||||
| } | |||||
| /** | /** | ||||
| * The enumerated values for the level attribute. | * The enumerated values for the level attribute. | ||||
| */ | */ | ||||
| @@ -24,12 +24,22 @@ import org.apache.tools.ant.Project; | |||||
| import org.apache.tools.ant.BuildException; | import org.apache.tools.ant.BuildException; | ||||
| import org.apache.tools.ant.util.XMLFragment; | import org.apache.tools.ant.util.XMLFragment; | ||||
| import org.apache.tools.ant.util.DOMElementWriter; | import org.apache.tools.ant.util.DOMElementWriter; | ||||
| import org.apache.tools.ant.util.FileUtils; | |||||
| import org.w3c.dom.Node; | import org.w3c.dom.Node; | ||||
| import org.w3c.dom.Element; | import org.w3c.dom.Element; | ||||
| /** | /** | ||||
| * Echo XML. | * Echo XML. | ||||
| * Notice how this is a ProjectComponent and not a task, which means that certain | |||||
| * well-known task operations (such as {@link org.apache.tools.ant.Task#getLocation()}) do not work. | |||||
| * | |||||
| * Other limitations | |||||
| * <ol> | |||||
| * <li>Currently no XMLNS support</li> | |||||
| * <li>Processing Instructions get ignored</li> | |||||
| * <li>Encoding is always UTF-8</li> | |||||
| * | |||||
| * @since Ant 1.7 | * @since Ant 1.7 | ||||
| */ | */ | ||||
| public class EchoXML extends XMLFragment { | public class EchoXML extends XMLFragment { | ||||
| @@ -60,8 +70,8 @@ public class EchoXML extends XMLFragment { | |||||
| */ | */ | ||||
| public void execute() { | public void execute() { | ||||
| DOMElementWriter writer = new DOMElementWriter(!append); | DOMElementWriter writer = new DOMElementWriter(!append); | ||||
| OutputStream os = null; | |||||
| try { | try { | ||||
| OutputStream os = null; | |||||
| if (file != null) { | if (file != null) { | ||||
| os = new FileOutputStream(file.getAbsolutePath(), append); | os = new FileOutputStream(file.getAbsolutePath(), append); | ||||
| } else { | } else { | ||||
| @@ -72,8 +82,12 @@ public class EchoXML extends XMLFragment { | |||||
| throw new BuildException(ERROR_NO_XML); | throw new BuildException(ERROR_NO_XML); | ||||
| } | } | ||||
| writer.write((Element) n, os); | writer.write((Element) n, os); | ||||
| } catch (BuildException e) { | |||||
| throw e; | |||||
| } catch (Exception e) { | } catch (Exception e) { | ||||
| throw new BuildException(e); | throw new BuildException(e); | ||||
| } finally { | |||||
| FileUtils.close(os); | |||||
| } | } | ||||
| } | } | ||||
| @@ -50,4 +50,23 @@ public class EchoTest extends BuildFileTest { | |||||
| " message\n"+ | " message\n"+ | ||||
| " "); | " "); | ||||
| } | } | ||||
| } | |||||
| public void testFile() throws Exception { | |||||
| executeTarget("testFile"); | |||||
| } | |||||
| public void testAppend() throws Exception { | |||||
| executeTarget("testAppend"); | |||||
| } | |||||
| public void testEmptyEncoding() throws Exception { | |||||
| executeTarget("testEmptyEncoding"); | |||||
| } | |||||
| public void testUTF16Encoding() throws Exception { | |||||
| executeTarget("testUTF16Encoding"); | |||||
| } | |||||
| public void testUTF8Encoding() throws Exception { | |||||
| executeTarget("testUTF8Encoding"); | |||||
| } | |||||
| } | |||||