@@ -9,13 +9,13 @@ package org.apache.tools.ant.taskdefs.optional;
import java.io.File;
import org.apache.myrmidon.api.TaskException;
import org.apache.tools.ant.types.DirectoryScanner;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.DirectoryScanner;
import org.apache.tools.ant.types.Mapper;
import org.apache.tools.ant.types.SourceFileScanner;
import org.apache.tools.ant.util.FileNameMapper;
import org.apache.tools.ant.util.IdentityMapper;
import org.apache.tools.ant.types.SourceFileScanner;
/**
* Convert files from native encodings to ascii.
@@ -23,25 +23,24 @@ import org.apache.tools.ant.types.SourceFileScanner;
* @author <a href="asudell@acm.org">Drew Sudell</a>
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a>
*/
public class Native2Ascii extends MatchingTask
public class Native2Ascii
extends MatchingTask
{
private boolean reverse = false;// convert from ascii back to native
private String encoding = null;// encoding to convert to/from
private File srcDir = null;// Where to find input files
private File destDir = null;// Where to put output files
private String extension = null;// Extension of output files if different
private Mapper mapper;
private boolean m_reverse;// convert from ascii back to native
private String m_encoding;// encoding to convert to/from
private File m_srcDir;// Where to find input files
private File m_destDir;// Where to put output files
private String m_ext;// Extension of output files if different
private Mapper m_mapper;
/**
* Set the destination dirctory to place converted files into.
*
* @param destDir directory to place output file into.
*/
public void setDest( File destDir )
public void setDest( final File destDir )
{
this. destDir = destDir;
m_ destDir = destDir;
}
/**
@@ -51,9 +50,9 @@ public class Native2Ascii extends MatchingTask
* @param encoding String containing the name of the Native encoding to
* convert from or to.
*/
public void setEncoding( String encoding )
public void setEncoding( final String encoding )
{
this. encoding = encoding;
m_ encoding = encoding;
}
/**
@@ -62,9 +61,9 @@ public class Native2Ascii extends MatchingTask
*
* @param ext File extension to use for converted files.
*/
public void setExt( String ext )
public void setExt( final String ext )
{
this.extension = ext;
m_ext = ext;
}
/**
@@ -75,7 +74,7 @@ public class Native2Ascii extends MatchingTask
*/
public void setReverse( boolean reverse )
{
this. reverse = reverse;
m_ reverse = reverse;
}
/**
@@ -83,9 +82,9 @@ public class Native2Ascii extends MatchingTask
*
* @param srcDir Direcrory to find input file in.
*/
public void setSrc( File srcDir )
public void setSrc( final File srcDir )
{
this. srcDir = srcDir;
m_ srcDir = srcDir;
}
/**
@@ -97,75 +96,88 @@ public class Native2Ascii extends MatchingTask
public Mapper createMapper()
throws TaskException
{
if( mapper != null )
if( m_m apper != null )
{
throw new TaskException( "Cannot define more than one mapper" );
}
mapper = new Mapper();
return mapper;
m_m apper = new Mapper();
return m_m apper;
}
public void execute()
throws TaskException
{
validate();
Commandline baseCmd = null;// the common portion of our cmd line
DirectoryScanner scanner = null;// Scanner to find our inputs
String[] files;// list of files to process
final DirectoryScanner scanner = getDirectoryScanner( m_srcDir );
String[] files = scanner.getIncludedFiles();
// default srcDir to basedir
if( srcDir == null )
final SourceFileScanner sfs = new SourceFileScanner( this );
final FileNameMapper mapper = buildMapper();
files = sfs.restrict( files, m_srcDir, m_destDir, mapper );
int count = files.length;
if( count == 0 )
{
srcDir = getBaseDirectory();
return ;
}
// Require destDir
if( destDir == null )
{
throw new TaskException( "The dest attribute must be set." );
}
final String message = "Converting " + count + " file" +
( count != 1 ? "s" : "" ) + " from " + m_srcDir + " to " +
m_destDir;
getLogger().info( message );
// if src and dest dirs are the same, require the extension
// to be set, so we don't stomp every file. One could still
// include a file with the same extension, but ....
if( srcDir.equals( destDir ) && extension == null && mapper == null )
for( int i = 0; i < files.length; i++ )
{
throw new TaskException( "The ext attribute or a mapper must be set if"
+ " src and dest dirs are the same." );
final String name = mapper.mapFileName( files[ i ] )[ 0 ];
convert( files[ i ], name );
}
}
FileNameMapper m = null;
if( mapper == null )
private FileNameMapper buildMapper()
throws TaskException
{
FileNameMapper mapper = null;
if( m_mapper == null )
{
if( extension == null )
if( m_ ext == null )
{
m = new IdentityMapper();
mapper = new IdentityMapper();
}
else
{
m = new ExtMapper();
mapper = new ExtMapper( m_ext );
}
}
else
{
m = mapper.getImplementation();
mapper = m_ mapper.getImplementation();
}
scanner = getDirectoryScanner( srcDir );
files = scanner.getIncludedFiles();
SourceFileScanner sfs = new SourceFileScanner( this );
files = sfs.restrict( files, srcDir, destDir, m );
int count = files.length;
if( count == 0 )
return mapper;
}
private void validate()
throws TaskException
{
// Require destDir
if( m_destDir == null )
{
return;
throw new TaskException( "The dest attribute must be set." ) ;
}
String message = "Converting " + count + " file"
+ ( count != 1 ? "s" : "" ) + " from ";
getLogger().info( message + srcDir + " to " + destDir );
for( int i = 0; i < files.length; i++ )
// if src and dest dirs are the same, require the extension
// to be set, so we don't stomp every file. One could still
// include a file with the same extension, but ....
if( m_srcDir.equals( m_destDir ) && m_ext == null && m_mapper == null )
{
throw new TaskException( "The ext attribute or a mapper must be set if" +
" src and dest dirs are the same." );
}
// default srcDir to basedir
if( m_srcDir == null )
{
convert( files[ i ], m.mapFileName( files[ i ] )[ 0 ] );
m_srcDir = getBaseDirectory( );
}
}
@@ -179,30 +191,10 @@ public class Native2Ascii extends MatchingTask
private void convert( String srcName, String destName )
throws TaskException
{
Commandline cmd = new Commandline();// Command line to run
File srcFile;// File to convert
File destFile;// where to put the results
// Set up the basic args (this could be done once, but
// it's cleaner here)
if( reverse )
{
cmd.createArgument().setValue( "-reverse" );
}
if( encoding != null )
{
cmd.createArgument().setValue( "-encoding" );
cmd.createArgument().setValue( encoding );
}
// Build the full file names
srcFile = new File( srcDir, srcName );
destFile = new File( destDir, destName );
final File srcFile = new File( m_srcDir, srcName );
final File destFile = new File( m_destDir, destName );
cmd.createArgument().setFile( srcFile );
cmd.createArgument().setFile( destFile );
// Make sure we're not about to clobber something
if( srcFile.equals( destFile ) )
{
@@ -210,51 +202,46 @@ public class Native2Ascii extends MatchingTask
+ " would overwrite its self" );
}
final Commandline cmd = buildCommand( srcFile, destFile );
// Make intermediate directories if needed
// XXX JDK 1.1 dosen't have File.getParentFile,
String parentName = destFile.getParent ();
if( parentName != null )
final File parent = destFile.getParentFile ();
if( parent != null )
{
File parentFile = new File( parentName );
if( ( !parentFile.exists() ) && ( !parentFile.mkdirs() ) )
if( ( !parent.exists() ) && ( !parent.mkdirs() ) )
{
throw new TaskException( "cannot create parent directory "
+ parentName );
throw new TaskException( "cannot create parent directory " + parent );
}
}
getLogger().debug( "converting " + srcName );
sun.tools.native2ascii.Main n2a
= new sun.tools.native2ascii.Main();
sun.tools.native2ascii.Main n2a = new sun.tools.native2ascii.Main();
if( !n2a.convert( cmd.getArguments() ) )
{
throw new TaskException( "conversion failed" );
}
}
private class ExtMapper implements FileNameMapper
private Commandline buildCommand( final File srcFile,
final File destFile )
{
public void setFrom( String s )
final Commandline cmd = new Commandline();// Command line to run
// Set up the basic args (this could be done once, but
// it's cleaner here)
if( m_reverse )
{
cmd.createArgument().setValue( "-reverse" );
}
public void setTo( String s )
if( m_encoding != null )
{
cmd.createArgument().setValue( "-encoding" );
cmd.createArgument().setValue( m_encoding );
}
public String[] mapFileName( String fileName )
{
int lastDot = fileName.lastIndexOf( '.' );
if( lastDot >= 0 )
{
return new String[]{fileName.substring( 0, lastDot ) + extension};
}
else
{
return new String[]{fileName + extension};
}
}
cmd.createArgument().setFile( srcFile );
cmd.createArgument().setFile( destFile );
return cmd;
}
}