Dear All,
I have written a rountine todo this, was there an easier way or plans to include such things in ANT in the future?
Also wrote one to fix "include <filename>" case issues within source code that had been moved to unix, as in the
filename in the source code was mixed case but the file on the unix was now upper case.
Notes:
This code expects a filename.ext format because we are dealing with
PeopleSoft
cobol source that needs to be in the format FILENAME.cbl and SQR source that
needs to be lower case. Now we can use ant to convert Windows based source
to unix thanks to ant + a bit of java code added.
You need this your build.xml file
<taskdef name="fixfilecase" classname="AntXtra.FixFileCase"/>
Then for COBOL:
<!-- fix uppercase filename lower case extension -->
<fixfilecase srcdir="${build}" mask="1" action=""/>
<fixfilecase srcdir="${build}" mask="2" action=""/>
For SQR:
<fixfilecase srcdir="${build}" mask="3" action=""/>
And the source:
package AntXtra;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import java.io.File;
import java.io.IOException;
/**
* Converts file names providing lowercase/uppercase functionality.
* <p>
* This task can take the following arguments:
* <ul>
* <li>srcdir
* <li>mask
* <li>action
* </ul>
* <p>
*
* @author Paul Gilligan<a href=""mailto:[EMAIL PROTECTED]">mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]
net</a>
* @version 1.0
*/
public class FixFileCase extends MatchingTask {
public static final int FILENAME = 1;
public static final int EXTENSION = 2;
public static final int BOTH = 3;
public static final int UPPER = 1;
public static final int LOWER = 2;
private File srcDir = null;
private int mask = BOTH;
private int action = "">
/**
* Set the source dir to find the source text files.
*/
public void setSrcdir(File srcDir) {
this.srcDir = srcDir;
}
/**
* Set the mask for processing file names and or extensions
*/
public void setMask(int mask) {
this.mask = mask;
}
/**
* Set the action for processing file names and or extensions
*/
public void setAction(int action) {
this.action = "">
}
/**
* Executes the task.
*/
public void execute() throws BuildException {
// Make sure that we've got a good srcdir
if (srcDir == null) {
throw new BuildException("srcdir attribute must be set!");
}
if (!srcDir.exists()) {
throw new BuildException("srcdir does not exist!");
}
if (!srcDir.isDirectory()) {
throw new BuildException("srcdir is not a directory!");
}
// Check the for vaild mask
switch (mask) {
case FILENAME: log("Fixing Filename only"); break;
case EXTENSION: log("Fixing Extension only"); break;
case BOTH: log("Fixing Filename and Extension"); break;
default: throw new BuildException("invalid mask, must be 1, 2 or 3!");
}
// Check the for vaild action
switch (action) {
case UPPER: log("with upper case action"); break;
case LOWER: log("with lower case action"); break;
default: throw new BuildException("invalid action, must be 1 or 2!");
}
DirectoryScanner ds = super.getDirectoryScanner(srcDir);
String[] files = ds.getIncludedFiles();
for (int i = 0; i < files.length; i++) {
// try for an extension
String n = files[i].substring(0, files[i].lastIndexOf("."));
String e = files[i].substring(files[i].lastIndexOf(".")+1);
switch(action)
{
case UPPER:
{
switch(mask) {
case FILENAME: n = n.toUpperCase(); break;
case EXTENSION: e = e.toUpperCase(); break;
case BOTH: n = n.toUpperCase(); e = e.toUpperCase(); break;
}
}
break;
case LOWER:
{
switch(mask) {
case FILENAME: n = n.toLowerCase(); break;
case EXTENSION: e = e.toLowerCase(); break;
case BOTH: n = n.toLowerCase(); e = e.toLowerCase(); break;
}
}
break;
}
File src = "" File(srcDir, files[i]);
File dst = new File(srcDir, n+"."+e);
boolean renamed = src.renameTo(dst);
if(!renamed) {
log("source "+src+" destination "+dst+" not renamed!");
}
} // (int i = 0; i < files.length; i++) {
}
}
|