ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dona...@apache.org
Subject cvs commit: jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional ExtMapper.java Native2Ascii.java
Date Sun, 13 Jan 2002 00:17:55 GMT
donaldp     02/01/12 16:17:54

  Modified:    proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional
                        Native2Ascii.java
  Added:       proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional
                        ExtMapper.java
  Log:
  Cleanup Native2Ascii a little and extract inner class to be a top-level class.
  
  Revision  Changes    Path
  1.11      +92 -105   jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/Native2Ascii.java
  
  Index: Native2Ascii.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/Native2Ascii.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Native2Ascii.java	12 Jan 2002 23:54:23 -0000	1.10
  +++ Native2Ascii.java	13 Jan 2002 00:17:54 -0000	1.11
  @@ -9,13 +9,13 @@
   
   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 @@
    * @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 @@
        * @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 @@
        *
        * @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 void setReverse( boolean reverse )
       {
  -        this.reverse = reverse;
  +        m_reverse = reverse;
       }
   
       /**
  @@ -83,9 +82,9 @@
        *
        * @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 Mapper createMapper()
           throws TaskException
       {
  -        if( mapper != null )
  +        if( m_mapper != null )
           {
               throw new TaskException( "Cannot define more than one mapper" );
           }
  -        mapper = new Mapper();
  -        return mapper;
  +        m_mapper = new Mapper();
  +        return m_mapper;
       }
   
       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 @@
       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 @@
                                        + " 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;
       }
   }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/optional/ExtMapper.java
  
  Index: ExtMapper.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.tools.ant.taskdefs.optional;
  
  import org.apache.tools.ant.util.FileNameMapper;
  
  class ExtMapper
      implements FileNameMapper
  {
      private final String m_extension;
  
      public ExtMapper( final String extension )
      {
          m_extension = extension;
      }
  
      public void setFrom( final String from )
      {
      }
  
      public void setTo( final String to )
      {
      }
  
      public String[] mapFileName( final String filename )
      {
          final int index = filename.lastIndexOf( '.' );
          if( index >= 0 )
          {
              final String reult = filename.substring( 0, index ) + m_extension;
              return new String[]{reult};
          }
          else
          {
              return new String[]{filename + m_extension};
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message