From e98e1462b5a05048e3336b230901ea885977f339 Mon Sep 17 00:00:00 2001 From: Diane Holt Date: Sat, 30 Mar 2002 01:03:40 +0000 Subject: [PATCH] (Oh what the hell -- if the coding sucks, it can get backed out or corrected :) Added: - new DirSet datatype - support for DirSet and FileList to - support for DirSet and FileList to (Note: The added support for FileList doesn't have anything to do with DirSet, I just figured while was there, might as well add that as well.) Will put through doc/testcases stuff in a separate commit. PR: Obtained from: Submitted by: Reviewed by: git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272100 13f79535-47bb-0310-9956-ffa450edef68 --- .../tools/ant/taskdefs/PathConvert.java | 15 +- .../org/apache/tools/ant/types/DirSet.java | 313 ++++++++++++++++++ src/main/org/apache/tools/ant/types/Path.java | 41 +++ .../tools/ant/types/defaults.properties | 1 + 4 files changed, 367 insertions(+), 3 deletions(-) create mode 100644 src/main/org/apache/tools/ant/types/DirSet.java diff --git a/src/main/org/apache/tools/ant/taskdefs/PathConvert.java b/src/main/org/apache/tools/ant/taskdefs/PathConvert.java index 2ffd58ec1..af47aa616 100644 --- a/src/main/org/apache/tools/ant/taskdefs/PathConvert.java +++ b/src/main/org/apache/tools/ant/taskdefs/PathConvert.java @@ -60,6 +60,8 @@ import org.apache.tools.ant.Task; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Reference; import org.apache.tools.ant.types.FileSet; +import org.apache.tools.ant.types.DirSet; +import org.apache.tools.ant.types.FileList; import java.util.Vector; import java.io.File; @@ -79,7 +81,7 @@ public class PathConvert extends Task { /** * Helper class, holds the nested values. Elements will look like this: - * <map from="d:" to="/foo"/> + * <map from="d:" to="/foo"/> *

* When running on windows, the prefix comparison will be case insensitive. */ @@ -226,7 +228,7 @@ public class PathConvert extends Task { */ public void execute() throws BuildException { - // If we are a reference, the create a Path from the reference + // If we are a reference, create a Path from the reference if( isReference() ) { path = new Path(getProject()).createPath(); @@ -237,8 +239,15 @@ public class PathConvert extends Task { } else if( obj instanceof FileSet ) { FileSet fs = (FileSet)obj; path.addFileset( fs ); + } else if( obj instanceof DirSet ) { + DirSet ds = (DirSet)obj; + path.addDirset( ds ); + } else if( obj instanceof FileList ) { + FileList fl = (FileList)obj; + path.addFilelist( fl ); + } else { - throw new BuildException( "'refid' does not refer to a path or fileset" ); + throw new BuildException( "'refid' does not refer to a path, fileset, dirset, or filelist." ); } } diff --git a/src/main/org/apache/tools/ant/types/DirSet.java b/src/main/org/apache/tools/ant/types/DirSet.java new file mode 100644 index 000000000..01f061fda --- /dev/null +++ b/src/main/org/apache/tools/ant/types/DirSet.java @@ -0,0 +1,313 @@ +/* + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2000-2002 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", "Ant", 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 + * . + */ + +package org.apache.tools.ant.types; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.FileScanner; +import org.apache.tools.ant.DirectoryScanner; +import org.apache.tools.ant.Project; + +import java.io.File; +import java.util.Stack; +import java.util.Vector; + +/** + * Essentially a clone of FileSet, but gets a set of dirs instead of files. + */ +public class DirSet extends DataType implements Cloneable { + + private PatternSet defaultPatterns = new PatternSet(); + private Vector additionalPatterns = new Vector(); + + private File dir; + private boolean isCaseSensitive = true; + + public DirSet() { + super(); + } + + 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. + * + *

You must not set another attribute or nest elements inside + * this element if you make it a reference.

+ */ + 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; + } + } + + /** + * Return a DirSet that has the same basedir and same patternsets + * as this one. + */ + public Object clone() { + if (isReference()) { + return new DirSet(getRef(getProject())); + } else { + return new DirSet(this); + } + } + +} diff --git a/src/main/org/apache/tools/ant/types/Path.java b/src/main/org/apache/tools/ant/types/Path.java index d7554ad58..f6b0032db 100644 --- a/src/main/org/apache/tools/ant/types/Path.java +++ b/src/main/org/apache/tools/ant/types/Path.java @@ -198,6 +198,28 @@ public class Path extends DataType implements Cloneable { checked = false; } + /** + * Adds a nested <filelist> element. + */ + public void addFilelist(FileList fl) throws BuildException { + if (isReference()) { + throw noChildrenAllowed(); + } + elements.addElement(fl); + checked = false; + } + + /** + * Adds a nested <dirset> element. + */ + public void addDirset(DirSet dset) throws BuildException { + if (isReference()) { + throw noChildrenAllowed(); + } + elements.addElement(dset); + checked = false; + } + /** * Creates a nested <path> element. */ @@ -307,6 +329,25 @@ public class Path extends DataType implements Cloneable { String absolutePath = f.getAbsolutePath(); addUnlessPresent(result, translateFile(absolutePath)); } + } else if (o instanceof DirSet) { + DirSet dset = (DirSet) o; + DirectoryScanner ds = dset.getDirectoryScanner(getProject()); + String[] s = ds.getIncludedDirectories(); + File dir = dset.getDir(getProject()); + for (int j=0; j