The attached class contains a custom task that will do the trick. The javadoc 
explains how to use it.



Gerard



On Wednesday 19 January 2005 16:36, Dick, Brian E. wrote:
> I need to remove the extension from a file name. The basename task
> almost works when specifying the suffix attribute. However, I don't know
> the value of the suffix.
>
> Does anyone know how I can remove the extension (suffix) from a file
> name?
>
> Later,
> BEDick
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
/*
 * 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 GREEN
 * LIGHT CONSULTANCY B.V. 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.
 *
 * $Lib $
 */

package nl.glc.tools.ant.classname;

import java.io.File;

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

/**
 * Title: ClassName<br>
 * Description: This task can accept the following attributes: Sets a property to the class name of a specified
 * file, optionally minus a suffix.
 * 
 * <ul>
 * <li>
 * file
 * </li>
 * <li>
 * property
 * </li>
 * <li>
 * suffix
 * </li>
 * <li>
 * prefix
 * </li>
 * <li>
 * separator
 * </li>
 * </ul>
 * 
 * The <b>file</b> and <b>property</b> attributes are required. The <b>suffix</b> attribute can be
 * specified either with or without the &quot;.&quot;, and the result will be the same (ie., the
 * returned file name will be minus the .suffix).
 * 
 * <p>
 * When this task executes, it will set the specified property to the value of the last element in
 * the specified file. If file is a directory, the classname will be the last directory element.
 * If file is a full-path filename, the basename will be the simple file name. If a suffix is
 * specified, and the specified file ends in that suffix, the basename will be the simple file
 * name without the suffix. If a prefix is specified, and the specified file contains the prefix,
 * the classname will be the file name without everything preceding and including the
 * prefix. If &quot;\&quot;es occur in the resulting classname, they will be substituted for
 * &quot;/&quot;es.
 * If separator is specified, instead of substituting all &quot;\&quot;es for &quot;/&quot;es, all
 * &quot;\&quot;es <b>AND</b> all &quot;/&quot;es will be substituted for the separator.
 * </p>
 * Copyright: Copyright (c) 2004<br>
 * Company: GLC eProfessionals B.V.<br>
 *
 * @author Jan R. Zeinstra
 * @version $Id $
 */
public class ClassName
	extends Task
{
	//~ Methods ····················································································

	/**
	 * file or directory to get base name from
	 *
	 * @param aFile file or directory to get base name from
	 */
	public void setFile(File aFile)
	{
		this.theFile = aFile;
	}

	/**
	 * Property to set base name to.
	 *
	 * @param aProperty name of property
	 */
	public void setProperty(String aProperty)
	{
		this.theProperty = aProperty;
	}

	/**
	 * Optional suffix to remove from base name.
	 *
	 * @param aSuffix suffix to remove from base name
	 */
	public void setSuffix(String aSuffix)
	{
		this.theSuffix = aSuffix;
	}
	
	/**
	 * Optional prefix to remove from classname.
	 *
	 * @param aPrefix aPrefix to remove from classname
	 */
	public void setPrefix(String aPrefix)
	{
		this.thePrefix = aPrefix;
	}
	
	/**
	 * Optional separator to use for separating path entries in the classname.
	 *
	 * @param aSeparator separator to use
	 */
	public void setSeparator(String aSeparator)
	{
		this.theSeparator = aSeparator;
	}

	/**
	 * do the work
	 *
	 * @throws BuildException if required attributes are not supplied property and attribute are
	 * 		   required attributes
	 */
	public void execute()
		throws BuildException
	{
		if (theProperty == null)
		{
			throw new BuildException("property attribute required",
				getLocation());
		}

		if (theFile == null)
		{
			throw new BuildException("file attribute required",
				getLocation());
		}

		String myValue = null;
		if(thePrefix == null)
		{
			myValue = theFile.getName();
		}
		else
		{
			myValue = theFile.getAbsolutePath();
			int myPrefixIndex = myValue.indexOf(thePrefix);
			if(myPrefixIndex!=-1)
			{
				myValue = myValue.substring(myPrefixIndex + thePrefix.length());
			}
			else
			{
				myValue = theFile.getName();				
			}
		}

		if ((theSuffix != null) && myValue.endsWith(theSuffix))
		{
			// if the suffix does not starts with a '.' and the
			// char preceding the suffix is a '.', we assume the user
			// wants to remove the '.' as well (see docs)
			int pos = myValue.length() - theSuffix.length();

			if ((pos > 0) && (theSuffix.charAt(0) != '.') && (myValue.charAt(pos - 1) == '.'))
			{
				pos--;
			}

			myValue = myValue.substring(0, pos);
			if(theSeparator == null)
			{
				myValue = myValue.replaceAll("\\\\","/");
				if(myValue.startsWith("/"))
				{
					myValue = myValue.substring(1);
				}
			}
			else
			{
				myValue = myValue.replaceAll("\\\\",theSeparator);
				myValue = myValue.replaceAll("/",theSeparator);				
				if(myValue.startsWith(theSeparator))
				{
					myValue = myValue.substring(1);
				}
			}
		}

		getProject().setNewProperty(theProperty, myValue);
	}

	//~ Instance variables ·········································································

	private File   theFile;
	private String theProperty;
	private String theSuffix;
	private String thePrefix;
	private String theSeparator;
}

/*
 * Formatted according to GLC Coding Convention.
 */

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to