@@ -19,8 +19,11 @@ package org.apache.tools.ant.types;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@@ -41,8 +44,8 @@ import org.apache.tools.ant.PropertyHelper;
public class PatternSet extends DataType implements Cloneable {
private List<NameEntry> includeList = new ArrayList<>();
private List<NameEntry> excludeList = new ArrayList<>();
private List<NameEntry> includesFileList = new ArrayList<>();
private List<NameEntry> excludesFileList = new ArrayList<>();
private List<PatternFile NameEntry> includesFileList = new ArrayList<>();
private List<PatternFile NameEntry> excludesFileList = new ArrayList<>();
/**
* inner class to hold a name on list. "If" and "Unless" attributes
@@ -178,6 +181,45 @@ public class PatternSet extends DataType implements Cloneable {
}
}
/**
* Adds encoding support to {@link NameEntry}.
* @since Ant 1.10.4
*/
public class PatternFileNameEntry extends NameEntry {
private String encoding;
/**
* Encoding to use when reading the file, defaults to the platform's default
* encoding.
*
* <p>
* For a list of possible values see
* <a href="https://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html">
* https://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html</a>.
* </p>
*
* @param encoding String
*/
public final void setEncoding(String encoding) {
this.encoding = encoding;
}
/**
* Encoding to use when reading the file, defaults to the platform's default
* encoding.
*/
public final String getEncoding() {
return encoding;
}
@Override
public String toString() {
String baseString = super.toString();
return encoding == null ? baseString
: new StringBuilder(baseString).append(";encoding->").append(encoding).toString();
}
}
private static final class InvertedPatternSet extends PatternSet {
private InvertedPatternSet(PatternSet p) {
setProject(p.getProject());
@@ -260,7 +302,7 @@ public class PatternSet extends DataType implements Cloneable {
if (isReference()) {
throw noChildrenAllowed();
}
return addPatternToList(includesFileList);
return addPatternFile ToList(includesFileList);
}
/**
@@ -282,7 +324,7 @@ public class PatternSet extends DataType implements Cloneable {
if (isReference()) {
throw noChildrenAllowed();
}
return addPatternToList(excludesFileList);
return addPatternFile ToList(excludesFileList);
}
/**
@@ -330,6 +372,15 @@ public class PatternSet extends DataType implements Cloneable {
return result;
}
/**
* add a pattern file name entry to the given list
*/
private PatternFileNameEntry addPatternFileToList(List<PatternFileNameEntry> list) {
PatternFileNameEntry result = new PatternFileNameEntry();
list.add(result);
return result;
}
/**
* Sets the name of the file containing the includes patterns.
*
@@ -360,11 +411,12 @@ public class PatternSet extends DataType implements Cloneable {
* Reads path matching patterns from a file and adds them to the
* includes or excludes list (as appropriate).
*/
private void readPatterns(File patternfile, List<NameEntry> patternlist, Project p)
private void readPatterns(File patternfile, String encoding, List<NameEntry> patternlist, Project p)
throws BuildException {
try (BufferedReader patternReader =
new BufferedReader(new FileReader(patternfile))) {
try (Reader r = encoding == null ? new FileReader(patternfile)
: new InputStreamReader(new FileInputStream(patternfile), encoding);
BufferedReader patternReader = new BufferedReader(r)) {
// Create one NameEntry in the appropriate pattern list for each
// line in the file.
@@ -470,7 +522,7 @@ public class PatternSet extends DataType implements Cloneable {
*/
private void readFiles(Project p) {
if (!includesFileList.isEmpty()) {
for (NameEntry ne : includesFileList) {
for (PatternFile NameEntry ne : includesFileList) {
String fileName = ne.evalName(p);
if (fileName != null) {
File inclFile = p.resolveFile(fileName);
@@ -478,13 +530,13 @@ public class PatternSet extends DataType implements Cloneable {
throw new BuildException("Includesfile " + inclFile.getAbsolutePath()
+ " not found.");
}
readPatterns(inclFile, includeList, p);
readPatterns(inclFile, ne.getEncoding(), includeList, p);
}
}
includesFileList.clear();
}
if (!excludesFileList.isEmpty()) {
for (NameEntry ne : excludesFileList) {
for (PatternFile NameEntry ne : excludesFileList) {
String fileName = ne.evalName(p);
if (fileName != null) {
File exclFile = p.resolveFile(fileName);
@@ -492,7 +544,7 @@ public class PatternSet extends DataType implements Cloneable {
throw new BuildException("Excludesfile " + exclFile.getAbsolutePath()
+ " not found.");
}
readPatterns(exclFile, excludeList, p);
readPatterns(exclFile, ne.getEncoding(), excludeList, p);
}
}
excludesFileList.clear();