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/java/org/apache/antlib/file FilteredCopyTask.java
Date Sun, 10 Feb 2002 00:17:43 GMT
donaldp     02/02/09 16:17:43

  Added:       proposal/myrmidon/src/java/org/apache/antlib/file
                        FilteredCopyTask.java
  Log:
  Add in a task that does filtered coping. It does it in the same was as Ant1.x excep that
it also acepts a character encoding and defaults to ascii. This is to workaround platform
specific deviations and also to make it easier to have more reliable builds.
  
  Revision  Changes    Path
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/file/FilteredCopyTask.java
  
  Index: FilteredCopyTask.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.antlib.file;
  
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.io.Reader;
  import java.io.UnsupportedEncodingException;
  import java.io.Writer;
  import java.util.ArrayList;
  import org.apache.avalon.excalibur.io.IOUtil;
  import org.apache.myrmidon.api.TaskException;
  import org.apache.tools.ant.types.FilterSet;
  import org.apache.tools.ant.types.FilterSetCollection;
  
  /**
   * A task used to copy files and simultaneously apply a
   * filter on said files.
   *
   * @ant:task name="filtered-copy"
   * @author <a href="mailto:peter@apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/02/10 00:17:43 $
   */
  public class FilteredCopyTask
      extends CopyTask
  {
      private ArrayList m_filterSets = new ArrayList();
      private FilterSetCollection m_filterSetCollection;
      private String m_encoding = "US-ASCII";
  
      public void addFilterSet( final FilterSet filterSet )
      {
          m_filterSets.add( filterSet );
      }
  
      public void setEncoding( final String encoding )
      {
          m_encoding = encoding;
      }
  
      /**
       * Utility method to perform operation to transform a single source file
       * to a destination.
       */
      protected void doOperation( final String sourceFilename,
                                  final String destinationFilename )
          throws IOException
      {
          final File source = new File( sourceFilename );
          final File destination = new File( destinationFilename );
  
          InputStream inputStream = null;
          OutputStream outputStream = null;
          BufferedReader input = null;
          BufferedWriter output = null;
          try
          {
              inputStream = new FileInputStream( source );
              outputStream = new FileOutputStream( destination );
  
              final Reader fileReader = new InputStreamReader( inputStream, m_encoding );
              final Writer fileWriter = new OutputStreamWriter( outputStream, m_encoding );
              input = new BufferedReader( fileReader );
              output = new BufferedWriter( fileWriter );
  
              process( input, output );
          }
          catch( final UnsupportedEncodingException uee )
          {
              throw new IOException( uee.toString() );
          }
          finally
          {
              IOUtil.shutdownReader( input );
              IOUtil.shutdownStream( inputStream );
              IOUtil.shutdownWriter( output );
              IOUtil.shutdownStream( outputStream );
          }
  
          if( isPreserveLastModified() )
          {
              destination.setLastModified( source.lastModified() );
          }
      }
  
      private void process( final BufferedReader input,
                            final BufferedWriter output )
          throws IOException
      {
          String newline = null;
          String line = input.readLine();
          while( null != line )
          {
              if( line.length() == 0 )
              {
                  output.newLine();
              }
              else
              {
                  newline = replaceTokens( line );
                  output.write( newline );
                  output.newLine();
              }
              line = input.readLine();
          }
      }
  
      private String replaceTokens( final String line )
          throws IOException
      {
          final FilterSetCollection filters = buildFilterSetCollection();
          try
          {
              return filters.replaceTokens( line );
          }
          catch( final TaskException te )
          {
              throw new IOException( te.getMessage() );
          }
      }
  
      private FilterSetCollection buildFilterSetCollection()
      {
          if( null == m_filterSetCollection )
          {
              m_filterSetCollection = new FilterSetCollection();
  
              final int size = m_filterSets.size();
              for( int i = 0; i < size; i++ )
              {
                  final FilterSet filterSet = (FilterSet)m_filterSets.get( i );
                  setupLogger( filterSet );
                  m_filterSetCollection.addFilterSet( filterSet );
              }
          }
  
          return m_filterSetCollection;
      }
  }
  
  
  

--
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