com.hfg.anttask.FileProperty Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.anttask;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.FileScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.PatternSet;
import java.io.File;
import com.hfg.util.BooleanUtil;
import com.hfg.util.StringUtil;
//------------------------------------------------------------------------------
/**
Ant task for assigning a file to a property.
Description
If the specified file exists, this task sets the properties
<name>
, <name>.name
,
<name>.wildcard
,
in the project. Properties are case sensitive.
Normally, this task is used with files whose names contain a variable portion, the
matching of which is facilitated by a PatternSet which allows '*' and '?' wildcarding
(see Ant's documentation).
If multiple files are found which match the file pattern, a BuildException is thrown.
Parameters
Parameter descriptions
Attribute
Description
Required
name
The name (handle) of the property.
Yes
dir
The directory containing the file. If not
set, it defaults to the current directory.
Yes
file
The name of the file (may contain wildcards).
If not set, a directory is searched for in place of a file.
if
Only execute if a property of the given name exists in the current project.
No
unless
Only execute if a property of the given name doesn't exist in the current project.
No
The if
and unless
attributes make the
execution conditional -both probe for the named property being defined.
The if
tests for the property being defined, the
unless
for a property being undefined.
If both attributes are set, then the task executes only if both tests
are true. i.e.
execute := defined(ifProperty) && !defined(unlessProperty)
Examples
If ${lib.dir}
contained the file commons-net-20040324.jar
<fileproperty name='commons_net' dir='${lib.dir}' file='commons-net-*.jar' />
would set the commons_net
property to ${lib.dir}/commons-net-20040324.jar
,
the commons_net.name
property to commons-net-20040324.jar
,
the commons_net.wildcard
property to 20040324
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class FileProperty extends Task
{
private String mName;
private PatternSet mPattern;
private File mDir = new File(".");
private String mIfCondition;
private String mUnlessCondition;
private boolean mDebug;
private int mMode = FILE_MODE;
private static int FILE_MODE = 1;
private static int DIR_MODE = 2;
//--------------------------------------------------------------------------
public void setName(String name)
{
mName = name;
}
//--------------------------------------------------------------------------
public void setFile(String file)
{
mPattern = new PatternSet();
mPattern.setIncludes(file);
}
//--------------------------------------------------------------------------
public void setDir(File dir) throws BuildException
{
mDir = dir;
}
//--------------------------------------------------------------------------
public void setDebug(String inValue) throws BuildException
{
mDebug = BooleanUtil.valueOf(inValue);
}
//---------------------------------------------------------------------------
/**
* Only execute if a property of the given name exists in the current project
*
* @param inPropertyName property name
*/
public void setIf(String inPropertyName)
{
mIfCondition = inPropertyName;
}
//---------------------------------------------------------------------------
/**
* Only execute if a property of the given name does not exist in the current project
*
* @param inPropertyName property name
*/
public void setUnless(String inPropertyName)
{
mUnlessCondition = inPropertyName;
}
//--------------------------------------------------------------------------
public void execute()
throws BuildException
{
if (TaskUtil.testIfCondition(mIfCondition, getProject())
&& TaskUtil.testUnlessCondition(mUnlessCondition, getProject()))
{
try
{
if (null == mPattern)
{
// No file pattern specified. Use the dir as a pattern
mPattern = new PatternSet();
mPattern.setIncludes(mDir.getName());
mDir = mDir.getParentFile();
mMode = DIR_MODE;
}
if (mDir.exists()
&& mDir.isDirectory())
{
DirectoryScanner ds = getDirectoryScanner();
if (mDebug)
{
System.out.println("Looking for file pattern " + StringUtil.singleQuote(mPattern.toString()) + " ...");
}
String[] srcFiles = (mMode == FILE_MODE ? ds.getIncludedFiles() : ds.getIncludedDirectories());
if (mDebug)
{
System.out.println(srcFiles.length + " matching files found.");
}
if (srcFiles.length > 1)
{
throw new BuildException("More than one file matches pattern.");
}
else if (srcFiles.length == 1)
{
File file = new File(mDir, srcFiles[0]);
getProject().setProperty(mName, file.getAbsolutePath());
getProject().setProperty(mName + ".name", file.getName());
setWildcardProperty(file.getName());
}
}
else if (mDebug)
{
System.out.println("Directory " + StringUtil.singleQuote(mDir.getPath()) + " doesn't exist!");
}
}
catch (Exception e)
{
throw new BuildException(e);
}
}
}
//--------------------------------------------------------------------------
private void setWildcardProperty(String matchedFilename)
{
String patternString = mPattern.getIncludePatterns(getProject())[0];
int wildcardIndex = patternString.indexOf("*");
if (wildcardIndex >= 0)
{
String wildcard = "";
if (wildcardIndex > 0)
{
String leftSide = patternString.substring(0, wildcardIndex);
wildcard = matchedFilename.substring(leftSide.length());
}
String rightSide = patternString.substring(wildcardIndex + 1);
if (wildcardIndex < patternString.length()
&& rightSide.length() < wildcard.length())
{
wildcard = wildcard.substring(0, wildcard.length() - rightSide.length());
}
getProject().setProperty(mName + ".wildcard", wildcard);
}
else
{
wildcardIndex = patternString.indexOf("?");
if (wildcardIndex >= 0)
{
StringBuilder wildcard = new StringBuilder();
while (wildcardIndex < patternString.length())
{
if (patternString.charAt(wildcardIndex) == '?')
{
wildcard.append(matchedFilename.charAt(wildcardIndex));
}
else
{
break;
}
wildcardIndex++;
}
getProject().setProperty(mName + ".wildcard", wildcard.toString());
}
}
}
//--------------------------------------------------------------------------
/**
* Returns the directory scanner needed to access the files to process.
*/
private DirectoryScanner getDirectoryScanner()
{
DirectoryScanner ds = new DirectoryScanner();
setupDirectoryScanner(ds);
ds.scan();
return ds;
}
//--------------------------------------------------------------------------
private void setupDirectoryScanner(FileScanner ds)
{
if (ds == null)
{
throw new IllegalArgumentException("ds cannot be null");
}
ds.setBasedir(mDir);
ds.setIncludes(mPattern.getIncludePatterns(getProject()));
}
}