Skip to content

Commit 6cab4a1

Browse files
committed
Retrofit Ant with CharSet
1 parent 9037e93 commit 6cab4a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1708
-683
lines changed

src/main/org/apache/tools/ant/listener/MailLogger.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.apache.tools.ant.taskdefs.email.EmailAddress;
3737
import org.apache.tools.ant.taskdefs.email.Mailer;
3838
import org.apache.tools.ant.taskdefs.email.Message;
39+
import org.apache.tools.ant.types.CharSet;
3940
import org.apache.tools.ant.util.ClasspathUtils;
4041
import org.apache.tools.ant.util.DateUtils;
4142
import org.apache.tools.ant.util.FileUtils;
@@ -96,6 +97,7 @@
9697
*/
9798
public class MailLogger extends DefaultLogger {
9899
private static final String DEFAULT_MIME_TYPE = "text/plain";
100+
private static final String DEFAULT_CHARSET_NAME = "default";
99101

100102
/** Buffer in which the message is constructed prior to sending */
101103
private StringBuffer buffer = new StringBuffer();
@@ -159,7 +161,7 @@ public void buildFinished(BuildEvent event) {
159161
.toCcList(getValue(properties, prefix + ".cc", ""))
160162
.toBccList(getValue(properties, prefix + ".bcc", ""))
161163
.mimeType(getValue(properties, "mimeType", DEFAULT_MIME_TYPE))
162-
.charset(getValue(properties, "charset", ""))
164+
.charSet(new CharSet(getValue(properties, "charset", DEFAULT_CHARSET_NAME)))
163165
.body(getValue(properties, prefix + ".body", ""))
164166
.subject(getValue(
165167
properties, prefix + ".subject",
@@ -267,12 +269,19 @@ public Values subject(String subject) {
267269
this.subject = subject;
268270
return this;
269271
}
270-
private String charset;
272+
private CharSet charSet;
271273
public String charset() {
272-
return charset;
274+
return charSet.getValue();
275+
}
276+
public CharSet charSet() {
277+
return charSet;
273278
}
274279
public Values charset(String charset) {
275-
this.charset = charset;
280+
this.charSet = new CharSet(charset);
281+
return this;
282+
}
283+
public Values charSet(CharSet charSet) {
284+
this.charSet = charSet;
276285
return this;
277286
}
278287
private String mimeType;
@@ -365,7 +374,7 @@ private void sendMail(Values values, String message) throws IOException {
365374

366375
mailMessage.setSubject(values.subject());
367376

368-
if (values.charset().isEmpty()) {
377+
if (DEFAULT_CHARSET_NAME.equals(values.charset())) {
369378
mailMessage.setHeader("Content-Type", values.mimeType());
370379
} else {
371380
mailMessage.setHeader("Content-Type", values.mimeType()
@@ -406,8 +415,8 @@ private void sendMimeMail(Project project, Values values, String message) {
406415
new Message(!values.body().isEmpty() ? values.body() : message);
407416
mymessage.setProject(project);
408417
mymessage.setMimeType(values.mimeType());
409-
if (!values.charset().isEmpty()) {
410-
mymessage.setCharset(values.charset());
418+
if (!DEFAULT_CHARSET_NAME.equals(values.charset())) {
419+
mymessage.setCharSet(values.charSet());
411420
}
412421
mailer.setMessage(mymessage);
413422
mailer.setFrom(new EmailAddress(values.from()));

src/main/org/apache/tools/ant/taskdefs/Concat.java

+64-43
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.io.BufferedReader;
2121
import java.io.File;
22-
import java.io.FileReader;
2322
import java.io.IOException;
2423
import java.io.InputStream;
2524
import java.io.InputStreamReader;
@@ -37,6 +36,7 @@
3736
import org.apache.tools.ant.ProjectComponent;
3837
import org.apache.tools.ant.Task;
3938
import org.apache.tools.ant.filters.util.ChainReaderHelper;
39+
import org.apache.tools.ant.types.CharSet;
4040
import org.apache.tools.ant.types.FileList;
4141
import org.apache.tools.ant.types.FileSet;
4242
import org.apache.tools.ant.types.FilterChain;
@@ -89,11 +89,11 @@ public class Concat extends Task implements ResourceCollection {
8989
* sub element points to a file or contains text
9090
*/
9191
public static class TextElement extends ProjectComponent {
92-
private String value = "";
93-
private boolean trimLeading = false;
94-
private boolean trim = false;
95-
private boolean filtering = true;
96-
private String encoding = null;
92+
private String value = "";
93+
private boolean trimLeading = false;
94+
private boolean trim = false;
95+
private boolean filtering = true;
96+
private CharSet charSet = CharSet.getDefault();
9797

9898
/**
9999
* whether to filter the text in this element
@@ -117,7 +117,16 @@ private boolean getFiltering() {
117117
* @param encoding the name of the charset used to encode
118118
*/
119119
public void setEncoding(String encoding) {
120-
this.encoding = encoding;
120+
this.charSet = new CharSet(encoding);
121+
}
122+
123+
/**
124+
* The CharSet of the text element
125+
*
126+
* @param charSet the name of the charset used to encode
127+
*/
128+
public void setEncoding(CharSet charSet) {
129+
this.charSet = charSet;
121130
}
122131

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

135-
BufferedReader reader = null;
136-
try {
137-
if (this.encoding == null) {
138-
reader = new BufferedReader(new FileReader(file));
139-
} else {
140-
reader = new BufferedReader(
141-
new InputStreamReader(Files.newInputStream(file.toPath()),
142-
this.encoding));
143-
}
144+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
145+
Files.newInputStream(file.toPath()), charSet.getCharset()))) {
144146
value = FileUtils.safeReadFully(reader);
145147
} catch (IOException ex) {
146148
throw new BuildException(ex);
147-
} finally {
148-
FileUtils.close(reader);
149149
}
150150
}
151151

@@ -418,8 +418,7 @@ public InputStream getInputStream() {
418418
rdr = new MultiReader<>(Arrays.asList(readers).iterator(),
419419
identityReaderFactory);
420420
}
421-
return outputEncoding == null ? new ReaderInputStream(rdr)
422-
: new ReaderInputStream(rdr, outputEncoding);
421+
return new ReaderInputStream(rdr, outputCharSet.getCharset());
423422
}
424423
@Override
425424
public String getName() {
@@ -444,12 +443,18 @@ public String getName() {
444443
private boolean append;
445444

446445
/**
447-
* Stores the input file encoding.
446+
* Stores the input file CharSet.
448447
*/
449-
private String encoding;
448+
private CharSet charSet = CharSet.getDefault();
449+
450+
/** Indicates if attribute is set explicitly */
451+
private boolean hasCharSet = false;
452+
453+
/** Stores the output file CharSet. */
454+
private CharSet outputCharSet = CharSet.getDefault();
450455

451-
/** Stores the output file encoding. */
452-
private String outputEncoding;
456+
/** Indicates if attribute is set explicitly */
457+
private boolean hasOutputCharSet = false;
453458

454459
/** Stores the binary attribute */
455460
private boolean binary;
@@ -489,15 +494,9 @@ public String getName() {
489494
/** exposed resource name */
490495
private String resourceName;
491496

492-
private ReaderFactory<Resource> resourceReaderFactory = new ReaderFactory<Resource>() {
493-
@Override
494-
public Reader getReader(Resource o) throws IOException {
495-
InputStream is = o.getInputStream();
496-
return new BufferedReader(encoding == null
497-
? new InputStreamReader(is)
498-
: new InputStreamReader(is, encoding));
499-
}
500-
};
497+
private ReaderFactory<Resource> resourceReaderFactory =
498+
o -> new BufferedReader(new InputStreamReader(o.getInputStream(),
499+
charSet.getCharset()));
501500

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

@@ -515,8 +514,10 @@ public void reset() {
515514
append = false;
516515
forceOverwrite = true;
517516
dest = null;
518-
encoding = null;
519-
outputEncoding = null;
517+
charSet = CharSet.getDefault();
518+
hasCharSet = false;
519+
outputCharSet = CharSet.getDefault();
520+
hasOutputCharSet = false;
520521
fixLastLine = false;
521522
filterChains = null;
522523
footer = null;
@@ -566,10 +567,7 @@ public void setAppend(boolean append) {
566567
* outputencoding is set, the outputstream.
567568
*/
568569
public void setEncoding(String encoding) {
569-
this.encoding = encoding;
570-
if (outputEncoding == null) {
571-
outputEncoding = encoding;
572-
}
570+
setCharSet(new CharSet(encoding));
573571
}
574572

575573
/**
@@ -578,7 +576,30 @@ public void setEncoding(String encoding) {
578576
* @since Ant 1.6
579577
*/
580578
public void setOutputEncoding(String outputEncoding) {
581-
this.outputEncoding = outputEncoding;
579+
setOutputCharSet(new CharSet(outputEncoding));
580+
}
581+
582+
583+
/**
584+
* Sets the charset
585+
* @param charSet the charset of the input stream and unless
586+
* outputcharset is set, the outputstream.
587+
*/
588+
public void setCharSet(CharSet charSet) {
589+
this.charSet = charSet;
590+
hasCharSet = true;
591+
if (!hasOutputCharSet) {
592+
outputCharSet = charSet;
593+
}
594+
}
595+
596+
/**
597+
* Sets the charset for outputting
598+
* @param outputCharSet the charset for the output file
599+
*/
600+
public void setOutputCharSet(CharSet outputCharSet) {
601+
this.outputCharSet = outputCharSet;
602+
hasOutputCharSet = true;
582603
}
583604

584605
/**
@@ -802,8 +823,8 @@ public void execute() {
802823
ResourceUtils.copyResource(new ConcatResource(c), dest == null
803824
? new LogOutputResource(this, Project.MSG_WARN)
804825
: dest,
805-
null, null, true, false, append, null,
806-
null, getProject(), force);
826+
null, null, true, false, append, CharSet.getDefault(),
827+
CharSet.getDefault(), getProject(), force);
807828
} catch (IOException e) {
808829
throw new BuildException("error concatenating content to " + dest, e);
809830
}
@@ -853,7 +874,7 @@ private void validate() {
853874
throw new BuildException(
854875
"Nested text is incompatible with binary concatenation");
855876
}
856-
if (encoding != null || outputEncoding != null) {
877+
if (hasCharSet || hasOutputCharSet) {
857878
throw new BuildException(
858879
"Setting input or output encoding is incompatible with binary concatenation");
859880
}

0 commit comments

Comments
 (0)