Author: stevel
Date: Mon Dec 12 11:49:07 2005
New Revision: 356356
URL: http://svn.apache.org/viewcvs?rev=356356&view=rev
Log:
<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.
Modified:
ant/core/trunk/WHATSNEW
ant/core/trunk/docs/manual/CoreTasks/echo.html
ant/core/trunk/src/etc/testcases/taskdefs/echo.xml
ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Checksum.java
ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Echo.java
ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/EchoXML.java
ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/EchoTest.java
Modified: ant/core/trunk/WHATSNEW
URL:
http://svn.apache.org/viewcvs/ant/core/trunk/WHATSNEW?rev=356356&r1=356355&r2=356356&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Mon Dec 12 11:49:07 2005
@@ -300,7 +300,8 @@
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,8 +326,9 @@
* <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
===================================
Modified: ant/core/trunk/docs/manual/CoreTasks/echo.html
URL:
http://svn.apache.org/viewcvs/ant/core/trunk/docs/manual/CoreTasks/echo.html?rev=356356&r1=356355&r2=356356&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/echo.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/echo.html Mon Dec 12 11:49:07 2005
@@ -50,6 +50,11 @@
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>
Modified: ant/core/trunk/src/etc/testcases/taskdefs/echo.xml
URL:
http://svn.apache.org/viewcvs/ant/core/trunk/src/etc/testcases/taskdefs/echo.xml?rev=356356&r1=356355&r2=356356&view=diff
==============================================================================
--- ant/core/trunk/src/etc/testcases/taskdefs/echo.xml (original)
+++ ant/core/trunk/src/etc/testcases/taskdefs/echo.xml Mon Dec 12 11:49:07 2005
@@ -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>
@@ -19,5 +29,56 @@
message
</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="©" />
+ <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>
Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Checksum.java
URL:
http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Checksum.java?rev=356356&r1=356355&r2=356356&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Checksum.java
(original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Checksum.java Mon Dec
12 11:49:07 2005
@@ -444,6 +444,14 @@
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();
Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Echo.java
URL:
http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Echo.java?rev=356356&r1=356355&r2=356356&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Echo.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Echo.java Mon Dec 12
11:49:07 2005
@@ -20,6 +20,10 @@
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 @@
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 @@
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());
@@ -112,6 +125,15 @@
*/
public void setLevel(EchoLevel echoLevel) {
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;
}
/**
Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/EchoXML.java
URL:
http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/EchoXML.java?rev=356356&r1=356355&r2=356356&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/EchoXML.java
(original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/EchoXML.java Mon Dec
12 11:49:07 2005
@@ -24,12 +24,22 @@
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 [EMAIL PROTECTED]
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 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 @@
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);
}
}
Modified:
ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/EchoTest.java
URL:
http://svn.apache.org/viewcvs/ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/EchoTest.java?rev=356356&r1=356355&r2=356356&view=diff
==============================================================================
--- ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/EchoTest.java
(original)
+++ ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/EchoTest.java
Mon Dec 12 11:49:07 2005
@@ -50,4 +50,23 @@
" 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");
+ }
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]