FileDocCategorySizeDatePackage
Native2Ascii.javaAPI DocApache Ant 1.709758Wed Dec 13 06:16:24 GMT 2006org.apache.tools.ant.taskdefs.optional

Native2Ascii

public class Native2Ascii extends org.apache.tools.ant.taskdefs.MatchingTask
Converts files from native encodings to ASCII.
since
Ant 1.2

Fields Summary
private boolean
reverse
private String
encoding
private File
srcDir
private File
destDir
private String
extension
private org.apache.tools.ant.types.Mapper
mapper
private org.apache.tools.ant.util.facade.FacadeTaskHelper
facade
Constructors Summary
public Native2Ascii()
No args constructor


        
      
        facade = new FacadeTaskHelper(Native2AsciiAdapterFactory.getDefault());
    
Methods Summary
public voidadd(org.apache.tools.ant.util.FileNameMapper fileNameMapper)
A nested filenamemapper

param
fileNameMapper the mapper to add
since
Ant 1.6.3

        createMapper().add(fileNameMapper);
    
private voidconvert(java.lang.String srcName, java.lang.String destName)
Convert a single file.

param
srcName name of the input file.
param
destName name of the input file.

        File srcFile;                         // File to convert
        File destFile;                        // where to put the results

        // Build the full file names
        srcFile = new File(srcDir, srcName);
        destFile = new File(destDir, destName);

        // Make sure we're not about to clobber something
        if (srcFile.equals(destFile)) {
            throw new BuildException("file " + srcFile
                                     + " would overwrite its self");
        }

        // Make intermediate directories if needed
        // XXX JDK 1.1 doesn't have File.getParentFile,
        String parentName = destFile.getParent();
        if (parentName != null) {
            File parentFile = new File(parentName);

            if ((!parentFile.exists()) && (!parentFile.mkdirs())) {
                throw new BuildException("cannot create parent directory "
                                         + parentName);
            }
        }

        log("converting " + srcName, Project.MSG_VERBOSE);
        Native2AsciiAdapter ad =
            Native2AsciiAdapterFactory.getAdapter(facade.getImplementation(),
                                                  this);
        if (!ad.convert(this, srcFile, destFile)) {
            throw new BuildException("conversion failed");
        }
    
public org.apache.tools.ant.util.facade.ImplementationSpecificArgumentcreateArg()
Adds an implementation specific command-line argument.

return
a ImplementationSpecificArgument to be configured
since
Ant 1.6.3

        ImplementationSpecificArgument arg =
            new ImplementationSpecificArgument();
        facade.addImplementationArgument(arg);
        return arg;
    
public org.apache.tools.ant.types.MappercreateMapper()
Defines the FileNameMapper to use (nested mapper element).

return
the mapper to use for file name translations.
throws
BuildException if more than one mapper is defined.

        if (mapper != null) {
            throw new BuildException("Cannot define more than one mapper",
                                     getLocation());
        }
        mapper = new Mapper(getProject());
        return mapper;
    
public voidexecute()
Execute the task

throws
BuildException is there is a problem in the task execution.


        DirectoryScanner scanner = null; // Scanner to find our inputs
        String[] files;                  // list of files to process

        // default srcDir to basedir
        if (srcDir == null) {
            srcDir = getProject().resolveFile(".");
        }

        // Require destDir
        if (destDir == null) {
            throw new BuildException("The dest attribute must be set.");
        }

        // 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) {
            throw new BuildException("The ext attribute or a mapper must be set if"
                                     + " src and dest dirs are the same.");
        }

        FileNameMapper m = null;
        if (mapper == null) {
            if (extension == null) {
                m = new IdentityMapper();
            } else {
                m = new ExtMapper();
            }
        } else {
            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;
        }
        String message = "Converting " + count + " file"
            + (count != 1 ? "s" : "") + " from ";
        log(message + srcDir + " to " + destDir);
        for (int i = 0; i < files.length; i++) {
            convert(files[i], m.mapFileName(files[i])[0]);
        }
    
public java.lang.String[]getCurrentArgs()
Returns the (implementation specific) settings given as nested arg elements.

return
the arguments.
since
Ant 1.6.3

        return facade.getArgs();
    
public java.lang.StringgetEncoding()
The value of the encoding attribute.

return
the encoding attribute.
since
Ant 1.6.3

        return encoding;
    
public booleangetReverse()
The value of the reverse attribute.

return
the reverse attribute.
since
Ant 1.6.3

        return reverse;
    
public voidsetDest(java.io.File destDir)
Set the destination directory to place converted files into.

param
destDir directory to place output file into.

        this.destDir = destDir;
    
public voidsetEncoding(java.lang.String encoding)
Set the encoding to translate to/from. If unset, the default encoding for the JVM is used.

param
encoding String containing the name of the Native encoding to convert from or to.

        this.encoding = encoding;
    
public voidsetExt(java.lang.String ext)
Set the extension which converted files should have. If unset, files will not be renamed.

param
ext File extension to use for converted files.

        this.extension = ext;
    
public voidsetImplementation(java.lang.String impl)
Choose the implementation for this particular task.

param
impl the name of the implemenation
since
Ant 1.6.3

        if ("default".equals(impl)) {
            facade.setImplementation(Native2AsciiAdapterFactory.getDefault());
        } else {
            facade.setImplementation(impl);
        }
    
public voidsetReverse(boolean reverse)
Flag the conversion to run in the reverse sense, that is Ascii to Native encoding.

param
reverse True if the conversion is to be reversed, otherwise false;

        this.reverse = reverse;
    
public voidsetSrc(java.io.File srcDir)
Set the source directory in which to find files to convert.

param
srcDir directory to find input file in.

        this.srcDir = srcDir;