diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/ExtFileNameMapper.java b/proposal/myrmidon/src/java/org/apache/antlib/core/ExtFileNameMapper.java new file mode 100644 index 000000000..896ae9d9e --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/ExtFileNameMapper.java @@ -0,0 +1,43 @@ +/* + * 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.core; + +import org.apache.avalon.excalibur.io.FileUtil; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.FileNameMapper; + +/** + * Maps file extensions. + * + * @ant:type type="mapper" name="map-extension" + */ +public class ExtFileNameMapper + implements FileNameMapper +{ + private String m_extension; + + public void setExtension( final String extension ) + { + m_extension = extension; + } + + public String[] mapFileName( final String filename, TaskContext context ) + throws TaskException + { + final String name = FileUtil.removeExtension( filename ); + if( m_extension != null ) + { + return new String[]{ name + '.' + m_extension }; + } + else + { + return new String[]{ name }; + } + } +} diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/FlatFileNameMapper.java b/proposal/myrmidon/src/java/org/apache/antlib/core/FlatFileNameMapper.java similarity index 59% rename from proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/FlatFileNameMapper.java rename to proposal/myrmidon/src/java/org/apache/antlib/core/FlatFileNameMapper.java index 29b031379..8a8bf57de 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/FlatFileNameMapper.java +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/FlatFileNameMapper.java @@ -5,35 +5,25 @@ * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ -package org.apache.tools.ant.util.mappers; +package org.apache.antlib.core; -import java.io.File; +import org.apache.avalon.excalibur.io.FileUtil; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.FileNameMapper; /** * Implementation of FileNameMapper that always returns the source file name * without any leading directory information.
* - * This is the default FileNameMapper for the copy and move tasks if the flatten - * attribute has been set.
- * * @author Stefan Bodewig + * + * @ant:type type="mapper" name="flatten" */ public class FlatFileNameMapper + extends PrefixFileNameMapper implements FileNameMapper { - /** - * Ignored. - */ - public void setFrom( final String from ) - { - } - - /** - * Ignored. - */ - public void setTo( final String to ) - { - } /** * Returns an one-element array containing the source file name without any @@ -42,8 +32,10 @@ public class FlatFileNameMapper * @param sourceFileName Description of Parameter * @return Description of the Returned Value */ - public String[] mapFileName( final String sourceFileName ) + public String[] mapFileName( final String sourceFileName, TaskContext context ) + throws TaskException { - return new String[]{new File( sourceFileName ).getName()}; + final String baseName = FileUtil.removePath( sourceFileName, '/' ); + return super.mapFileName( baseName, context ); } } diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/PrefixFileNameMapper.java b/proposal/myrmidon/src/java/org/apache/antlib/core/PrefixFileNameMapper.java new file mode 100644 index 000000000..02d63ab1e --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/PrefixFileNameMapper.java @@ -0,0 +1,56 @@ +/* + * 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.core; + +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.FileNameMapper; + +/** + * A filename mapper that applies a prefix to each file. + * + * @author Adam Murdoch + * @version $Revision$ $Date$ + * + * @ant:type type="mapper" name="prefix" + */ +public class PrefixFileNameMapper + implements FileNameMapper +{ + private String m_prefix; + + /** + * Sets the prefix. + */ + public void setPrefix( final String prefix ) + { + m_prefix = prefix; + if( ! m_prefix.endsWith( "/" ) ) + { + m_prefix = m_prefix + '/'; + } + } + + /** + * Returns an array containing the target filename(s) for the given source + * file. + */ + public String[] mapFileName( final String sourceFileName, + final TaskContext context ) + throws TaskException + { + if( m_prefix == null ) + { + return new String[]{ sourceFileName }; + } + else + { + return new String[] { m_prefix + sourceFileName }; + } + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/file/CopyTask.java b/proposal/myrmidon/src/java/org/apache/antlib/file/CopyTask.java index cdb61c72b..9dfe36bd3 100644 --- a/proposal/myrmidon/src/java/org/apache/antlib/file/CopyTask.java +++ b/proposal/myrmidon/src/java/org/apache/antlib/file/CopyTask.java @@ -18,14 +18,12 @@ import org.apache.avalon.excalibur.i18n.Resources; import org.apache.avalon.excalibur.io.FileUtil; import org.apache.myrmidon.api.AbstractTask; import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.FileNameMapper; import org.apache.tools.ant.types.DirectoryScanner; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.ScannerUtil; import org.apache.tools.ant.types.SourceFileScanner; -import org.apache.tools.ant.util.mappers.FileNameMapper; -import org.apache.tools.ant.util.mappers.FlatFileNameMapper; import org.apache.tools.ant.util.mappers.IdentityMapper; -import org.apache.tools.ant.util.mappers.Mapper; /** * This is a task used to copy files. @@ -50,9 +48,8 @@ public class CopyTask private File m_destDir; private boolean m_preserveLastModified; private boolean m_overwrite; - private boolean m_flatten; private boolean m_includeEmpty = true; - private Mapper m_mapper; + private FileNameMapper m_mapper; private HashMap m_fileMap = new HashMap(); private HashMap m_dirMap = new HashMap(); @@ -93,23 +90,10 @@ public class CopyTask m_overwrite = overwrite; } - /** - * When copying directory trees, the files can be "flattened" into a single - * directory. If there are multiple files with the same name in the source - * directory tree, only the first file will be copied into the "flattened" - * directory, unless the forceoverwrite attribute is true. - * - * @param flatten The new Flatten value - */ - public void setFlatten( final boolean flatten ) - { - m_flatten = flatten; - } - /** * Defines the FileNameMapper to use (nested mapper element). */ - public void addMapper( final Mapper mapper ) + public void addMapper( final FileNameMapper mapper ) throws TaskException { if( null != m_mapper ) @@ -269,7 +253,7 @@ public class CopyTask final String[] toCopy = buildFilenameList( files, mapper, sourceDir, destDir ); for( int i = 0; i < toCopy.length; i++ ) { - final String destFilename = mapper.mapFileName( toCopy[ i ] )[ 0 ]; + final String destFilename = mapper.mapFileName( toCopy[ i ], getContext() )[ 0 ]; final File source = new File( sourceDir, toCopy[ i ] ); final File destination = new File( destDir, destFilename ); map.put( source.getAbsolutePath(), destination.getAbsolutePath() ); @@ -292,7 +276,7 @@ public class CopyTask for( int i = 0; i < names.length; i++ ) { final String name = names[ i ]; - if( null != mapper.mapFileName( name ) ) + if( null != mapper.mapFileName( name, getContext() ) ) { list.add( name ); } @@ -304,7 +288,7 @@ public class CopyTask { final SourceFileScanner scanner = new SourceFileScanner(); setupLogger( scanner ); - return scanner.restrict( names, fromDir, toDir, mapper ); + return scanner.restrict( names, fromDir, toDir, mapper, getContext() ); } } @@ -406,11 +390,7 @@ public class CopyTask { if( null != m_mapper ) { - return m_mapper.getImplementation(); - } - else if( m_flatten ) - { - return new FlatFileNameMapper(); + return m_mapper; } else { diff --git a/proposal/myrmidon/src/java/org/apache/antlib/vfile/MappedFileSet.java b/proposal/myrmidon/src/java/org/apache/antlib/vfile/MappedFileSet.java new file mode 100644 index 000000000..3ae290456 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/vfile/MappedFileSet.java @@ -0,0 +1,95 @@ +/* + * 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.vfile; + +import java.util.ArrayList; +import org.apache.aut.vfs.FileObject; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.ChainFileNameMapper; + +/** + * A fileset that maps another fileset. + * + * @author Adam Murdoch + * @version $Revision$ $Date$ + * + * @ant:data-type name="mapped-fileset" + */ +public class MappedFileSet + implements FileSet +{ + private final ArrayList m_filesets = new ArrayList(); + private ChainFileNameMapper m_mapper = new ChainFileNameMapper(); + + /** + * Sets the mapper to use. + */ + public void setMapper( final ChainFileNameMapper mapper ) + { + m_mapper.add( mapper ); + } + + /** + * Sets the fileset to map. + */ + public void add( final FileSet fileset ) + { + m_filesets.add( fileset ); + } + + /** + * Returns the contents of the set. + */ + public FileSetResult getResult( final TaskContext context ) + throws TaskException + { + final DefaultFileSetResult result = new DefaultFileSetResult(); + + // Map each source fileset. + final int count = m_filesets.size(); + for( int i = 0; i < count; i++ ) + { + final FileSet fileSet = (FileSet)m_filesets.get(i ); + mapFileSet( fileSet, result, context ); + } + + return result; + } + + /** + * Maps the contents of a fileset. + */ + private void mapFileSet( final FileSet fileset, + final DefaultFileSetResult result, + final TaskContext context ) + throws TaskException + { + // Build the result from the nested fileset + FileSetResult origResult = fileset.getResult( context ); + final FileObject[] files = origResult.getFiles(); + final String[] paths = origResult.getPaths(); + + // Map each element of the result + for( int i = 0; i < files.length; i++ ) + { + final FileObject file = files[ i ]; + final String path = paths[ i ]; + String[] newPaths = m_mapper.mapFileName( path, context ); + if( newPaths == null ) + { + continue; + } + for( int j = 0; j < newPaths.length; j++ ) + { + String newPath = newPaths[j ]; + result.addElement( file, newPath ); + } + } + } +} diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/framework/ChainFileNameMapper.java b/proposal/myrmidon/src/java/org/apache/myrmidon/framework/ChainFileNameMapper.java new file mode 100644 index 000000000..9bb8937b9 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/framework/ChainFileNameMapper.java @@ -0,0 +1,85 @@ +/* + * 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.myrmidon.framework; + +import java.util.ArrayList; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.api.TaskException; + +/** + * A mapper that applies a chain of mappers. + * + * @author Adam Murdoch + * @version $Revision$ $Date$ + * + * @ant:type type="mapper" name="chain" + */ +public class ChainFileNameMapper + implements FileNameMapper +{ + private final ArrayList m_mappers = new ArrayList(); + + /** + * Adds a nested mapper. + */ + public void add( final FileNameMapper mapper ) + { + m_mappers.add( mapper ); + } + + /** + * Returns an array containing the target filename(s) for the given source + * file. + */ + public String[] mapFileName( final String sourceFileName, + final TaskContext context ) + throws TaskException + { + ArrayList names = new ArrayList(); + names.add( sourceFileName ); + + final int count = m_mappers.size(); + for( int i = 0; i < count; i++ ) + { + final FileNameMapper mapper = (FileNameMapper)m_mappers.get( i ); + names = mapNames( mapper, names, context ); + } + + return (String[])names.toArray( new String[ names.size() ] ); + } + + /** + * Maps a set of names. + */ + private ArrayList mapNames( final FileNameMapper mapper, + final ArrayList names, + final TaskContext context ) + throws TaskException + { + final ArrayList retval = new ArrayList(); + + // Map each of the supplied names + final int count = names.size(); + for( int i = 0; i < count; i++ ) + { + final String name = (String)names.get( i ); + final String[] newNames = mapper.mapFileName( name, context ); + if( newNames == null ) + { + continue; + } + for( int j = 0; j < newNames.length; j++ ) + { + final String newName = newNames[ j ]; + retval.add( newName ); + } + } + + return retval; + } +} diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/FileNameMapper.java b/proposal/myrmidon/src/java/org/apache/myrmidon/framework/FileNameMapper.java similarity index 64% rename from proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/FileNameMapper.java rename to proposal/myrmidon/src/java/org/apache/myrmidon/framework/FileNameMapper.java index 8d34e489c..9640171f6 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/FileNameMapper.java +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/framework/FileNameMapper.java @@ -5,8 +5,9 @@ * version 1.1, a copy of which has been included with this distribution in * the LICENSE.txt file. */ -package org.apache.tools.ant.util.mappers; +package org.apache.myrmidon.framework; +import org.apache.myrmidon.api.TaskContext; import org.apache.myrmidon.api.TaskException; /** @@ -20,36 +21,24 @@ import org.apache.myrmidon.api.TaskException; * * * @author Stefan Bodewig + * + * @ant:role shorthand="mapper" */ public interface FileNameMapper { - /** - * Sets the from part of the transformation rule. - * - * @param from The new From value - */ - void setFrom( String from ) - throws TaskException; - - /** - * Sets the to part of the transformation rule. - * - * @param to The new To value - */ - void setTo( String to ); - /** * Returns an array containing the target filename(s) for the given source - * file.+ * file. * - * if the given rule doesn't apply to the source file, implementation must - * return null. SourceFileScanner will then omit the source file in + *
if the given rule doesn't apply to the source file, implementation + * must return null. SourceFileScanner will then omit the source file in * question.
* * @param sourceFileName the name of the source file relative to some given * basedirectory. + * @param context the context to perform the mapping in. * @return Description of the Returned Value */ - String[] mapFileName( String sourceFileName ) + String[] mapFileName( String sourceFileName, TaskContext context ) throws TaskException; } diff --git a/proposal/myrmidon/src/java/org/apache/myrmidon/launcher/Main.java b/proposal/myrmidon/src/java/org/apache/myrmidon/launcher/Main.java index 50b3115e2..f9b58303f 100644 --- a/proposal/myrmidon/src/java/org/apache/myrmidon/launcher/Main.java +++ b/proposal/myrmidon/src/java/org/apache/myrmidon/launcher/Main.java @@ -116,7 +116,7 @@ public final class Main final String name = file.getName(); if( !name.endsWith( ".jar" ) && !name.endsWith( ".zip" ) ) { - //Ifnore files in lib dir that are not jars or zips + //Ignore files in lib dir that are not jars or zips continue; } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/UpToDate.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/UpToDate.java index 26d3decc2..902e6ad02 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/UpToDate.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/UpToDate.java @@ -10,13 +10,13 @@ package org.apache.tools.ant.taskdefs; import java.io.File; import java.util.ArrayList; import java.util.Iterator; +import org.apache.myrmidon.api.AbstractTask; import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.FileNameMapper; import org.apache.tools.ant.types.DirectoryScanner; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.ScannerUtil; import org.apache.tools.ant.types.SourceFileScanner; -import org.apache.tools.ant.util.mappers.FileNameMapper; -import org.apache.tools.ant.util.mappers.Mapper; import org.apache.tools.ant.util.mappers.MergingMapper; /** @@ -29,16 +29,15 @@ import org.apache.tools.ant.util.mappers.MergingMapper; * hnakamur@mc.neweb.ne.jp * @author Stefan Bodewig */ - -public class UpToDate extends MatchingTask +public class UpToDate + extends AbstractTask { - private ArrayList sourceFileSets = new ArrayList(); - - protected Mapper mapperElement = null; + private final ArrayList m_fileSets = new ArrayList(); + private FileNameMapper m_mapper; - private String _property; - private File _targetFile; - private String _value; + private String m_property; + private File m_targetFile; + private String m_value; /** * The property to set if the target file is more up to date than each of @@ -46,9 +45,9 @@ public class UpToDate extends MatchingTask * * @param property the name of the property to set if Target is up to date. */ - public void setProperty( String property ) + public void setProperty( final String property ) { - _property = property; + m_property = property; } /** @@ -57,9 +56,9 @@ public class UpToDate extends MatchingTask * * @param file the file which we are checking against. */ - public void setTargetFile( File file ) + public void setTargetFile( final File file ) { - _targetFile = file; + m_targetFile = file; } /** @@ -68,9 +67,9 @@ public class UpToDate extends MatchingTask * * @param value the value to set the property to if Target is up to date */ - public void setValue( String value ) + public void setValue( final String value ) { - _value = value; + m_value = value; } /** @@ -78,26 +77,22 @@ public class UpToDate extends MatchingTask * * @param fs The feature to be added to the Srcfiles attribute */ - public void addSrcfiles( FileSet fs ) + public void addSrcfiles( final FileSet fs ) { - sourceFileSets.add( fs ); + m_fileSets.add( fs ); } /** * Defines the FileNameMapper to use (nested mapper element). - * - * @return Description of the Returned Value - * @exception TaskException Description of Exception */ - public Mapper createMapper() + public void addMapper( final FileNameMapper mapper ) throws TaskException { - if( mapperElement != null ) + if( m_mapper != null ) { throw new TaskException( "Cannot define more than one mapper" ); } - mapperElement = new Mapper(); - return mapperElement; + m_mapper = mapper; } /** @@ -108,23 +103,23 @@ public class UpToDate extends MatchingTask public boolean eval() throws TaskException { - if( sourceFileSets.size() == 0 ) + if( m_fileSets.size() == 0 ) { throw new TaskException( "At least onediff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/text/ExtMapper.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/text/ExtMapper.java deleted file mode 100644 index 02d83c200..000000000 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/text/ExtMapper.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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.text; - -import org.apache.tools.ant.util.mappers.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}; - } - } -} diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/text/Native2Ascii.java b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/text/Native2Ascii.java index abb24d000..566ac9129 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/text/Native2Ascii.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/taskdefs/text/Native2Ascii.java @@ -9,13 +9,12 @@ package org.apache.tools.ant.taskdefs.text; import java.io.File; import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.FileNameMapper; 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.SourceFileScanner; -import org.apache.tools.ant.util.mappers.FileNameMapper; import org.apache.tools.ant.util.mappers.IdentityMapper; -import org.apache.tools.ant.util.mappers.Mapper; /** * Convert files from native encodings to ascii. @@ -30,8 +29,7 @@ public class Native2Ascii 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; + private FileNameMapper m_mapper; /** * Set the destination dirctory to place converted files into. @@ -55,17 +53,6 @@ public class Native2Ascii m_encoding = encoding; } - /** - * Set the extension which converted files should have. If unset, files will - * not be renamed. - * - * @param ext File extension to use for converted files. - */ - public void setExt( final String ext ) - { - m_ext = ext; - } - /** * Flag the conversion to run in the reverse sense, that is Ascii to Native * encoding. @@ -89,19 +76,15 @@ public class Native2Ascii /** * Defines the FileNameMapper to use (nested mapper element). - * - * @return Description of the Returned Value - * @exception TaskException Description of Exception */ - public Mapper createMapper() + public void createMapper( final FileNameMapper mapper ) throws TaskException { if( m_mapper != null ) { throw new TaskException( "Cannot define more than one mapper" ); } - m_mapper = new Mapper(); - return m_mapper; + m_mapper = mapper; } public void execute() @@ -115,7 +98,7 @@ public class Native2Ascii final SourceFileScanner sfs = new SourceFileScanner(); setupLogger( sfs ); final FileNameMapper mapper = buildMapper(); - files = sfs.restrict( files, m_srcDir, m_destDir, mapper ); + files = sfs.restrict( files, m_srcDir, m_destDir, mapper, getContext() ); int count = files.length; if( count == 0 ) { @@ -129,7 +112,7 @@ public class Native2Ascii for( int i = 0; i < files.length; i++ ) { - final String name = mapper.mapFileName( files[ i ] )[ 0 ]; + final String name = mapper.mapFileName( files[ i ], getContext() )[ 0 ]; convert( files[ i ], name ); } } @@ -140,18 +123,11 @@ public class Native2Ascii FileNameMapper mapper = null; if( m_mapper == null ) { - if( m_ext == null ) - { - mapper = new IdentityMapper(); - } - else - { - mapper = new ExtMapper( m_ext ); - } + mapper = new IdentityMapper(); } else { - mapper = m_mapper.getImplementation(); + mapper = m_mapper; } return mapper; @@ -169,9 +145,9 @@ public class Native2Ascii // 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 ) + if( m_srcDir.equals( m_destDir ) && m_mapper == null ) { - throw new TaskException( "The ext attribute or a mapper must be set if" + + throw new TaskException( "A mapper must be specified if" + " src and dest dirs are the same." ); } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/types/SourceFileScanner.java b/proposal/myrmidon/src/main/org/apache/tools/ant/types/SourceFileScanner.java index 91a108f65..30b794c64 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/types/SourceFileScanner.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/types/SourceFileScanner.java @@ -13,8 +13,9 @@ import java.util.Date; import org.apache.aut.nativelib.Os; import org.apache.avalon.excalibur.io.FileUtil; import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.myrmidon.api.TaskContext; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.util.mappers.FileNameMapper; +import org.apache.myrmidon.framework.FileNameMapper; /** * Utility class that collects the functionality of the various scanDir methods @@ -40,8 +41,11 @@ public class SourceFileScanner * @param mapper knows how to construct a target file names from source file * names. */ - public String[] restrict( String[] files, File srcDir, File destDir, - FileNameMapper mapper ) + public String[] restrict( final String[] files, + final File srcDir, + final File destDir, + final FileNameMapper mapper, + final TaskContext context ) throws TaskException { @@ -64,7 +68,7 @@ public class SourceFileScanner final ArrayList v = new ArrayList(); for( int i = 0; i < files.length; i++ ) { - final String[] targets = mapper.mapFileName( files[ i ] ); + final String[] targets = mapper.mapFileName( files[ i ], context ); if( targets == null || targets.length == 0 ) { final String message = files[ i ] + " skipped - don\'t know how to handle it"; @@ -130,10 +134,11 @@ public class SourceFileScanner public File[] restrictAsFiles( final String[] files, final File srcDir, final File destDir, - final FileNameMapper mapper ) + final FileNameMapper mapper, + final TaskContext context ) throws TaskException { - final String[] res = restrict( files, srcDir, destDir, mapper ); + final String[] res = restrict( files, srcDir, destDir, mapper, context ); final File[] result = new File[ res.length ]; for( int i = 0; i < res.length; i++ ) { diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/FileNameMapper.java b/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/FileNameMapper.java deleted file mode 100644 index 8d34e489c..000000000 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/FileNameMapper.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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.util.mappers; - -import org.apache.myrmidon.api.TaskException; - -/** - * Interface to be used by SourceFileScanner.
- * - * Used to find the name of the target file(s) corresponding to a source file. - *
- * - * The rule by which the file names are transformed is specified via the setFrom - * and setTo methods. The exact meaning of these is implementation dependent. - *
- * - * @author Stefan Bodewig - */ -public interface FileNameMapper -{ - /** - * Sets the from part of the transformation rule. - * - * @param from The new From value - */ - void setFrom( String from ) - throws TaskException; - - /** - * Sets the to part of the transformation rule. - * - * @param to The new To value - */ - void setTo( String to ); - - /** - * Returns an array containing the target filename(s) for the given source - * file.- * - * if the given rule doesn't apply to the source file, implementation must - * return null. SourceFileScanner will then omit the source file in - * question.
- * - * @param sourceFileName the name of the source file relative to some given - * basedirectory. - * @return Description of the Returned Value - */ - String[] mapFileName( String sourceFileName ) - throws TaskException; -} diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/GlobPatternMapper.java b/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/GlobPatternMapper.java index 0caaf1d45..117ca96c7 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/GlobPatternMapper.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/GlobPatternMapper.java @@ -7,6 +7,9 @@ */ package org.apache.tools.ant.util.mappers; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.framework.FileNameMapper; + /** * Implementation of FileNameMapper that does simple wildcard pattern * replacements.@@ -18,6 +21,8 @@ package org.apache.tools.ant.util.mappers; * This is one of the more useful Mappers, it is used by javac for example.
* * @author Stefan Bodewig + * + * @ant:type type="mapper" name="glob" */ public class GlobPatternMapper implements FileNameMapper @@ -101,7 +106,7 @@ public class GlobPatternMapper * @param sourceFileName Description of Parameter * @return Description of the Returned Value */ - public String[] mapFileName( final String sourceFileName ) + public String[] mapFileName( final String sourceFileName, TaskContext context ) { if( m_fromPrefix == null || !sourceFileName.startsWith( m_fromPrefix ) || @@ -124,7 +129,7 @@ public class GlobPatternMapper * @param name Description of Parameter * @return Description of the Returned Value */ - protected String extractVariablePart( final String name ) + private String extractVariablePart( final String name ) { return name.substring( m_prefixLength, name.length() - m_postfixLength ); diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/IdentityMapper.java b/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/IdentityMapper.java index f1bc43d63..e3ada4441 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/IdentityMapper.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/IdentityMapper.java @@ -7,43 +7,28 @@ */ package org.apache.tools.ant.util.mappers; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.framework.FileNameMapper; + /** * Implementation of FileNameMapper that always returns the source file name. ** - * This is the default FileNameMapper for the copy and move tasks.
- * * @author Stefan Bodewig + * + * @ant:type type="mapper" name="identity" */ public class IdentityMapper implements FileNameMapper { - /** - * Ignored. - * - * @param from The new From value - */ - public void setFrom( final String from ) - { - } - - /** - * Ignored. - * - * @param to The new To value - */ - public void setTo( final String to ) - { - } - /** * Returns an one-element array containing the source file name. * * @param sourceFileName Description of Parameter * @return Description of the Returned Value */ - public String[] mapFileName( final String sourceFileName ) + public String[] mapFileName( final String sourceFileName, TaskContext context ) { - return new String[]{sourceFileName}; + return new String[]{ sourceFileName }; } } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/Mapper.java b/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/Mapper.java deleted file mode 100644 index 4e9bd05fb..000000000 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/Mapper.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * 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.util.mappers; - -import java.net.URL; -import java.net.URLClassLoader; -import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.types.Path; -import org.apache.tools.ant.types.PathUtil; - -/** - * Element to define a FileNameMapper. - * - * @author Stefan Bodewig - */ -public class Mapper -{ - private MapperType m_type; - private String m_classname; - private Path m_classpath; - private String m_from; - private String m_to; - - /** - * Set the class name of the FileNameMapper to use. - * - * @param classname The new Classname value - */ - public void setClassname( final String classname ) - { - m_classname = classname; - } - - /** - * Set the classpath to load the FileNameMapper through (attribute). - * - * @param classpath The new Classpath value - */ - public void setClasspath( Path classpath ) - throws TaskException - { - if( m_classpath == null ) - { - m_classpath = classpath; - } - else - { - m_classpath.append( classpath ); - } - } - - /** - * Set the argument to FileNameMapper.setFrom - */ - public void setFrom( final String from ) - { - m_from = from; - } - - /** - * Set the argument to FileNameMapper.setTo - */ - public void setTo( final String to ) - { - m_to = to; - } - - /** - * Set the type of FileNameMapper to use. - */ - public void setType( MapperType type ) - { - m_type = type; - } - - /** - * Returns a fully configured FileNameMapper implementation. - * - * @return The Implementation value - * @exception TaskException Description of Exception - */ - public FileNameMapper getImplementation() - throws TaskException - { - if( m_type == null && m_classname == null ) - { - throw new TaskException( "one of the attributes type or classname is required" ); - } - - if( m_type != null && m_classname != null ) - { - throw new TaskException( "must not specify both type and classname attribute" ); - } - - try - { - if( m_type != null ) - { - m_classname = m_type.getImplementation(); - } - - Class c = null; - if( m_classpath == null ) - { - c = Class.forName( m_classname ); - } - else - { - final URL[] urls = PathUtil.toURLs( m_classpath ); - final URLClassLoader classLoader = new URLClassLoader( urls ); - c = classLoader.loadClass( m_classname ); - } - - FileNameMapper m = (FileNameMapper)c.newInstance(); - m.setFrom( m_from ); - m.setTo( m_to ); - return m; - } - catch( TaskException be ) - { - throw be; - } - catch( Throwable t ) - { - throw new TaskException( "Error", t ); - } - finally - { - if( m_type != null ) - { - m_classname = null; - } - } - } - - /** - * Set the classpath to load the FileNameMapper through (nested element). - * - * @return Description of the Returned Value - */ - public Path createClasspath() - throws TaskException - { - if( m_classpath == null ) - { - m_classpath = new Path(); - } - Path path1 = m_classpath; - final Path path = new Path(); - path1.addPath( path ); - return path; - } -} diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/MapperType.java b/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/MapperType.java deleted file mode 100644 index 6ccfe5657..000000000 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/MapperType.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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.util.mappers; - -import java.util.Properties; -import org.apache.tools.ant.types.EnumeratedAttribute; - -/** - * Class as Argument to FileNameMapper.setType. - */ -public class MapperType - extends EnumeratedAttribute -{ - private final Properties c_implementations; - - public MapperType() - { - c_implementations = new Properties(); - c_implementations.put( "identity", - "org.apache.tools.ant.util.IdentityMapper" ); - c_implementations.put( "flatten", - "org.apache.tools.ant.util.FlatFileNameMapper" ); - c_implementations.put( "glob", - "org.apache.tools.ant.util.GlobPatternMapper" ); - c_implementations.put( "merge", - "org.apache.tools.ant.util.MergingMapper" ); - c_implementations.put( "regexp", - "org.apache.tools.ant.util.RegexpPatternMapper" ); - } - - public String getImplementation() - { - return c_implementations.getProperty( getValue() ); - } - - public String[] getValues() - { - return new String[]{"identity", "flatten", "glob", "merge", "regexp"}; - } -} diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/MergingMapper.java b/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/MergingMapper.java index 598b8c515..d5bf1185e 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/MergingMapper.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/MergingMapper.java @@ -7,36 +7,31 @@ */ package org.apache.tools.ant.util.mappers; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.FileNameMapper; + /** * Implementation of FileNameMapper that always returns the same target file * name.* - * This is the default FileNameMapper for the archiving tasks and uptodate.
- * * @author Stefan Bodewig + * + * @ant:type type="mapper" name="merge" */ public class MergingMapper implements FileNameMapper { private String[] m_mergedFile; - /** - * Ignored. - * - * @param from The new From value - */ - public void setFrom( String from ) - { - } - /** * Sets the name of the merged file. * * @param to The new To value */ - public void setTo( String to ) + public void setTo( final String to ) { - m_mergedFile = new String[]{to}; + m_mergedFile = new String[]{ to }; } /** @@ -45,8 +40,13 @@ public class MergingMapper * @param sourceFileName Description of Parameter * @return Description of the Returned Value */ - public String[] mapFileName( final String sourceFileName ) + public String[] mapFileName( final String sourceFileName, TaskContext context ) + throws TaskException { + if( m_mergedFile == null ) + { + throw new TaskException( "Destination file was not specified." ); + } return m_mergedFile; } } diff --git a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/RegexpPatternMapper.java b/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/RegexpPatternMapper.java index 12dfb9973..f77e9e76a 100644 --- a/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/RegexpPatternMapper.java +++ b/proposal/myrmidon/src/main/org/apache/tools/ant/util/mappers/RegexpPatternMapper.java @@ -8,7 +8,9 @@ package org.apache.tools.ant.util.mappers; import java.util.ArrayList; +import org.apache.myrmidon.api.TaskContext; import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.FileNameMapper; import org.apache.tools.ant.util.regexp.RegexpMatcher; import org.apache.tools.ant.util.regexp.RegexpMatcherFactory; @@ -16,6 +18,8 @@ import org.apache.tools.ant.util.regexp.RegexpMatcherFactory; * Implementation of FileNameMapper that does regular expression replacements. * * @author Stefan Bodewig + * + * @ant:type type="mapper" name="regexp" */ public class RegexpPatternMapper implements FileNameMapper @@ -65,7 +69,7 @@ public class RegexpPatternMapper * @param sourceFileName Description of Parameter * @return Description of the Returned Value */ - public String[] mapFileName( final String sourceFileName ) + public String[] mapFileName( final String sourceFileName, TaskContext context ) throws TaskException { if( m_matcher == null || m_to == null || diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/UpToDate.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/UpToDate.java index 26d3decc2..902e6ad02 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/UpToDate.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/UpToDate.java @@ -10,13 +10,13 @@ package org.apache.tools.ant.taskdefs; import java.io.File; import java.util.ArrayList; import java.util.Iterator; +import org.apache.myrmidon.api.AbstractTask; import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.FileNameMapper; import org.apache.tools.ant.types.DirectoryScanner; import org.apache.tools.ant.types.FileSet; import org.apache.tools.ant.types.ScannerUtil; import org.apache.tools.ant.types.SourceFileScanner; -import org.apache.tools.ant.util.mappers.FileNameMapper; -import org.apache.tools.ant.util.mappers.Mapper; import org.apache.tools.ant.util.mappers.MergingMapper; /** @@ -29,16 +29,15 @@ import org.apache.tools.ant.util.mappers.MergingMapper; * hnakamur@mc.neweb.ne.jp * @author Stefan Bodewig */ - -public class UpToDate extends MatchingTask +public class UpToDate + extends AbstractTask { - private ArrayList sourceFileSets = new ArrayList(); - - protected Mapper mapperElement = null; + private final ArrayList m_fileSets = new ArrayList(); + private FileNameMapper m_mapper; - private String _property; - private File _targetFile; - private String _value; + private String m_property; + private File m_targetFile; + private String m_value; /** * The property to set if the target file is more up to date than each of @@ -46,9 +45,9 @@ public class UpToDate extends MatchingTask * * @param property the name of the property to set if Target is up to date. */ - public void setProperty( String property ) + public void setProperty( final String property ) { - _property = property; + m_property = property; } /** @@ -57,9 +56,9 @@ public class UpToDate extends MatchingTask * * @param file the file which we are checking against. */ - public void setTargetFile( File file ) + public void setTargetFile( final File file ) { - _targetFile = file; + m_targetFile = file; } /** @@ -68,9 +67,9 @@ public class UpToDate extends MatchingTask * * @param value the value to set the property to if Target is up to date */ - public void setValue( String value ) + public void setValue( final String value ) { - _value = value; + m_value = value; } /** @@ -78,26 +77,22 @@ public class UpToDate extends MatchingTask * * @param fs The feature to be added to the Srcfiles attribute */ - public void addSrcfiles( FileSet fs ) + public void addSrcfiles( final FileSet fs ) { - sourceFileSets.add( fs ); + m_fileSets.add( fs ); } /** * Defines the FileNameMapper to use (nested mapper element). - * - * @return Description of the Returned Value - * @exception TaskException Description of Exception */ - public Mapper createMapper() + public void addMapper( final FileNameMapper mapper ) throws TaskException { - if( mapperElement != null ) + if( m_mapper != null ) { throw new TaskException( "Cannot define more than one mapper" ); } - mapperElement = new Mapper(); - return mapperElement; + m_mapper = mapper; } /** @@ -108,23 +103,23 @@ public class UpToDate extends MatchingTask public boolean eval() throws TaskException { - if( sourceFileSets.size() == 0 ) + if( m_fileSets.size() == 0 ) { throw new TaskException( "At least onediff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/text/ExtMapper.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/text/ExtMapper.java deleted file mode 100644 index 02d83c200..000000000 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/text/ExtMapper.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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.text; - -import org.apache.tools.ant.util.mappers.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}; - } - } -} diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/text/Native2Ascii.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/text/Native2Ascii.java index abb24d000..566ac9129 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/text/Native2Ascii.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/taskdefs/text/Native2Ascii.java @@ -9,13 +9,12 @@ package org.apache.tools.ant.taskdefs.text; import java.io.File; import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.FileNameMapper; 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.SourceFileScanner; -import org.apache.tools.ant.util.mappers.FileNameMapper; import org.apache.tools.ant.util.mappers.IdentityMapper; -import org.apache.tools.ant.util.mappers.Mapper; /** * Convert files from native encodings to ascii. @@ -30,8 +29,7 @@ public class Native2Ascii 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; + private FileNameMapper m_mapper; /** * Set the destination dirctory to place converted files into. @@ -55,17 +53,6 @@ public class Native2Ascii m_encoding = encoding; } - /** - * Set the extension which converted files should have. If unset, files will - * not be renamed. - * - * @param ext File extension to use for converted files. - */ - public void setExt( final String ext ) - { - m_ext = ext; - } - /** * Flag the conversion to run in the reverse sense, that is Ascii to Native * encoding. @@ -89,19 +76,15 @@ public class Native2Ascii /** * Defines the FileNameMapper to use (nested mapper element). - * - * @return Description of the Returned Value - * @exception TaskException Description of Exception */ - public Mapper createMapper() + public void createMapper( final FileNameMapper mapper ) throws TaskException { if( m_mapper != null ) { throw new TaskException( "Cannot define more than one mapper" ); } - m_mapper = new Mapper(); - return m_mapper; + m_mapper = mapper; } public void execute() @@ -115,7 +98,7 @@ public class Native2Ascii final SourceFileScanner sfs = new SourceFileScanner(); setupLogger( sfs ); final FileNameMapper mapper = buildMapper(); - files = sfs.restrict( files, m_srcDir, m_destDir, mapper ); + files = sfs.restrict( files, m_srcDir, m_destDir, mapper, getContext() ); int count = files.length; if( count == 0 ) { @@ -129,7 +112,7 @@ public class Native2Ascii for( int i = 0; i < files.length; i++ ) { - final String name = mapper.mapFileName( files[ i ] )[ 0 ]; + final String name = mapper.mapFileName( files[ i ], getContext() )[ 0 ]; convert( files[ i ], name ); } } @@ -140,18 +123,11 @@ public class Native2Ascii FileNameMapper mapper = null; if( m_mapper == null ) { - if( m_ext == null ) - { - mapper = new IdentityMapper(); - } - else - { - mapper = new ExtMapper( m_ext ); - } + mapper = new IdentityMapper(); } else { - mapper = m_mapper.getImplementation(); + mapper = m_mapper; } return mapper; @@ -169,9 +145,9 @@ public class Native2Ascii // 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 ) + if( m_srcDir.equals( m_destDir ) && m_mapper == null ) { - throw new TaskException( "The ext attribute or a mapper must be set if" + + throw new TaskException( "A mapper must be specified if" + " src and dest dirs are the same." ); } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/types/SourceFileScanner.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/types/SourceFileScanner.java index 91a108f65..30b794c64 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/types/SourceFileScanner.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/types/SourceFileScanner.java @@ -13,8 +13,9 @@ import java.util.Date; import org.apache.aut.nativelib.Os; import org.apache.avalon.excalibur.io.FileUtil; import org.apache.avalon.framework.logger.AbstractLogEnabled; +import org.apache.myrmidon.api.TaskContext; import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.util.mappers.FileNameMapper; +import org.apache.myrmidon.framework.FileNameMapper; /** * Utility class that collects the functionality of the various scanDir methods @@ -40,8 +41,11 @@ public class SourceFileScanner * @param mapper knows how to construct a target file names from source file * names. */ - public String[] restrict( String[] files, File srcDir, File destDir, - FileNameMapper mapper ) + public String[] restrict( final String[] files, + final File srcDir, + final File destDir, + final FileNameMapper mapper, + final TaskContext context ) throws TaskException { @@ -64,7 +68,7 @@ public class SourceFileScanner final ArrayList v = new ArrayList(); for( int i = 0; i < files.length; i++ ) { - final String[] targets = mapper.mapFileName( files[ i ] ); + final String[] targets = mapper.mapFileName( files[ i ], context ); if( targets == null || targets.length == 0 ) { final String message = files[ i ] + " skipped - don\'t know how to handle it"; @@ -130,10 +134,11 @@ public class SourceFileScanner public File[] restrictAsFiles( final String[] files, final File srcDir, final File destDir, - final FileNameMapper mapper ) + final FileNameMapper mapper, + final TaskContext context ) throws TaskException { - final String[] res = restrict( files, srcDir, destDir, mapper ); + final String[] res = restrict( files, srcDir, destDir, mapper, context ); final File[] result = new File[ res.length ]; for( int i = 0; i < res.length; i++ ) { diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/FlatFileNameMapper.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/FlatFileNameMapper.java deleted file mode 100644 index 29b031379..000000000 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/FlatFileNameMapper.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.util.mappers; - -import java.io.File; - -/** - * Implementation of FileNameMapper that always returns the source file name - * without any leading directory information.
- * - * This is the default FileNameMapper for the copy and move tasks if the flatten - * attribute has been set.
- * - * @author Stefan Bodewig - */ -public class FlatFileNameMapper - implements FileNameMapper -{ - /** - * Ignored. - */ - public void setFrom( final String from ) - { - } - - /** - * Ignored. - */ - public void setTo( final String to ) - { - } - - /** - * Returns an one-element array containing the source file name without any - * leading directory information. - * - * @param sourceFileName Description of Parameter - * @return Description of the Returned Value - */ - public String[] mapFileName( final String sourceFileName ) - { - return new String[]{new File( sourceFileName ).getName()}; - } -} diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/GlobPatternMapper.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/GlobPatternMapper.java index 0caaf1d45..117ca96c7 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/GlobPatternMapper.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/GlobPatternMapper.java @@ -7,6 +7,9 @@ */ package org.apache.tools.ant.util.mappers; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.framework.FileNameMapper; + /** * Implementation of FileNameMapper that does simple wildcard pattern * replacements.@@ -18,6 +21,8 @@ package org.apache.tools.ant.util.mappers; * This is one of the more useful Mappers, it is used by javac for example.
* * @author Stefan Bodewig + * + * @ant:type type="mapper" name="glob" */ public class GlobPatternMapper implements FileNameMapper @@ -101,7 +106,7 @@ public class GlobPatternMapper * @param sourceFileName Description of Parameter * @return Description of the Returned Value */ - public String[] mapFileName( final String sourceFileName ) + public String[] mapFileName( final String sourceFileName, TaskContext context ) { if( m_fromPrefix == null || !sourceFileName.startsWith( m_fromPrefix ) || @@ -124,7 +129,7 @@ public class GlobPatternMapper * @param name Description of Parameter * @return Description of the Returned Value */ - protected String extractVariablePart( final String name ) + private String extractVariablePart( final String name ) { return name.substring( m_prefixLength, name.length() - m_postfixLength ); diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/IdentityMapper.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/IdentityMapper.java index f1bc43d63..e3ada4441 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/IdentityMapper.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/IdentityMapper.java @@ -7,43 +7,28 @@ */ package org.apache.tools.ant.util.mappers; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.framework.FileNameMapper; + /** * Implementation of FileNameMapper that always returns the source file name. ** - * This is the default FileNameMapper for the copy and move tasks.
- * * @author Stefan Bodewig + * + * @ant:type type="mapper" name="identity" */ public class IdentityMapper implements FileNameMapper { - /** - * Ignored. - * - * @param from The new From value - */ - public void setFrom( final String from ) - { - } - - /** - * Ignored. - * - * @param to The new To value - */ - public void setTo( final String to ) - { - } - /** * Returns an one-element array containing the source file name. * * @param sourceFileName Description of Parameter * @return Description of the Returned Value */ - public String[] mapFileName( final String sourceFileName ) + public String[] mapFileName( final String sourceFileName, TaskContext context ) { - return new String[]{sourceFileName}; + return new String[]{ sourceFileName }; } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/Mapper.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/Mapper.java deleted file mode 100644 index 4e9bd05fb..000000000 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/Mapper.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * 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.util.mappers; - -import java.net.URL; -import java.net.URLClassLoader; -import org.apache.myrmidon.api.TaskException; -import org.apache.tools.ant.types.Path; -import org.apache.tools.ant.types.PathUtil; - -/** - * Element to define a FileNameMapper. - * - * @author Stefan Bodewig - */ -public class Mapper -{ - private MapperType m_type; - private String m_classname; - private Path m_classpath; - private String m_from; - private String m_to; - - /** - * Set the class name of the FileNameMapper to use. - * - * @param classname The new Classname value - */ - public void setClassname( final String classname ) - { - m_classname = classname; - } - - /** - * Set the classpath to load the FileNameMapper through (attribute). - * - * @param classpath The new Classpath value - */ - public void setClasspath( Path classpath ) - throws TaskException - { - if( m_classpath == null ) - { - m_classpath = classpath; - } - else - { - m_classpath.append( classpath ); - } - } - - /** - * Set the argument to FileNameMapper.setFrom - */ - public void setFrom( final String from ) - { - m_from = from; - } - - /** - * Set the argument to FileNameMapper.setTo - */ - public void setTo( final String to ) - { - m_to = to; - } - - /** - * Set the type of FileNameMapper to use. - */ - public void setType( MapperType type ) - { - m_type = type; - } - - /** - * Returns a fully configured FileNameMapper implementation. - * - * @return The Implementation value - * @exception TaskException Description of Exception - */ - public FileNameMapper getImplementation() - throws TaskException - { - if( m_type == null && m_classname == null ) - { - throw new TaskException( "one of the attributes type or classname is required" ); - } - - if( m_type != null && m_classname != null ) - { - throw new TaskException( "must not specify both type and classname attribute" ); - } - - try - { - if( m_type != null ) - { - m_classname = m_type.getImplementation(); - } - - Class c = null; - if( m_classpath == null ) - { - c = Class.forName( m_classname ); - } - else - { - final URL[] urls = PathUtil.toURLs( m_classpath ); - final URLClassLoader classLoader = new URLClassLoader( urls ); - c = classLoader.loadClass( m_classname ); - } - - FileNameMapper m = (FileNameMapper)c.newInstance(); - m.setFrom( m_from ); - m.setTo( m_to ); - return m; - } - catch( TaskException be ) - { - throw be; - } - catch( Throwable t ) - { - throw new TaskException( "Error", t ); - } - finally - { - if( m_type != null ) - { - m_classname = null; - } - } - } - - /** - * Set the classpath to load the FileNameMapper through (nested element). - * - * @return Description of the Returned Value - */ - public Path createClasspath() - throws TaskException - { - if( m_classpath == null ) - { - m_classpath = new Path(); - } - Path path1 = m_classpath; - final Path path = new Path(); - path1.addPath( path ); - return path; - } -} diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/MapperType.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/MapperType.java deleted file mode 100644 index 6ccfe5657..000000000 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/MapperType.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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.util.mappers; - -import java.util.Properties; -import org.apache.tools.ant.types.EnumeratedAttribute; - -/** - * Class as Argument to FileNameMapper.setType. - */ -public class MapperType - extends EnumeratedAttribute -{ - private final Properties c_implementations; - - public MapperType() - { - c_implementations = new Properties(); - c_implementations.put( "identity", - "org.apache.tools.ant.util.IdentityMapper" ); - c_implementations.put( "flatten", - "org.apache.tools.ant.util.FlatFileNameMapper" ); - c_implementations.put( "glob", - "org.apache.tools.ant.util.GlobPatternMapper" ); - c_implementations.put( "merge", - "org.apache.tools.ant.util.MergingMapper" ); - c_implementations.put( "regexp", - "org.apache.tools.ant.util.RegexpPatternMapper" ); - } - - public String getImplementation() - { - return c_implementations.getProperty( getValue() ); - } - - public String[] getValues() - { - return new String[]{"identity", "flatten", "glob", "merge", "regexp"}; - } -} diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/MergingMapper.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/MergingMapper.java index 598b8c515..d5bf1185e 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/MergingMapper.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/MergingMapper.java @@ -7,36 +7,31 @@ */ package org.apache.tools.ant.util.mappers; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.FileNameMapper; + /** * Implementation of FileNameMapper that always returns the same target file * name.* - * This is the default FileNameMapper for the archiving tasks and uptodate.
- * * @author Stefan Bodewig + * + * @ant:type type="mapper" name="merge" */ public class MergingMapper implements FileNameMapper { private String[] m_mergedFile; - /** - * Ignored. - * - * @param from The new From value - */ - public void setFrom( String from ) - { - } - /** * Sets the name of the merged file. * * @param to The new To value */ - public void setTo( String to ) + public void setTo( final String to ) { - m_mergedFile = new String[]{to}; + m_mergedFile = new String[]{ to }; } /** @@ -45,8 +40,13 @@ public class MergingMapper * @param sourceFileName Description of Parameter * @return Description of the Returned Value */ - public String[] mapFileName( final String sourceFileName ) + public String[] mapFileName( final String sourceFileName, TaskContext context ) + throws TaskException { + if( m_mergedFile == null ) + { + throw new TaskException( "Destination file was not specified." ); + } return m_mergedFile; } } diff --git a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/RegexpPatternMapper.java b/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/RegexpPatternMapper.java index 12dfb9973..f77e9e76a 100644 --- a/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/RegexpPatternMapper.java +++ b/proposal/myrmidon/src/todo/org/apache/tools/ant/util/mappers/RegexpPatternMapper.java @@ -8,7 +8,9 @@ package org.apache.tools.ant.util.mappers; import java.util.ArrayList; +import org.apache.myrmidon.api.TaskContext; import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.FileNameMapper; import org.apache.tools.ant.util.regexp.RegexpMatcher; import org.apache.tools.ant.util.regexp.RegexpMatcherFactory; @@ -16,6 +18,8 @@ import org.apache.tools.ant.util.regexp.RegexpMatcherFactory; * Implementation of FileNameMapper that does regular expression replacements. * * @author Stefan Bodewig + * + * @ant:type type="mapper" name="regexp" */ public class RegexpPatternMapper implements FileNameMapper @@ -65,7 +69,7 @@ public class RegexpPatternMapper * @param sourceFileName Description of Parameter * @return Description of the Returned Value */ - public String[] mapFileName( final String sourceFileName ) + public String[] mapFileName( final String sourceFileName, TaskContext context ) throws TaskException { if( m_matcher == null || m_to == null ||