Browse Source

Added fileset and patternset types and made MatchingTask use them.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267802 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 25 years ago
parent
commit
1c3e443dd7
4 changed files with 473 additions and 132 deletions
  1. +2
    -1
      src/main/org/apache/tools/ant/taskdefs/CompileTask.java
  2. +18
    -131
      src/main/org/apache/tools/ant/taskdefs/MatchingTask.java
  3. +171
    -0
      src/main/org/apache/tools/ant/types/FileSet.java
  4. +282
    -0
      src/main/org/apache/tools/ant/types/PatternSet.java

+ 2
- 1
src/main/org/apache/tools/ant/taskdefs/CompileTask.java View File

@@ -55,6 +55,7 @@
package org.apache.tools.ant.taskdefs; package org.apache.tools.ant.taskdefs;


import org.apache.tools.ant.*; import org.apache.tools.ant.*;
import org.apache.tools.ant.types.PatternSet;


import java.util.*; import java.util.*;


@@ -88,7 +89,7 @@ public class CompileTask extends Javac {
for (Enumeration e=taskList.elements(); e.hasMoreElements(); ) { for (Enumeration e=taskList.elements(); e.hasMoreElements(); ) {
Taskdef task = (Taskdef)e.nextElement(); Taskdef task = (Taskdef)e.nextElement();
String source = task.getClassname().replace('.','/') + ".java"; String source = task.getClassname().replace('.','/') + ".java";
MatchingTask.NameEntry include = super.createInclude();
PatternSet.NameEntry include = super.createInclude();
include.setName("**/" + source); include.setName("**/" + source);
} }




+ 18
- 131
src/main/org/apache/tools/ant/taskdefs/MatchingTask.java View File

@@ -55,6 +55,7 @@
package org.apache.tools.ant.taskdefs; package org.apache.tools.ant.taskdefs;


import org.apache.tools.ant.*; import org.apache.tools.ant.*;
import org.apache.tools.ant.types.*;


import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
@@ -67,54 +68,26 @@ import java.util.*;
* @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a> * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a>
* @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a> * @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
* @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a> * @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
* @author <a href="mailto:stefan.bodewig@megabit.net">Stefan Bodewig</a>
*/ */


public abstract class MatchingTask extends Task { public abstract class MatchingTask extends Task {


protected Vector includeList = new Vector();
protected Vector excludeList = new Vector();
protected boolean useDefaultExcludes = true; protected boolean useDefaultExcludes = true;

/**
* provide access to properties from within the inner class
*/
protected String getProperty(String name) {
return project.getProperty(name);
}

/**
* inner class to hold a name on list. "If" and "Unless" attributes
* may be used to invalidate the entry based on the existence of a
* property (typically set thru the use of the Available task).
*/
public class NameEntry {
private boolean valid = true;
private String name;

public String getName() { return valid ? name : null; }
public void setName(String name) { this.name = name; }

public void setIf(String name) {
if (getProperty(name) == null) valid = false;
}

public void setUnless(String name) {
if (getProperty(name) != null) valid = false;
}
}
protected FileSet fileset = new FileSet();


/** /**
* add a name entry on the include list * add a name entry on the include list
*/ */
public NameEntry createInclude() {
return addPatternToList(includeList);
public PatternSet.NameEntry createInclude() {
return fileset.createInclude();
} }
/** /**
* add a name entry on the exclude list * add a name entry on the exclude list
*/ */
public NameEntry createExclude() {
return addPatternToList(excludeList);
public PatternSet.NameEntry createExclude() {
return fileset.createExclude();
} }


/** /**
@@ -124,9 +97,7 @@ public abstract class MatchingTask extends Task {
* @param includes the string containing the include patterns * @param includes the string containing the include patterns
*/ */
public void setIncludes(String includes) { public void setIncludes(String includes) {
if (includes != null && includes.length() > 0) {
createInclude().setName(includes);
}
fileset.setIncludes(includes);
} }


/** /**
@@ -161,9 +132,7 @@ public abstract class MatchingTask extends Task {
* @param excludes the string containing the exclude patterns * @param excludes the string containing the exclude patterns
*/ */
public void setExcludes(String excludes) { public void setExcludes(String excludes) {
if (excludes != null && excludes.length() > 0) {
createExclude().setName(excludes);
}
fileset.setExcludes(excludes);
} }


/** /**
@@ -192,83 +161,17 @@ public abstract class MatchingTask extends Task {
* should be used, "false"|"off"|"no" when they * should be used, "false"|"off"|"no" when they
* shouldn't be used. * shouldn't be used.
*/ */
public void setDefaultexcludes(String useDefaultExcludes) {
this.useDefaultExcludes = Project.toBoolean(useDefaultExcludes);
public void setDefaultexcludes(boolean useDefaultExcludes) {
this.useDefaultExcludes = useDefaultExcludes;
} }
/**
* Convert a vector of NameEntry elements into an array of Strings.
*/
private String[] makeArray(Vector list) {
if (list.size() == 0) return null;

Vector tmpNames = new Vector();
for (Enumeration e = list.elements() ; e.hasMoreElements() ;) {
String includes = ((NameEntry)e.nextElement()).getName();
if (includes == null) continue;
StringTokenizer tok = new StringTokenizer(includes, ", ", false);
while (tok.hasMoreTokens()) {
String pattern = tok.nextToken().trim();
if (pattern.length() > 0) {
tmpNames.addElement(pattern);
}
}
}

String result[] = new String[tmpNames.size()];
for (int i = 0; i < tmpNames.size(); i++) {
result[i] = (String)tmpNames.elementAt(i);
}

return result;
}
/** /**
* Returns the directory scanner needed to access the files to process. * Returns the directory scanner needed to access the files to process.
*/ */
protected DirectoryScanner getDirectoryScanner(File baseDir) { protected DirectoryScanner getDirectoryScanner(File baseDir) {
DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir(baseDir);
ds.setIncludes(makeArray(includeList));
ds.setExcludes(makeArray(excludeList));
if (useDefaultExcludes) ds.addDefaultExcludes();
ds.scan();
return ds;
}

/**
* add a name entry to the given list
*/
private NameEntry addPatternToList(Vector list) {
NameEntry result = new NameEntry();
list.addElement(result);
return result;
}

/**
* Reads path matching patterns from a file and adds them to the
* includes or excludes list (as appropriate).
*/
private void readPatterns(File patternfile, Vector patternlist) {

try {
// Get a FileReader
BufferedReader patternReader =
new BufferedReader(new FileReader(patternfile));
// Create one NameEntry in the appropriate pattern list for each
// line in the file.
String line = patternReader.readLine();
while (line != null) {
if (line.length() > 0) {
addPatternToList(patternlist).setName(line);
}
line = patternReader.readLine();
}
} catch(IOException ioe) {
log("An error occured while reading from pattern file: "
+ patternfile, Project.MSG_ERR);
}
fileset.setDir(baseDir);
fileset.setDefaultexcludes(useDefaultExcludes);
return fileset.getDirectoryScanner(project);
} }


/** /**
@@ -277,16 +180,8 @@ public abstract class MatchingTask extends Task {
* @param includesfile A string containing the filename to fetch * @param includesfile A string containing the filename to fetch
* the include patterns from. * the include patterns from.
*/ */
public void setIncludesfile(String includesfile) {
if (includesfile != null && includesfile.length() > 0) {
File incl = project.resolveFile(includesfile);
if (!incl.exists()) {
log("Includesfile "+includesfile+" not found.",
Project.MSG_ERR);
} else {
readPatterns(incl, includeList);
}
}
public void setIncludesfile(File includesfile) {
fileset.setIncludesfile(includesfile);
} }


/** /**
@@ -295,16 +190,8 @@ public abstract class MatchingTask extends Task {
* @param excludesfile A string containing the filename to fetch * @param excludesfile A string containing the filename to fetch
* the include patterns from. * the include patterns from.
*/ */
public void setExcludesfile(String excludesfile) {
if (excludesfile != null && excludesfile.length() > 0) {
File excl = project.resolveFile(excludesfile);
if (!excl.exists()) {
log("Excludesfile "+excludesfile+" not found.",
Project.MSG_ERR);
} else {
readPatterns(excl, excludeList);
}
}
public void setExcludesfile(File excludesfile) {
fileset.setExcludesfile(excludesfile);
} }


} }

+ 171
- 0
src/main/org/apache/tools/ant/types/FileSet.java View File

@@ -0,0 +1,171 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/

package org.apache.tools.ant.types;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import java.io.File;

/**
* Moved out of MatchingTask to make it a standalone object that could
* be referenced (by scripts for example).
*
* @author Arnout J. Kuiper <a href="mailto:ajkuiper@wxs.nl">ajkuiper@wxs.nl</a>
* @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a>
* @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
* @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
* @author <a href="mailto:stefan.bodewig@megabit.net">Stefan Bodewig</a>
*/
public class FileSet {
private PatternSet patterns = new PatternSet();

private File dir;
private boolean useDefaultExcludes = true;

public void setDir(File dir) throws BuildException {
if (!dir.exists()) {
throw new BuildException(dir.getAbsolutePath()+" not found.");
}
if (!dir.isDirectory()) {
throw new BuildException(dir.getAbsolutePath()+" is not a directory.");
}
this.dir = dir;
}

public PatternSet createPatternSet() {
return patterns;
}

/**
* add a name entry on the include list
*/
public PatternSet.NameEntry createInclude() {
return patterns.createInclude();
}
/**
* add a name entry on the exclude list
*/
public PatternSet.NameEntry createExclude() {
return patterns.createExclude();
}

/**
* 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) {
patterns.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) {
patterns.setExcludes(excludes);
}

/**
* Sets the name of the file containing the includes patterns.
*
* @param includesfile A string containing the filename to fetch
* the include patterns from.
*/
public void setIncludesfile(File incl) throws BuildException {
patterns.setIncludesfile(incl);
}

/**
* Sets the name of the file containing the includes patterns.
*
* @param excludesfile A string containing the filename to fetch
* the include patterns from.
*/
public void setExcludesfile(File excl) throws BuildException {
patterns.setExcludesfile(excl);
}

/**
* Sets whether default exclusions should be used or not.
*
* @param useDefaultExcludes "true"|"on"|"yes" when default exclusions
* should be used, "false"|"off"|"no" when they
* shouldn't be used.
*/
public void setDefaultexcludes(boolean useDefaultExcludes) {
this.useDefaultExcludes = useDefaultExcludes;
}

/**
* Returns the directory scanner needed to access the files to process.
*/
public DirectoryScanner getDirectoryScanner(Project p) {
DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir(dir);
ds.setIncludes(patterns.getIncludePatterns(p));
ds.setExcludes(patterns.getExcludePatterns(p));
if (useDefaultExcludes) ds.addDefaultExcludes();
ds.scan();
return ds;
}

}

+ 282
- 0
src/main/org/apache/tools/ant/types/PatternSet.java View File

@@ -0,0 +1,282 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/

package org.apache.tools.ant.types;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;

import java.io.*;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;

/**
* Named collection of include/exclude tags.
*
* <p>Moved out of MatchingTask to make it a standalone object that
* could be referenced (by scripts for example).
*
* @author Arnout J. Kuiper <a href="mailto:ajkuiper@wxs.nl">ajkuiper@wxs.nl</a>
* @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a>
* @author Sam Ruby <a href="mailto:rubys@us.ibm.com">rubys@us.ibm.com</a>
* @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
* @author <a href="mailto:stefan.bodewig@megabit.net">Stefan Bodewig</a>
*/
public class PatternSet {
private Vector includeList = new Vector();
private Vector excludeList = new Vector();
/**
* inner class to hold a name on list. "If" and "Unless" attributes
* may be used to invalidate the entry based on the existence of a
* property (typically set thru the use of the Available task).
*/
public class NameEntry {
private String name;
private String ifCond;
private String unlessCond;

public void setName(String name) {
this.name = name;
}

public void setIf(String cond) {
ifCond = cond;
}

public void setUnless(String cond) {
unlessCond = cond;
}

public String getName() {
return name;
}

public String evalName(Project p) {
return valid(p) ? name : null;
}

private boolean valid(Project p) {
if (ifCond != null && p.getProperty(ifCond) == null) {
return false;
} else if (unlessCond != null && p.getProperty(unlessCond) != null) {
return false;
}
return true;
}
}

public PatternSet() {
super();
}

/**
* add a name entry on the include list
*/
public NameEntry createInclude() {
return addPatternToList(includeList);
}
/**
* add a name entry on the exclude list
*/
public NameEntry createExclude() {
return addPatternToList(excludeList);
}

/**
* 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 (includes != null && includes.length() > 0) {
StringTokenizer tok = new StringTokenizer(includes, ", ", false);
while (tok.hasMoreTokens()) {
createInclude().setName(tok.nextToken());
}
}
}

/**
* 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 (excludes != null && excludes.length() > 0) {
StringTokenizer tok = new StringTokenizer(excludes, ", ", false);
while (tok.hasMoreTokens()) {
createExclude().setName(tok.nextToken());
}
}
}

/**
* add a name entry to the given list
*/
private NameEntry addPatternToList(Vector list) {
NameEntry result = new NameEntry();
list.addElement(result);
return result;
}

/**
* Sets the name of the file containing the includes patterns.
*
* @param includesfile A string containing the filename to fetch
* the include patterns from.
*/
public void setIncludesfile(File incl) throws BuildException {
if (!incl.exists()) {
throw new BuildException("Includesfile "+incl.getAbsolutePath()
+" not found.");
} else {
readPatterns(incl, includeList);
}
}

/**
* Sets the name of the file containing the includes patterns.
*
* @param excludesfile A string containing the filename to fetch
* the include patterns from.
*/
public void setExcludesfile(File excl) throws BuildException {
if (!excl.exists()) {
throw new BuildException("Excludesfile "+excl.getAbsolutePath()
+" not found.");
} else {
readPatterns(excl, excludeList);
}
}
/**
* Reads path matching patterns from a file and adds them to the
* includes or excludes list (as appropriate).
*/
private void readPatterns(File patternfile, Vector patternlist)
throws BuildException {
try {
// Get a FileReader
BufferedReader patternReader =
new BufferedReader(new FileReader(patternfile));
// Create one NameEntry in the appropriate pattern list for each
// line in the file.
String line = patternReader.readLine();
while (line != null) {
if (line.length() > 0) {
addPatternToList(patternlist).setName(line);
}
line = patternReader.readLine();
}
} catch(IOException ioe) {
String msg = "An error occured while reading from pattern file: "
+ patternfile;
throw new BuildException(msg, ioe);
}
}

/**
* Adds the patterns of the other instance to this set.
*/
public void append(PatternSet other) {
for (int i=0; i<other.includeList.size(); i++) {
includeList.addElement(other.includeList.elementAt(i));
}
for (int i=0; i<other.excludeList.size(); i++) {
excludeList.addElement(other.excludeList.elementAt(i));
}
}

/**
* Returns the filtered include patterns.
*/
public String[] getIncludePatterns(Project p) {
return makeArray(includeList, p);
}

/**
* Returns the filtered include patterns.
*/
public String[] getExcludePatterns(Project p) {
return makeArray(excludeList, p);
}

/**
* Convert a vector of NameEntry elements into an array of Strings.
*/
private String[] makeArray(Vector list, Project p) {
if (list.size() == 0) return null;

Vector tmpNames = new Vector();
for (Enumeration e = list.elements() ; e.hasMoreElements() ;) {
NameEntry ne = (NameEntry)e.nextElement();
String pattern = ne.evalName(p);
if (pattern != null && pattern.length() > 0) {
tmpNames.addElement(pattern);
}
}

String result[] = new String[tmpNames.size()];
tmpNames.copyInto(result);
return result;
}
}

Loading…
Cancel
Save