diff --git a/WHATSNEW b/WHATSNEW
index 5bbd50327..63bb359c4 100644
--- a/WHATSNEW
+++ b/WHATSNEW
@@ -60,6 +60,16 @@ Other changes:
strings to be present in the line.
Bugzilla Report 62313
+ *
patternset
Patternsets may be nested within one another, adding the nested patterns to the parent
diff --git a/manual/Types/resources.html b/manual/Types/resources.html
index 160c96f65..b07f73be5 100644
--- a/manual/Types/resources.html
+++ b/manual/Types/resources.html
@@ -1249,6 +1249,15 @@ of <filelist>
.
PatternSet.NameEntry
.
+ * @return PatternSet.PatternFileNameEntry
.
*/
public synchronized PatternSet.NameEntry createIncludesFile() {
if (isReference()) {
@@ -221,7 +221,7 @@ public abstract class AbstractFileSet extends DataType
/**
* Add a name entry to the excludes files list.
- * @return PatternSet.NameEntry
.
+ * @return PatternSet.PatternFileNameEntry
.
*/
public synchronized PatternSet.NameEntry createExcludesFile() {
if (isReference()) {
diff --git a/src/main/org/apache/tools/ant/types/PatternSet.java b/src/main/org/apache/tools/ant/types/PatternSet.java
index 8a833c87f..cc92a3e28 100644
--- a/src/main/org/apache/tools/ant/types/PatternSet.java
+++ b/src/main/org/apache/tools/ant/types/PatternSet.java
@@ -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+ * For a list of possible values see + * + * https://docs.oracle.com/javase/1.5.0/docs/guide/intl/encoding.doc.html. + *
+ * + * @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 addPatternFileToList(includesFileList); } /** @@ -282,7 +324,7 @@ public class PatternSet extends DataType implements Cloneable { if (isReference()) { throw noChildrenAllowed(); } - return addPatternToList(excludesFileList); + return addPatternFileToList(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(ListPatternSet.NameEntry
.
+ * @return PatternSet.PatternFileNameEntry
.
*/
public synchronized PatternSet.NameEntry createIncludesFile() {
if (isReference()) {
@@ -147,7 +147,7 @@ public class Files extends AbstractSelectorContainer
/**
* Add a name entry to the excludes files list.
- * @return PatternSet.NameEntry
.
+ * @return PatternSet.PatternFileNameEntry
.
*/
public synchronized PatternSet.NameEntry createExcludesFile() {
if (isReference()) {
diff --git a/src/main/org/apache/tools/ant/types/resources/ResourceList.java b/src/main/org/apache/tools/ant/types/resources/ResourceList.java
index 3de844990..9a5fef00f 100644
--- a/src/main/org/apache/tools/ant/types/resources/ResourceList.java
+++ b/src/main/org/apache/tools/ant/types/resources/ResourceList.java
@@ -19,6 +19,7 @@ package org.apache.tools.ant.types.resources;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
+import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
@@ -49,6 +50,7 @@ public class ResourceList extends DataType implements ResourceCollection {
private final Union cachedResources = new Union();
private volatile boolean cached = false;
private String encoding = null;
+ private File baseDir;
public ResourceList() {
cachedResources.setCache(true);
@@ -99,6 +101,21 @@ public class ResourceList extends DataType implements ResourceCollection {
this.encoding = encoding;
}
+ /**
+ * Basedir to use for file resources read from nested resources -
+ * this allows the resources contained inside this collection to
+ * be considered relative to a certain base directory.
+ *
+ * @param basedir the basedir
+ * @since Ant 1.10.4
+ */
+ public final void setBasedir(File baseDir) {
+ if (isReference()) {
+ throw tooManyAttributes();
+ }
+ this.baseDir = baseDir;
+ }
+
/**
* Makes this instance in effect a reference to another ResourceList
* instance.
@@ -242,6 +259,11 @@ public class ResourceList extends DataType implements ResourceCollection {
// resource
}
}
+ if (baseDir != null) {
+ FileResource fr = new FileResource(baseDir, expandedLine);
+ fr.setProject(getProject());
+ return fr;
+ }
return new FileResource(getProject(), expandedLine);
}
diff --git a/src/tests/antunit/taskdefs/copy-test.xml b/src/tests/antunit/taskdefs/copy-test.xml
index b26da75b7..d0fb49d87 100644
--- a/src/tests/antunit/taskdefs/copy-test.xml
+++ b/src/tests/antunit/taskdefs/copy-test.xml
@@ -476,4 +476,22 @@ public class NullByteStreamResource extends Resource {
This doesn't actually test much, mainly reference handling.
*/ public class FileSetTest extends AbstractFileSetTest { + @Rule + public BuildFileRule buildRule = new BuildFileRule(); + + @Before + public void buildFileRuleSetUp() { + buildRule.configureProject("src/etc/testcases/types/fileset.xml"); + } + protected AbstractFileSet getInstance() { return new FileSet(); } + @Test + public void testNoEncoding() { + buildRule.executeTarget("no-encoding"); + assertEquals("/abc/fileset.xml", buildRule.getLog()); + } + + @Test + public void testEncoding() { + buildRule.executeTarget("encoding"); + assertEquals("/abc/fileset.xml", buildRule.getLog()); + } + } diff --git a/src/tests/junit/org/apache/tools/ant/types/PatternSetTest.java b/src/tests/junit/org/apache/tools/ant/types/PatternSetTest.java index 1cd61e4fe..031c7abad 100644 --- a/src/tests/junit/org/apache/tools/ant/types/PatternSetTest.java +++ b/src/tests/junit/org/apache/tools/ant/types/PatternSetTest.java @@ -20,13 +20,20 @@ package org.apache.tools.ant.types; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; +import org.apache.tools.ant.util.FileUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; /** @@ -211,4 +218,26 @@ public class PatternSetTest { assertEquals("Includes", "**/*.java", includes[0]); assertEquals("Excludes", "**/*.class", excludes[0]); } + + @Test + public void testEncodingOfIncludesFile() throws IOException { + File testFile = File.createTempFile("ant-", ".pattern"); + testFile.deleteOnExit(); + OutputStream o = null; + Writer w = null; + try { + o = new FileOutputStream(testFile); + w = new OutputStreamWriter(o, "UTF-16LE"); + w.write("\u00e4\n"); + } finally { + FileUtils.close(w); + FileUtils.close(o); + } + PatternSet p = new PatternSet(); + PatternSet.PatternFileNameEntry ne = + (PatternSet.PatternFileNameEntry) p.createIncludesFile(); + ne.setName(testFile.getAbsolutePath()); + ne.setEncoding("UTF-16LE"); + assertArrayEquals(new String[] { "\u00e4" }, p.getIncludePatterns(project)); + } }