Browse Source

reimplement DirSet as subclass of FileSet

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272182 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 23 years ago
parent
commit
af0dbb6ef2
4 changed files with 69 additions and 250 deletions
  1. +7
    -226
      src/main/org/apache/tools/ant/types/DirSet.java
  2. +14
    -4
      src/main/org/apache/tools/ant/types/FileSet.java
  3. +20
    -20
      src/main/org/apache/tools/ant/types/Path.java
  4. +28
    -0
      src/testcases/org/apache/tools/ant/types/DirSetTest.java

+ 7
- 226
src/main/org/apache/tools/ant/types/DirSet.java View File

@@ -64,238 +64,19 @@ import java.util.Stack;
import java.util.Vector; import java.util.Vector;


/** /**
* Essentially a clone of FileSet, but gets a set of dirs instead of files.
* Subclass as hint for supporting tasks that the included directories
* instead of files should be used.
*/ */
public class DirSet extends DataType implements Cloneable {
public class DirSet extends FileSet {
private PatternSet defaultPatterns = new PatternSet();
private Vector additionalPatterns = new Vector();

private File dir;
private boolean isCaseSensitive = true;

public DirSet() { public DirSet() {
super(); super();
setDataTypeName("dirset");
} }


protected DirSet(DirSet dirset) { protected DirSet(DirSet dirset) {
this.dir = dirset.dir;
this.defaultPatterns = dirset.defaultPatterns;
this.additionalPatterns = dirset.additionalPatterns;
this.isCaseSensitive = dirset.isCaseSensitive;
setProject(getProject());
}

/**
* Makes this instance in effect a reference to another PatternSet
* instance.
*
* <p>You must not set another attribute or nest elements inside
* this element if you make it a reference.</p>
*/
public void setRefid(Reference r) throws BuildException {
if (dir != null || defaultPatterns.hasPatterns()) {
throw tooManyAttributes();
}
if (!additionalPatterns.isEmpty()) {
throw noChildrenAllowed();
}
super.setRefid(r);
}

public void setDir(File dir) throws BuildException {
if (isReference()) {
throw tooManyAttributes();
}

this.dir = dir;
}

public File getDir(Project p) {
if (isReference()) {
return getRef(p).getDir(p);
}
return dir;
}

public PatternSet createPatternSet() {
if (isReference()) {
throw noChildrenAllowed();
}
PatternSet patterns = new PatternSet();
additionalPatterns.addElement(patterns);
return patterns;
}

/**
* add a name entry on the include list
*/
public PatternSet.NameEntry createInclude() {
if (isReference()) {
throw noChildrenAllowed();
}
return defaultPatterns.createInclude();
}
/**
* add a name entry on the include files list
*/
public PatternSet.NameEntry createIncludesFile() {
if (isReference()) {
throw noChildrenAllowed();
}
return defaultPatterns.createIncludesFile();
}
/**
* add a name entry on the exclude list
*/
public PatternSet.NameEntry createExclude() {
if (isReference()) {
throw noChildrenAllowed();
}
return defaultPatterns.createExclude();
}

/**
* add a name entry on the include files list
*/
public PatternSet.NameEntry createExcludesFile() {
if (isReference()) {
throw noChildrenAllowed();
}
return defaultPatterns.createExcludesFile();
}
/**
* Sets the set of include patterns. Patterns may be separated by a comma
* or a space.
*
* @param includes the string containing the include patterns
*/
public void setIncludes(String includes) {
if (isReference()) {
throw tooManyAttributes();
}

defaultPatterns.setIncludes(includes);
}

/**
* Sets the set of exclude patterns. Patterns may be separated by a comma
* or a space.
*
* @param excludes the string containing the exclude patterns
*/
public void setExcludes(String excludes) {
if (isReference()) {
throw tooManyAttributes();
}

defaultPatterns.setExcludes(excludes);
}

/**
* Sets the name of the file containing the includes patterns.
*
* @param incl The file to fetch the include patterns from.
*/
public void setIncludesfile(File incl) throws BuildException {
if (isReference()) {
throw tooManyAttributes();
}

defaultPatterns.setIncludesfile(incl);
}

/**
* Sets the name of the file containing the includes patterns.
*
* @param excl The file to fetch the exclude patterns from.
*/
public void setExcludesfile(File excl) throws BuildException {
if (isReference()) {
throw tooManyAttributes();
}

defaultPatterns.setExcludesfile(excl);
}

/**
* Sets case sensitivity of the file system
*
* @param isCaseSensitive "true"|"on"|"yes" if file system is case
* sensitive, "false"|"off"|"no" when not.
*/
public void setCaseSensitive(boolean isCaseSensitive) {
this.isCaseSensitive = isCaseSensitive;
}

/**
* Returns the directory scanner needed to access the dirs to process.
*/
public DirectoryScanner getDirectoryScanner(Project p) {
if (isReference()) {
return getRef(p).getDirectoryScanner(p);
}

if (dir == null) {
throw new BuildException("No directory specified for dirset.");
}

if (!dir.exists()) {
throw new BuildException(dir.getAbsolutePath()+" not found.");
}
if (!dir.isDirectory()) {
throw new BuildException(dir.getAbsolutePath()+" is not a directory.");
}

DirectoryScanner ds = new DirectoryScanner();
setupDirectoryScanner(ds, p);
ds.scan();
ds.getIncludedDirectories();
return ds;
}
public void setupDirectoryScanner(FileScanner ds, Project p) {
if (ds == null) {
throw new IllegalArgumentException("ds cannot be null");
}
ds.setBasedir(dir);

final int count = additionalPatterns.size();
for (int i = 0; i < count; i++) {
Object o = additionalPatterns.elementAt(i);
defaultPatterns.append((PatternSet) o, p);
}

p.log( "DirSet: Setup dir scanner in dir " + dir +
" with " + defaultPatterns, Project.MSG_DEBUG );
ds.setIncludes(defaultPatterns.getIncludePatterns(p));
ds.setExcludes(defaultPatterns.getExcludePatterns(p));
ds.setCaseSensitive(isCaseSensitive);
}

/**
* Performs the check for circular references and returns the
* referenced DirSet.
*/
protected DirSet getRef(Project p) {
if (!checked) {
Stack stk = new Stack();
stk.push(this);
dieOnCircularReference(stk, p);
}
Object o = ref.getReferencedObject(p);
if (!(o instanceof DirSet)) {
String msg = ref.getRefId()+" doesn\'t denote a dirset";
throw new BuildException(msg);
} else {
return (DirSet) o;
}
super(dirset);
setDataTypeName("dirset");
} }


/** /**
@@ -304,7 +85,7 @@ public class DirSet extends DataType implements Cloneable {
*/ */
public Object clone() { public Object clone() {
if (isReference()) { if (isReference()) {
return new DirSet(getRef(getProject()));
return new DirSet((DirSet) getRef(getProject()));
} else { } else {
return new DirSet(this); return new DirSet(this);
} }


+ 14
- 4
src/main/org/apache/tools/ant/types/FileSet.java View File

@@ -83,6 +83,8 @@ public class FileSet extends DataType implements Cloneable {
private boolean useDefaultExcludes = true; private boolean useDefaultExcludes = true;
private boolean isCaseSensitive = true; private boolean isCaseSensitive = true;


private String dataTypeName = "fileset";

public FileSet() { public FileSet() {
super(); super();
} }
@@ -256,6 +258,13 @@ public class FileSet extends DataType implements Cloneable {
this.isCaseSensitive = isCaseSensitive; this.isCaseSensitive = isCaseSensitive;
} }


/**
* sets the name used for this datatype instance.
*/
protected final void setDataTypeName(String name) {
dataTypeName = name;
}

/** /**
* Returns the directory scanner needed to access the files to process. * Returns the directory scanner needed to access the files to process.
*/ */
@@ -265,7 +274,8 @@ public class FileSet extends DataType implements Cloneable {
} }


if (dir == null) { if (dir == null) {
throw new BuildException("No directory specified for fileset.");
throw new BuildException("No directory specified for "
+ dataTypeName + ".");
} }


if (!dir.exists()) { if (!dir.exists()) {
@@ -294,7 +304,7 @@ public class FileSet extends DataType implements Cloneable {
defaultPatterns.append((PatternSet) o, p); defaultPatterns.append((PatternSet) o, p);
} }


p.log( "FileSet: Setup file scanner in dir " + dir +
p.log(dataTypeName + ": Setup scanner in dir " + dir +
" with " + defaultPatterns, Project.MSG_DEBUG ); " with " + defaultPatterns, Project.MSG_DEBUG );
ds.setIncludes(defaultPatterns.getIncludePatterns(p)); ds.setIncludes(defaultPatterns.getIncludePatterns(p));
@@ -317,8 +327,8 @@ public class FileSet extends DataType implements Cloneable {
} }
Object o = ref.getReferencedObject(p); Object o = ref.getReferencedObject(p);
if (!(o instanceof FileSet)) {
String msg = ref.getRefId()+" doesn\'t denote a fileset";
if (!o.getClass().equals(getClass())) {
String msg = ref.getRefId()+" doesn\'t denote a " + dataTypeName;
throw new BuildException(msg); throw new BuildException(msg);
} else { } else {
return (FileSet) o; return (FileSet) o;


+ 20
- 20
src/main/org/apache/tools/ant/types/Path.java View File

@@ -319,35 +319,23 @@ public class Path extends DataType implements Cloneable {
for (int j=0; j<parts.length; j++) { for (int j=0; j<parts.length; j++) {
addUnlessPresent(result, parts[j]); addUnlessPresent(result, parts[j]);
} }
} else if (o instanceof FileSet) {
FileSet fs = (FileSet) o;
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
String[] s = ds.getIncludedFiles();
File dir = fs.getDir(getProject());
for (int j=0; j<s.length; j++) {
File f = new File(dir, s[j]);
String absolutePath = f.getAbsolutePath();
addUnlessPresent(result, translateFile(absolutePath));
}
} else if (o instanceof DirSet) { } else if (o instanceof DirSet) {
DirSet dset = (DirSet) o; DirSet dset = (DirSet) o;
DirectoryScanner ds = dset.getDirectoryScanner(getProject()); DirectoryScanner ds = dset.getDirectoryScanner(getProject());
String[] s = ds.getIncludedDirectories(); String[] s = ds.getIncludedDirectories();
File dir = dset.getDir(getProject()); File dir = dset.getDir(getProject());
for (int j=0; j<s.length; j++) {
File d = new File(dir, s[j]);
String absolutePath = d.getAbsolutePath();
addUnlessPresent(result, translateFile(absolutePath));
}
addUnlessPresent(result, dir, s);
} else if (o instanceof FileSet) {
FileSet fs = (FileSet) o;
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
String[] s = ds.getIncludedFiles();
File dir = fs.getDir(getProject());
addUnlessPresent(result, dir, s);
} else if (o instanceof FileList) { } else if (o instanceof FileList) {
FileList fl = (FileList) o; FileList fl = (FileList) o;
String[] s = fl.getFiles(getProject()); String[] s = fl.getFiles(getProject());
File dir = fl.getDir(getProject()); File dir = fl.getDir(getProject());
for (int j=0; j<s.length; j++) {
File d = new File(dir, s[j]);
String absolutePath = d.getAbsolutePath();
addUnlessPresent(result, translateFile(absolutePath));
}
addUnlessPresent(result, dir, s);
} }
} }
String[] res = new String[result.size()]; String[] res = new String[result.size()];
@@ -509,6 +497,18 @@ public class Path extends DataType implements Cloneable {
} }
} }


