@@ -19,8 +19,10 @@ package org.apache.tools.ant.types;
import java.io.BufferedReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
import java.util.StringTokenizer;
import java.util.StringTokenizer;
@@ -40,8 +42,8 @@ import org.apache.tools.ant.util.FileUtils;
public class PatternSet extends DataType implements Cloneable {
public class PatternSet extends DataType implements Cloneable {
private List<NameEntry> includeList = new ArrayList<NameEntry>();
private List<NameEntry> includeList = new ArrayList<NameEntry>();
private List<NameEntry> excludeList = new ArrayList<NameEntry>();
private List<NameEntry> excludeList = new ArrayList<NameEntry>();
private List<NameEntry> includesFileList = new ArrayList<NameEntry>();
private List<NameEntry> excludesFileList = new ArrayList<NameEntry>();
private List<PatternFile NameEntry> includesFileList = new ArrayList<PatternFile NameEntry>();
private List<PatternFile NameEntry> excludesFileList = new ArrayList<PatternFile NameEntry>();
/**
/**
* inner class to hold a name on list. "If" and "Unless" attributes
* inner class to hold a name on list. "If" and "Unless" attributes
@@ -176,6 +178,45 @@ public class PatternSet extends DataType implements Cloneable {
}
}
}
}
/**
* Adds encoding support to {@link NameEntry}.
* @since Ant 1.9.12
*/
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 static final class InvertedPatternSet extends PatternSet {
private InvertedPatternSet(PatternSet p) {
private InvertedPatternSet(PatternSet p) {
setProject(p.getProject());
setProject(p.getProject());
@@ -255,7 +296,7 @@ public class PatternSet extends DataType implements Cloneable {
if (isReference()) {
if (isReference()) {
throw noChildrenAllowed();
throw noChildrenAllowed();
}
}
return addPatternToList(includesFileList);
return addPatternFile ToList(includesFileList);
}
}
/**
/**
@@ -277,7 +318,7 @@ public class PatternSet extends DataType implements Cloneable {
if (isReference()) {
if (isReference()) {
throw noChildrenAllowed();
throw noChildrenAllowed();
}
}
return addPatternToList(excludesFileList);
return addPatternFile ToList(excludesFileList);
}
}
/**
/**
@@ -325,6 +366,15 @@ public class PatternSet extends DataType implements Cloneable {
return result;
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.
* Sets the name of the file containing the includes patterns.
*
*
@@ -355,13 +405,17 @@ public class PatternSet extends DataType implements Cloneable {
* Reads path matching patterns from a file and adds them to the
* Reads path matching patterns from a file and adds them to the
* includes or excludes list (as appropriate).
* 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 {
throws BuildException {
BufferedReader patternReader = null;
BufferedReader patternReader = null;
try {
try {
// Get a FileReader
// Get a FileReader
patternReader = new BufferedReader(new FileReader(patternfile));
if (encoding == null) {
patternReader = new BufferedReader(new FileReader(patternfile));
} else {
patternReader = new BufferedReader(new InputStreamReader(new FileInputStream(patternfile), encoding));
}
// Create one NameEntry in the appropriate pattern list for each
// Create one NameEntry in the appropriate pattern list for each
// line in the file.
// line in the file.
@@ -478,7 +532,7 @@ public class PatternSet extends DataType implements Cloneable {
*/
*/
private void readFiles(Project p) {
private void readFiles(Project p) {
if (includesFileList.size() > 0) {
if (includesFileList.size() > 0) {
for (NameEntry ne : includesFileList) {
for (PatternFile NameEntry ne : includesFileList) {
String fileName = ne.evalName(p);
String fileName = ne.evalName(p);
if (fileName != null) {
if (fileName != null) {
File inclFile = p.resolveFile(fileName);
File inclFile = p.resolveFile(fileName);
@@ -486,13 +540,13 @@ public class PatternSet extends DataType implements Cloneable {
throw new BuildException("Includesfile " + inclFile.getAbsolutePath()
throw new BuildException("Includesfile " + inclFile.getAbsolutePath()
+ " not found.");
+ " not found.");
}
}
readPatterns(inclFile, includeList, p);
readPatterns(inclFile, ne.getEncoding(), includeList, p);
}
}
}
}
includesFileList.clear();
includesFileList.clear();
}
}
if (excludesFileList.size() > 0) {
if (excludesFileList.size() > 0) {
for (NameEntry ne : excludesFileList) {
for (PatternFile NameEntry ne : excludesFileList) {
String fileName = ne.evalName(p);
String fileName = ne.evalName(p);
if (fileName != null) {
if (fileName != null) {
File exclFile = p.resolveFile(fileName);
File exclFile = p.resolveFile(fileName);
@@ -500,7 +554,7 @@ public class PatternSet extends DataType implements Cloneable {
throw new BuildException("Excludesfile " + exclFile.getAbsolutePath()
throw new BuildException("Excludesfile " + exclFile.getAbsolutePath()
+ " not found.");
+ " not found.");
}
}
readPatterns(exclFile, excludeList, p);
readPatterns(exclFile, ne.getEncoding(), excludeList, p);
}
}
}
}
excludesFileList.clear();
excludesFileList.clear();
@@ -523,8 +577,8 @@ public class PatternSet extends DataType implements Cloneable {
PatternSet ps = (PatternSet) super.clone();
PatternSet ps = (PatternSet) super.clone();
ps.includeList = new ArrayList<NameEntry>(includeList);
ps.includeList = new ArrayList<NameEntry>(includeList);
ps.excludeList = new ArrayList<NameEntry>(excludeList);
ps.excludeList = new ArrayList<NameEntry>(excludeList);
ps.includesFileList = new ArrayList<NameEntry>(includesFileList);
ps.excludesFileList = new ArrayList<NameEntry>(excludesFileList);
ps.includesFileList = new ArrayList<PatternFile NameEntry>(includesFileList);
ps.excludesFileList = new ArrayList<PatternFile NameEntry>(excludesFileList);
return ps;
return ps;
} catch (CloneNotSupportedException e) {
} catch (CloneNotSupportedException e) {
throw new BuildException(e);
throw new BuildException(e);