Browse Source

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.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271258 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Donald 23 years ago
parent
commit
dc89e4bd22
1 changed files with 152 additions and 0 deletions
  1. +152
    -0
      proposal/myrmidon/src/java/org/apache/antlib/file/FilteredCopyTask.java

+ 152
- 0
proposal/myrmidon/src/java/org/apache/antlib/file/FilteredCopyTask.java View File

@@ -0,0 +1,152 @@
/*
* 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$ $Date$
*/
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;
}
}

Loading…
Cancel
Save