From 6e5a68837b14b32e55ef3a556550cf0aac7b63fe Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 14 Mar 2014 17:28:53 +0000 Subject: [PATCH] add inputencoding to mail's message when read from a file. PR 56258 git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1577617 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 5 ++++ manual/Tasks/mail.html | 25 +++++++++++++++- .../tools/ant/taskdefs/email/EmailTask.java | 14 ++++++++- .../tools/ant/taskdefs/email/Message.java | 29 ++++++++++++++++++- 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/WHATSNEW b/WHATSNEW index 56336601a..7829d2bfe 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -124,6 +124,11 @@ Other changes: Bugzilla Report 55667 Bugzilla Report 56156 + * the nested elements of now have an optional + inputEncoding attribute that can be used to specify the encoding of + files read that don't use the platform's default encoding. + Bugzilla Report 56258 + Changes from Ant 1.9.2 TO Ant 1.9.3 =================================== diff --git a/manual/Tasks/mail.html b/manual/Tasks/mail.html index 665911f06..bc693e0db 100644 --- a/manual/Tasks/mail.html +++ b/manual/Tasks/mail.html @@ -98,7 +98,19 @@ File to send as the body of the email. Property values in the file will be expanded. - messagemimetype + + messagefileinputencoding + + Specifies the encoding of the input file. Please see + + Supported Encodings for a list of possible + values. Defaults to the platform's default character + encoding. Since Ant 1.9.4 + + No + + + messagemimetype The content type of the message. The default is text/plain. No @@ -253,6 +265,17 @@ attributes:

These options are mutually exclusive. No + + inputencoding + + Specifies the encoding of the input file. Please see + + Supported Encodings for a list of possible + values. Defaults to the platform's default character + encoding. Since Ant 1.9.4 + + No +

If the src attribute is not specified, then text can be added diff --git a/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java b/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java index 5423908e0..e5c15dfc4 100644 --- a/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/email/EmailTask.java @@ -78,6 +78,7 @@ public class EmailTask extends Task { private boolean failOnError = true; private boolean includeFileNames = false; private String messageMimeType = null; + private String messageFileInputEncoding; /* special headers */ /** sender */ private EmailAddress from = null; @@ -527,6 +528,7 @@ public class EmailTask extends Task { } message.setCharset(charset); } + message.setInputEncoding(messageFileInputEncoding); // identify which files should be attached Vector files = new Vector(); @@ -601,7 +603,7 @@ public class EmailTask extends Task { /** * Sets the character set of mail message. * Will be ignored if mimeType contains ....; Charset=... substring or - * encoding is not a mime. + * encoding is not mime. * @param charset the character encoding to use. * @since Ant 1.6 */ @@ -619,5 +621,15 @@ public class EmailTask extends Task { return charset; } + /** + * Sets the encoding to expect when reading the message from a file. + *

Will be ignored if the message has been specified inline.

+ * @param encoding the name of the charset used + * @since Ant 1.9.4 + */ + public void setMessageFileInputEncoding(String encoding) { + messageFileInputEncoding = encoding; + } + } diff --git a/src/main/org/apache/tools/ant/taskdefs/email/Message.java b/src/main/org/apache/tools/ant/taskdefs/email/Message.java index 29813dd07..c121f5d78 100644 --- a/src/main/org/apache/tools/ant/taskdefs/email/Message.java +++ b/src/main/org/apache/tools/ant/taskdefs/email/Message.java @@ -20,10 +20,13 @@ package org.apache.tools.ant.taskdefs.email; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; +import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; +import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintStream; +import java.io.Reader; import org.apache.tools.ant.ProjectComponent; @@ -38,6 +41,7 @@ public class Message extends ProjectComponent { private String mimeType = "text/plain"; private boolean specified = false; private String charset = null; + private String inputEncoding; /** Creates a new empty message */ public Message() { @@ -122,7 +126,7 @@ public class Message extends ProjectComponent { : new BufferedWriter(new OutputStreamWriter(ps)); if (messageSource != null) { // Read message from a file - FileReader freader = new FileReader(messageSource); + Reader freader = getReader(messageSource); try { BufferedReader in = new BufferedReader(freader); @@ -172,5 +176,28 @@ public class Message extends ProjectComponent { public String getCharset() { return charset; } + + /** + * Sets the encoding to expect when reading the message from a file. + *

Will be ignored if the message has been specified inline.

+ * @param encoding the name of the charset used + * @since Ant 1.9.4 + */ + public void setInputEncoding(String encoding) { + this.inputEncoding = encoding; + } + + private Reader getReader(File f) throws IOException { + if (inputEncoding != null) { + FileInputStream fis = new FileInputStream(f); + try { + return new InputStreamReader(fis, inputEncoding); + } catch (IOException ex) { + fis.close(); + throw ex; + } + } + return new FileReader(f); + } }