ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "pdgilligan@oyap.net" <pdgilli...@oyap.net>
Subject Java Add On for Ant to fix filename + extension case issues
Date Wed, 08 Oct 2003 16:04:19 GMT
 
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="1"/>
<fixfilecase srcdir="${build}" mask="2" action="2"/>
 
For SQR:
 
<fixfilecase srcdir="${build}" mask="3" action="2"/>
 
 
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:pdgilligan@oyap.net">pdgilligan@oyap
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 = UPPER;
 
/**
* 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 = 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 = new 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++) {
}
}
 
 
 
 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Mime
View raw message