Browse Source

<echo> gets encoding support. I cannot believe we never noticed this was missing. Wherever I have been echoing out XML, I have been creating encoding bugs waiting to surface.

Also, echoxml closes its file handles.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@356356 13f79535-47bb-0310-9956-ffa450edef68
master
Steve Loughran 19 years ago
parent
commit
4e7f82aede
7 changed files with 137 additions and 6 deletions
  1. +4
    -2
      WHATSNEW
  2. +5
    -0
      docs/manual/CoreTasks/echo.html
  3. +61
    -0
      src/etc/testcases/taskdefs/echo.xml
  4. +8
    -0
      src/main/org/apache/tools/ant/taskdefs/Checksum.java
  5. +24
    -2
      src/main/org/apache/tools/ant/taskdefs/Echo.java
  6. +15
    -1
      src/main/org/apache/tools/ant/taskdefs/EchoXML.java
  7. +20
    -1
      src/testcases/org/apache/tools/ant/taskdefs/EchoTest.java

+ 4
- 2
WHATSNEW View File

@@ -300,7 +300,8 @@ Other changes:
to destination strings.

* 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
same type. Bugzilla report 17901.
@@ -325,9 +326,10 @@ Other changes:
* <xslt> now accepts nested FileNameMappers e.g. <globmapper>.
Bugzilla report 37604.


* 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
===================================



+ 5
- 0
docs/manual/CoreTasks/echo.html View File

@@ -50,6 +50,11 @@ ignored</p>
One of "error", "warning", "info", "verbose", "debug" (decreasing order)</td>
<td valign="top" align="center">No - default is "warning".</td>
</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>

<h3>Examples</h3>


+ 61
- 0
src/etc/testcases/taskdefs/echo.xml View File

@@ -2,6 +2,16 @@

<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">
<echo/>
</target>
@@ -20,4 +30,55 @@
</echo>
</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="&#169;" />
<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="&#169;" />
<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>

+ 8
- 0
src/main/org/apache/tools/ant/taskdefs/Checksum.java View File

@@ -444,6 +444,14 @@ public class Checksum extends MatchingTask implements Condition {
if (todir != null) {
// A separate directory was explicitly declared
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();
// Create the directory, as it might not exist.
directory.mkdirs();


+ 24
- 2
src/main/org/apache/tools/ant/taskdefs/Echo.java View File

@@ -20,6 +20,10 @@ package org.apache.tools.ant.taskdefs;
import java.io.File;
import java.io.FileWriter;
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.Project;
@@ -37,6 +41,8 @@ public class Echo extends Task {
protected String message = ""; // required
protected File file = null;
protected boolean append = false;
/** encoding; set to null or empty means 'default' */
private String encoding = "";

// by default, messages are always displayed
protected int logLevel = Project.MSG_WARN;
@@ -50,9 +56,16 @@ public class Echo extends Task {
if (file == null) {
log(message, logLevel);
} else {
FileWriter out = null;
Writer out = null;
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());
} catch (IOException ioe) {
throw new BuildException(ioe, getLocation());
@@ -114,6 +127,15 @@ public class Echo extends Task {
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.
*/


+ 15
- 1
src/main/org/apache/tools/ant/taskdefs/EchoXML.java View File

@@ -24,12 +24,22 @@ import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.util.XMLFragment;
import org.apache.tools.ant.util.DOMElementWriter;
import org.apache.tools.ant.util.FileUtils;

import org.w3c.dom.Node;
import org.w3c.dom.Element;

/**
* 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
*/
public class EchoXML extends XMLFragment {
@@ -60,8 +70,8 @@ public class EchoXML extends XMLFragment {
*/
public void execute() {
DOMElementWriter writer = new DOMElementWriter(!append);
OutputStream os = null;
try {
OutputStream os = null;
if (file != null) {
os = new FileOutputStream(file.getAbsolutePath(), append);
} else {
@@ -72,8 +82,12 @@ public class EchoXML extends XMLFragment {
throw new BuildException(ERROR_NO_XML);
}
writer.write((Element) n, os);
} catch (BuildException e) {
throw e;
} catch (Exception e) {
throw new BuildException(e);
} finally {
FileUtils.close(os);
}
}



+ 20
- 1
src/testcases/org/apache/tools/ant/taskdefs/EchoTest.java View File

@@ -50,4 +50,23 @@ public class EchoTest extends BuildFileTest {
" 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");
}
}

Loading…
Cancel
Save