/**
* Adds absolute path names of listed files in the given directory
* to the Vector if they are not already included.
*/
private static void addUnlessPresent(Vector v, File dir, String[] s) {
for (int j=0; j<s.length; j++) {
File d = new File(dir, s[j]);
String absolutePath = d.getAbsolutePath();
addUnlessPresent(v, translateFile(absolutePath));
}
}

/** /**
* Concatenates the system class path in the order specified by * Concatenates the system class path in the order specified by
* the ${build.sysclasspath} property - using &quot;last&quot; as * the ${build.sysclasspath} property - using &quot;last&quot; as


+ 28
- 0
src/testcases/org/apache/tools/ant/types/DirSetTest.java View File

@@ -246,4 +246,32 @@ public class DirSetTest extends TestCase {
File dir = f1.getDir(project); File dir = f1.getDir(project);
assertEquals("Dir is basedir", dir, project.getBaseDir()); assertEquals("Dir is basedir", dir, project.getBaseDir());
} }

public void testFileSetIsNoDirSet() {
DirSet ds = new DirSet();
ds.setProject(project);
FileSet fs = new FileSet();
fs.setProject(project);
project.addReference("dummy", fs);
ds.setRefid(new Reference("dummy"));
try {
ds.getDir(project);
fail("DirSet created from FileSet reference");
} catch (BuildException e) {
assertEquals("dummy doesn\'t denote a dirset", e.getMessage());
}

ds = new DirSet();
ds.setProject(project);
project.addReference("dummy2", ds);
fs.setRefid(new Reference("dummy2"));
try {
fs.getDir(project);
fail("FileSet created from DirSet reference");
} catch (BuildException e) {
assertEquals("dummy2 doesn\'t denote a fileset", e.getMessage());
}

}

} }

Loading…
Cancel
Save