Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrofit Ant with CharSet #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/main/org/apache/tools/ant/listener/MailLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.tools.ant.taskdefs.email.EmailAddress;
import org.apache.tools.ant.taskdefs.email.Mailer;
import org.apache.tools.ant.taskdefs.email.Message;
import org.apache.tools.ant.types.CharSet;
import org.apache.tools.ant.util.ClasspathUtils;
import org.apache.tools.ant.util.DateUtils;
import org.apache.tools.ant.util.FileUtils;
Expand Down Expand Up @@ -96,6 +97,7 @@
*/
public class MailLogger extends DefaultLogger {
private static final String DEFAULT_MIME_TYPE = "text/plain";
private static final String DEFAULT_CHARSET_NAME = "default";

/** Buffer in which the message is constructed prior to sending */
private StringBuffer buffer = new StringBuffer();
Expand Down Expand Up @@ -159,7 +161,7 @@ public void buildFinished(BuildEvent event) {
.toCcList(getValue(properties, prefix + ".cc", ""))
.toBccList(getValue(properties, prefix + ".bcc", ""))
.mimeType(getValue(properties, "mimeType", DEFAULT_MIME_TYPE))
.charset(getValue(properties, "charset", ""))
.charSet(new CharSet(getValue(properties, "charset", DEFAULT_CHARSET_NAME)))
.body(getValue(properties, prefix + ".body", ""))
.subject(getValue(
properties, prefix + ".subject",
Expand Down Expand Up @@ -267,12 +269,19 @@ public Values subject(String subject) {
this.subject = subject;
return this;
}
private String charset;
private CharSet charSet;
public String charset() {
return charset;
return charSet.getValue();
}
public CharSet charSet() {
return charSet;
}
public Values charset(String charset) {
this.charset = charset;
this.charSet = new CharSet(charset);
return this;
}
public Values charSet(CharSet charSet) {
this.charSet = charSet;
return this;
}
private String mimeType;
Expand Down Expand Up @@ -365,7 +374,7 @@ private void sendMail(Values values, String message) throws IOException {

mailMessage.setSubject(values.subject());

if (values.charset().isEmpty()) {
if (DEFAULT_CHARSET_NAME.equals(values.charset())) {
mailMessage.setHeader("Content-Type", values.mimeType());
} else {
mailMessage.setHeader("Content-Type", values.mimeType()
Expand Down Expand Up @@ -406,8 +415,8 @@ private void sendMimeMail(Project project, Values values, String message) {
new Message(!values.body().isEmpty() ? values.body() : message);
mymessage.setProject(project);
mymessage.setMimeType(values.mimeType());
if (!values.charset().isEmpty()) {
mymessage.setCharset(values.charset());
if (!DEFAULT_CHARSET_NAME.equals(values.charset())) {
mymessage.setCharSet(values.charSet());
}
mailer.setMessage(mymessage);
mailer.setFrom(new EmailAddress(values.from()));
Expand Down
107 changes: 64 additions & 43 deletions src/main/org/apache/tools/ant/taskdefs/Concat.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -37,6 +36,7 @@
import org.apache.tools.ant.ProjectComponent;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.filters.util.ChainReaderHelper;
import org.apache.tools.ant.types.CharSet;
import org.apache.tools.ant.types.FileList;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.FilterChain;
Expand Down Expand Up @@ -89,11 +89,11 @@ public class Concat extends Task implements ResourceCollection {
* sub element points to a file or contains text
*/
public static class TextElement extends ProjectComponent {
private String value = "";
private boolean trimLeading = false;
private boolean trim = false;
private boolean filtering = true;
private String encoding = null;
private String value = "";
private boolean trimLeading = false;
private boolean trim = false;
private boolean filtering = true;
private CharSet charSet = CharSet.getDefault();

/**
* whether to filter the text in this element
Expand All @@ -117,7 +117,16 @@ private boolean getFiltering() {
* @param encoding the name of the charset used to encode
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
this.charSet = new CharSet(encoding);
}

/**
* The CharSet of the text element
*
* @param charSet the name of the charset used to encode
*/
public void setEncoding(CharSet charSet) {
this.charSet = charSet;
}

/**
Expand All @@ -132,20 +141,11 @@ public void setFile(File file) throws BuildException {
throw new BuildException("File %s does not exist.", file);
}

BufferedReader reader = null;
try {
if (this.encoding == null) {
reader = new BufferedReader(new FileReader(file));
} else {
reader = new BufferedReader(
new InputStreamReader(Files.newInputStream(file.toPath()),
this.encoding));
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
Files.newInputStream(file.toPath()), charSet.getCharset()))) {
value = FileUtils.safeReadFully(reader);
} catch (IOException ex) {
throw new BuildException(ex);
} finally {
FileUtils.close(reader);
}
}

Expand Down Expand Up @@ -418,8 +418,7 @@ public InputStream getInputStream() {
rdr = new MultiReader<>(Arrays.asList(readers).iterator(),
identityReaderFactory);
}
return outputEncoding == null ? new ReaderInputStream(rdr)
: new ReaderInputStream(rdr, outputEncoding);
return new ReaderInputStream(rdr, outputCharSet.getCharset());
}
@Override
public String getName() {
Expand All @@ -444,12 +443,18 @@ public String getName() {
private boolean append;

/**
* Stores the input file encoding.
* Stores the input file CharSet.
*/
private String encoding;
private CharSet charSet = CharSet.getDefault();

/** Indicates if attribute is set explicitly */
private boolean hasCharSet = false;

/** Stores the output file CharSet. */
private CharSet outputCharSet = CharSet.getDefault();

/** Stores the output file encoding. */
private String outputEncoding;
/** Indicates if attribute is set explicitly */
private boolean hasOutputCharSet = false;

/** Stores the binary attribute */
private boolean binary;
Expand Down Expand Up @@ -489,15 +494,9 @@ public String getName() {
/** exposed resource name */
private String resourceName;

private ReaderFactory<Resource> resourceReaderFactory = new ReaderFactory<Resource>() {
@Override
public Reader getReader(Resource o) throws IOException {
InputStream is = o.getInputStream();
return new BufferedReader(encoding == null
? new InputStreamReader(is)
: new InputStreamReader(is, encoding));
}
};
private ReaderFactory<Resource> resourceReaderFactory =
o -> new BufferedReader(new InputStreamReader(o.getInputStream(),
charSet.getCharset()));

private ReaderFactory<Reader> identityReaderFactory = o -> o;

Expand All @@ -515,8 +514,10 @@ public void reset() {
append = false;
forceOverwrite = true;
dest = null;
encoding = null;
outputEncoding = null;
charSet = CharSet.getDefault();
hasCharSet = false;
outputCharSet = CharSet.getDefault();
hasOutputCharSet = false;
fixLastLine = false;
filterChains = null;
footer = null;
Expand Down Expand Up @@ -566,10 +567,7 @@ public void setAppend(boolean append) {
* outputencoding is set, the outputstream.
*/
public void setEncoding(String encoding) {
this.encoding = encoding;
if (outputEncoding == null) {
outputEncoding = encoding;
}
setCharSet(new CharSet(encoding));
}

/**
Expand All @@ -578,7 +576,30 @@ public void setEncoding(String encoding) {
* @since Ant 1.6
*/
public void setOutputEncoding(String outputEncoding) {
this.outputEncoding = outputEncoding;
setOutputCharSet(new CharSet(outputEncoding));
}


/**
* Sets the charset
* @param charSet the charset of the input stream and unless
* outputcharset is set, the outputstream.
*/
public void setCharSet(CharSet charSet) {
this.charSet = charSet;
hasCharSet = true;
if (!hasOutputCharSet) {
outputCharSet = charSet;
}
}

/**
* Sets the charset for outputting
* @param outputCharSet the charset for the output file
*/
public void setOutputCharSet(CharSet outputCharSet) {
this.outputCharSet = outputCharSet;
hasOutputCharSet = true;
}

/**
Expand Down Expand Up @@ -802,8 +823,8 @@ public void execute() {
ResourceUtils.copyResource(new ConcatResource(c), dest == null
? new LogOutputResource(this, Project.MSG_WARN)
: dest,
null, null, true, false, append, null,
null, getProject(), force);
null, null, true, false, append, CharSet.getDefault(),
CharSet.getDefault(), getProject(), force);
} catch (IOException e) {
throw new BuildException("error concatenating content to " + dest, e);
}
Expand Down Expand Up @@ -853,7 +874,7 @@ private void validate() {
throw new BuildException(
"Nested text is incompatible with binary concatenation");
}
if (encoding != null || outputEncoding != null) {
if (hasCharSet || hasOutputCharSet) {
throw new BuildException(
"Setting input or output encoding is incompatible with binary concatenation");
}
Expand Down
Loading