From 7515a67539ee296b056a86789290f958085850db Mon Sep 17 00:00:00 2001 From: adammurdoch Date: Thu, 21 Feb 2002 03:26:23 +0000 Subject: [PATCH] Added some experimental VFS data types and tasks: , and . git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271459 13f79535-47bb-0310-9956-ffa450edef68 --- proposal/myrmidon/build.xml | 3 +- .../org/apache/antlib/vfile/copy.ant | 8 + .../org/apache/antlib/vfile/src/emptyFile.txt | 0 .../org/apache/antlib/vfile/src/file1.txt | 1 + .../antlib/vfile/src/subdir1/someFile.html | 5 + .../apache/antlib/vfile/CopyFilesTask.java | 175 ++++++++++++++++++ .../apache/antlib/vfile/DefaultFileList.java | 76 ++++++++ .../antlib/vfile/DefaultFileSetResult.java | 50 +++++ .../org/apache/antlib/vfile/FileList.java | 34 ++++ .../java/org/apache/antlib/vfile/FileSet.java | 34 ++++ .../apache/antlib/vfile/FileSetResult.java | 28 +++ .../org/apache/antlib/vfile/PathFileList.java | 61 ++++++ .../apache/antlib/vfile/PatternFileSet.java | 114 ++++++++++++ .../apache/antlib/vfile/Resources.properties | 7 + .../antlib/vfile/SingletonFileList.java | 39 ++++ .../vfile/StringToFileObjectConverter.java | 57 ++++++ .../antlib/vfile/CopyFilesTaskTest.java | 35 ++++ .../antlib/vfile/CopyFilesTaskTest.java | 35 ++++ 18 files changed, 760 insertions(+), 2 deletions(-) create mode 100644 proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/copy.ant create mode 100644 proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/emptyFile.txt create mode 100644 proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/file1.txt create mode 100644 proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/subdir1/someFile.html create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/vfile/CopyFilesTask.java create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileList.java create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileSetResult.java create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/vfile/FileList.java create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSet.java create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSetResult.java create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/vfile/PathFileList.java create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/vfile/PatternFileSet.java create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/vfile/Resources.properties create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/vfile/SingletonFileList.java create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/vfile/StringToFileObjectConverter.java create mode 100644 proposal/myrmidon/src/test/org/apache/antlib/vfile/CopyFilesTaskTest.java create mode 100644 proposal/myrmidon/src/testcases/org/apache/antlib/vfile/CopyFilesTaskTest.java diff --git a/proposal/myrmidon/build.xml b/proposal/myrmidon/build.xml index 97ed68a8d..970bffb94 100644 --- a/proposal/myrmidon/build.xml +++ b/proposal/myrmidon/build.xml @@ -386,11 +386,9 @@ Legal: - + diff --git a/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/copy.ant b/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/copy.ant new file mode 100644 index 000000000..8b68c7057 --- /dev/null +++ b/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/copy.ant @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/emptyFile.txt b/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/emptyFile.txt new file mode 100644 index 000000000..e69de29bb diff --git a/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/file1.txt b/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/file1.txt new file mode 100644 index 000000000..9ad7b1b57 --- /dev/null +++ b/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/file1.txt @@ -0,0 +1 @@ +A test file. \ No newline at end of file diff --git a/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/subdir1/someFile.html b/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/subdir1/someFile.html new file mode 100644 index 000000000..c769030c0 --- /dev/null +++ b/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/subdir1/someFile.html @@ -0,0 +1,5 @@ + + +

Yo!

+ + diff --git a/proposal/myrmidon/src/java/org/apache/antlib/vfile/CopyFilesTask.java b/proposal/myrmidon/src/java/org/apache/antlib/vfile/CopyFilesTask.java new file mode 100644 index 000000000..a28759b36 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/vfile/CopyFilesTask.java @@ -0,0 +1,175 @@ +/* + * 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.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.Iterator; +import org.apache.aut.vfs.FileObject; +import org.apache.aut.vfs.FileSystemException; +import org.apache.avalon.excalibur.i18n.ResourceManager; +import org.apache.avalon.excalibur.i18n.Resources; +import org.apache.myrmidon.api.AbstractTask; +import org.apache.myrmidon.api.TaskException; + +/** + * A task that copies files. + * + * @author Adam Murdoch + * @ant:task name="v-copy" + */ +public class CopyFilesTask + extends AbstractTask +{ + private final static Resources REZ = + ResourceManager.getPackageResources( CopyFilesTask.class ); + + private FileObject m_srcFile; + private FileObject m_destFile; + private FileObject m_destDir; + private ArrayList m_fileSets = new ArrayList(); + + /** + * Sets the source file. + */ + public void setFile( final FileObject file ) + { + m_srcFile = file; + } + + /** + * Sets the destination file. + */ + public void setTofile( final FileObject file ) + { + m_destFile = file; + } + + /** + * Sets the destination directory. + */ + public void setTodir( final FileObject file ) + { + m_destDir = file; + } + + /** + * Adds a source file set. + */ + public void add( final FileSet fileset ) + { + m_fileSets.add( fileset ); + } + + /** + * Execute task. + * This method is called to perform actual work associated with task. + * It is called after Task has been Configured and Initialized and before + * beig Disposed (If task implements appropriate interfaces). + * + * @exception TaskException if an error occurs + */ + public void execute() + throws TaskException + { + if( m_srcFile == null && m_fileSets.size() == 0 ) + { + final String message = REZ.getString( "copyfilestask.no-source.error", getContext().getName() ); + throw new TaskException( message ); + } + if( m_destFile == null && m_destDir == null ) + { + final String message = REZ.getString( "copyfilestask.no-destination.error", getContext().getName() ); + throw new TaskException( message ); + } + if( m_fileSets.size() > 0 && m_destDir == null ) + { + final String message = REZ.getString( "copyfilestask.no-destination-dir.error", getContext().getName() ); + throw new TaskException( message ); + } + + try + { + // Copy the source file across + if( m_srcFile != null ) + { + if( m_destFile == null ) + { + m_destFile = m_destDir.resolveFile( m_srcFile.getName().getBaseName() ); + } + + copyFile( m_srcFile, m_destFile ); + } + + // Copy the contents of the filesets across + for( Iterator iterator = m_fileSets.iterator(); iterator.hasNext(); ) + { + FileSet fileset = (FileSet)iterator.next(); + FileSetResult result = fileset.getResult( getContext() ); + final FileObject[] files = result.getFiles(); + final String[] paths = result.getPaths(); + for( int i = 0; i < files.length; i++ ) + { + final FileObject srcFile = files[ i ]; + final String path = paths[ i ]; + + // TODO - map destination name + + // TODO - use scope here, to make sure that the result + // is a descendent of the dest dir + final FileObject destFile = m_destDir.resolveFile( path ); + copyFile( srcFile, destFile ); + } + } + } + catch( FileSystemException e ) + { + throw new TaskException( e.getMessage(), e ); + } + } + + /** + * Copies a file. + */ + private void copyFile( final FileObject srcFile, final FileObject destFile ) + throws TaskException + { + getLogger().info( "copy " + srcFile + " to " + destFile ); + + try + { + // TODO - move copy behind FileObject interface + InputStream instr = srcFile.getContent().getInputStream(); + try + { + OutputStream outstr = destFile.getContent().getOutputStream(); + byte[] buffer = new byte[ 4096 ]; + while( true ) + { + int nread = instr.read( buffer ); + if( nread == -1 ) + { + break; + } + outstr.write( buffer, 0, nread ); + } + outstr.close(); + } + finally + { + instr.close(); + } + } + catch( Exception exc ) + { + final String message = REZ.getString( "copyfilestask.copy-file.error", srcFile, destFile ); + throw new TaskException( message, exc ); + } + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileList.java b/proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileList.java new file mode 100644 index 000000000..b00df8b3f --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileList.java @@ -0,0 +1,76 @@ +/* + * 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 java.util.Iterator; +import java.util.List; +import org.apache.aut.vfs.FileObject; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.api.TaskException; + +/** + * A compound file list, which is made up of several other file lists. + * + * @author Adam Murdoch + * @ant:data-type name="v-path" + */ +public class DefaultFileList implements FileList +{ + private final List m_elements = new ArrayList(); + + /** + * Adds a single file to this list. + */ + public void addLocation( final FileObject file ) + { + final SingletonFileList element = new SingletonFileList(); + element.setFile( file ); + m_elements.add( element ); + } + + /** + * Adds a path to this list. + */ + public void addPath( final String pathStr ) + { + final PathFileList path = new PathFileList(); + path.setPath( pathStr ); + m_elements.add( path ); + } + + /** + * Adds a file list to this list. + */ + public void add( final FileList list ) + { + m_elements.add( list ); + } + + /** + * Returns the list of files. + */ + public FileObject[] listFiles( TaskContext context ) throws TaskException + { + // Collect the files from all elements + final ArrayList allFiles = new ArrayList(); + for( Iterator iterator = m_elements.iterator(); iterator.hasNext(); ) + { + FileList fileList = (FileList)iterator.next(); + FileObject[] files = fileList.listFiles( context ); + for( int i = 0; i < files.length; i++ ) + { + FileObject file = files[ i ]; + allFiles.add( file ); + } + } + + // Convert to array + return (FileObject[])allFiles.toArray( new FileObject[ allFiles.size() ] ); + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileSetResult.java b/proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileSetResult.java new file mode 100644 index 000000000..9e350a4aa --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileSetResult.java @@ -0,0 +1,50 @@ +/* + * 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 java.util.List; +import org.apache.aut.vfs.FileObject; + +/** + * An implementation of a file set result. + * + * @author Adam Murdoch + */ +public class DefaultFileSetResult + implements FileSetResult +{ + private List m_files = new ArrayList(); + private List m_paths = new ArrayList(); + + /** + * Adds an element to the result. + */ + public void addElement( final FileObject file, + final String path ) + { + m_files.add( file ); + m_paths.add( path ); + } + + /** + * Returns the files in the result. + */ + public FileObject[] getFiles() + { + return (FileObject[])m_files.toArray( new FileObject[ m_files.size() ] ); + } + + /** + * Returns the virtual paths of the files. + */ + public String[] getPaths() + { + return (String[])m_paths.toArray( new String[ m_paths.size() ] ); + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/vfile/FileList.java b/proposal/myrmidon/src/java/org/apache/antlib/vfile/FileList.java new file mode 100644 index 000000000..9f0c7e760 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/vfile/FileList.java @@ -0,0 +1,34 @@ +/* + * 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 org.apache.aut.vfs.FileObject; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.DataType; + +/** + * An ordered list of files. + * + * @author Adam Murdoch + * @ant:role shorthand="v-path" + */ +public interface FileList + extends DataType +{ + /** + * Returns the files in the list. + * + * @param context + * The context to use to build the list of files. + * + * @throws TaskException + * On error building the list of files. + */ + FileObject[] listFiles( TaskContext context ) throws TaskException; +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSet.java b/proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSet.java new file mode 100644 index 000000000..c56a68930 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSet.java @@ -0,0 +1,34 @@ +/* + * 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 org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.DataType; + +/** + * A set of files, where each file in the list has a virtual path associated + * with it. + * + * @author Adam Murdoch + * @ant:role shorthand="v-fileset" + */ +public interface FileSet + extends DataType +{ + /** + * Returns the contents of the set. + * + * @param context + * The context to use to build the set. + * + * @throws TaskException + * On error building the set. + */ + FileSetResult getResult( TaskContext context ) throws TaskException; +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSetResult.java b/proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSetResult.java new file mode 100644 index 000000000..6aa66de40 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSetResult.java @@ -0,0 +1,28 @@ +/* + * 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 org.apache.aut.vfs.FileObject; + +/** + * The contents of a {@link FileSet}. + * + * @author Adam Murdoch + */ +public interface FileSetResult +{ + /** + * Returns the files in the result. + */ + FileObject[] getFiles(); + + /** + * Returns the virtual paths of the files. + */ + String[] getPaths(); +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/vfile/PathFileList.java b/proposal/myrmidon/src/java/org/apache/antlib/vfile/PathFileList.java new file mode 100644 index 000000000..5975077f6 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/vfile/PathFileList.java @@ -0,0 +1,61 @@ +/* + * 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 org.apache.aut.vfs.FileObject; +import org.apache.aut.vfs.FileSystemException; +import org.apache.aut.vfs.FileSystemManager; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.api.TaskException; +import org.apache.tools.ant.util.FileUtils; + +/** + * A path made up of file names separated by ; and : characters. Similar to + * a CLASSPATH or PATH environment variable. + * + * @author Adam Murdoch + */ +public class PathFileList implements FileList +{ + private String m_path; + + /** + * Sets the path to use for this file list. + */ + public void setPath( final String path ) + { + m_path = path; + } + + /** + * Returns the list of files. + */ + public FileObject[] listFiles( final TaskContext context ) + throws TaskException + { + FileSystemManager fileSystemManager = (FileSystemManager)context.getService( FileSystemManager.class ); + + // TODO - move parsing to the VFS + final String[] elements = FileUtils.parsePath( m_path ); + final FileObject[] result = new FileObject[ elements.length ]; + for( int i = 0; i < elements.length; i++ ) + { + String element = elements[ i ]; + try + { + result[ i ] = fileSystemManager.resolveFile( element ); + } + catch( FileSystemException e ) + { + throw new TaskException( e.getMessage(), e ); + } + } + + return result; + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/vfile/PatternFileSet.java b/proposal/myrmidon/src/java/org/apache/antlib/vfile/PatternFileSet.java new file mode 100644 index 000000000..53d477a03 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/vfile/PatternFileSet.java @@ -0,0 +1,114 @@ +/* + * 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.aut.vfs.FileSystemException; +import org.apache.aut.vfs.FileType; +import org.apache.avalon.excalibur.i18n.ResourceManager; +import org.apache.avalon.excalibur.i18n.Resources; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.framework.AbstractFileSet; + +/** + * A file set, that contains those files under a directory that match + * a set of patterns. + * + * @author Adam Murdoch + * @ant:data-type name="v-fileset" + */ +public class PatternFileSet + extends AbstractFileSet + implements FileList, FileSet +{ + private final static Resources REZ = + ResourceManager.getPackageResources( PatternFileSet.class ); + + private FileObject m_dir; + + /** + * Sets the root directory. + */ + public void setDir( final FileObject dir ) + { + m_dir = dir; + } + + /** + * Returns the root directory + */ + public FileObject getDir() + { + return m_dir; + } + + /** + * Returns the list of files, in depthwise order. + */ + public FileObject[] listFiles( TaskContext context ) throws TaskException + { + final FileSetResult result = getResult( context ); + return result.getFiles(); + } + + /** + * Returns the contents of the set. + */ + public FileSetResult getResult( TaskContext context ) throws TaskException + { + if( m_dir == null ) + { + final String message = REZ.getString( "fileset.dir-not-set.error" ); + throw new TaskException( message ); + } + + try + { + final DefaultFileSetResult result = new DefaultFileSetResult(); + final ArrayList stack = new ArrayList(); + final ArrayList pathStack = new ArrayList(); + stack.add( m_dir ); + pathStack.add( "." ); + + while( stack.size() > 0 ) + { + // Pop next folder off the stack + FileObject folder = (FileObject)stack.remove( 0 ); + String path = (String)pathStack.remove( 0 ); + + // Queue the children of the folder + FileObject[] children = folder.getChildren(); + for( int i = 0; i < children.length; i++ ) + { + FileObject child = children[ i ]; + String childPath = path + '/' + child.getName().getBaseName(); + if( child.getType() == FileType.FILE ) + { + // A regular file - add it straight to the result + result.addElement( child, childPath ); + } + else + { + // A folder - push it on to the stack + stack.add( 0, child ); + pathStack.add( 0, childPath ); + } + } + } + + return result; + } + catch( FileSystemException e ) + { + final String message = REZ.getString( "fileset.list-files.error", m_dir ); + throw new TaskException( message, e ); + } + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/vfile/Resources.properties b/proposal/myrmidon/src/java/org/apache/antlib/vfile/Resources.properties new file mode 100644 index 000000000..ccaed603b --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/vfile/Resources.properties @@ -0,0 +1,7 @@ +bad-convert-string-to-file.error=Could not convert URI "{0}" into a file object. +fileset.dir-not-set.error=Fileset root directory is not set. +fileset.list-files.error=Could not list the files in folder "{0}". +copyfilestask.no-source.error=No source files specified for {0} task. +copyfilestask.no-destination.error=No destination file or directory specified for {0} task. +copyfilestask.no-destination.error=No destination directory specified for {0} task. +copyfilestask.copy-file.error=Could not copy "{0}" to "{1}". diff --git a/proposal/myrmidon/src/java/org/apache/antlib/vfile/SingletonFileList.java b/proposal/myrmidon/src/java/org/apache/antlib/vfile/SingletonFileList.java new file mode 100644 index 000000000..595be62e7 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/vfile/SingletonFileList.java @@ -0,0 +1,39 @@ +/* + * 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 org.apache.aut.vfs.FileObject; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.api.TaskException; + +/** + * A file list that contains a single file. + * + * @author Adam Murdoch + * @ant:data-type name="v-file" + */ +public class SingletonFileList implements FileList +{ + private FileObject m_file; + + /** + * Sets the file to use for tils file list. + */ + public void setFile( final FileObject file ) + { + m_file = file; + } + + /** + * Returns the list of files. + */ + public FileObject[] listFiles( TaskContext context ) throws TaskException + { + return new FileObject[]{m_file}; + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/vfile/StringToFileObjectConverter.java b/proposal/myrmidon/src/java/org/apache/antlib/vfile/StringToFileObjectConverter.java new file mode 100644 index 000000000..3ff5e046d --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/vfile/StringToFileObjectConverter.java @@ -0,0 +1,57 @@ +/* + * 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 org.apache.aut.vfs.FileObject; +import org.apache.aut.vfs.FileSystemManager; +import org.apache.avalon.excalibur.i18n.ResourceManager; +import org.apache.avalon.excalibur.i18n.Resources; +import org.apache.avalon.framework.context.Context; +import org.apache.myrmidon.api.TaskContext; +import org.apache.myrmidon.converter.AbstractConverter; +import org.apache.myrmidon.converter.ConverterException; + +/** + * Converts a String to a {@link FileObject} + * + * @author Adam Murdoch + * @ant:converter source="java.lang.String" destination="org.apache.aut.vfs.FileObject" + */ +public class StringToFileObjectConverter extends AbstractConverter +{ + private final static Resources REZ = + ResourceManager.getPackageResources( StringToFileObjectConverter.class ); + + public StringToFileObjectConverter() + { + super( String.class, FileObject.class ); + } + + /** + * Converts a String into a FileObject. + */ + protected Object convert( Object original, Context context ) + throws ConverterException + { + final String fileUri = (String)original; + final TaskContext taskContext = (TaskContext)context; + + try + { + final FileSystemManager manager = (FileSystemManager)taskContext.getService( FileSystemManager.class ); + + // TODO - change TaskContext.getBaseDirectory() to return a FileObject + return manager.resolveFile( taskContext.getBaseDirectory(), fileUri ); + } + catch( Exception e ) + { + final String message = REZ.getString( "bad-convert-string-to-file.error", fileUri ); + throw new ConverterException( message, e ); + } + } +} diff --git a/proposal/myrmidon/src/test/org/apache/antlib/vfile/CopyFilesTaskTest.java b/proposal/myrmidon/src/test/org/apache/antlib/vfile/CopyFilesTaskTest.java new file mode 100644 index 000000000..87f7030ae --- /dev/null +++ b/proposal/myrmidon/src/test/org/apache/antlib/vfile/CopyFilesTaskTest.java @@ -0,0 +1,35 @@ +/* + * 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 org.apache.myrmidon.AbstractProjectTest; +import java.io.File; + +/** + * Test cases for the task. + * + * @author Adam Murdoch + * @version $Revision$ $Date$ + */ +public class CopyFilesTaskTest + extends AbstractProjectTest +{ + public CopyFilesTaskTest( String name ) + { + super( name ); + } + + /** + * A simple smoke test. + */ + public void testCopy() throws Exception + { + final File projectFile = getTestResource( "copy.ant" ); + executeTarget( projectFile, "copy" ); + } +} diff --git a/proposal/myrmidon/src/testcases/org/apache/antlib/vfile/CopyFilesTaskTest.java b/proposal/myrmidon/src/testcases/org/apache/antlib/vfile/CopyFilesTaskTest.java new file mode 100644 index 000000000..87f7030ae --- /dev/null +++ b/proposal/myrmidon/src/testcases/org/apache/antlib/vfile/CopyFilesTaskTest.java @@ -0,0 +1,35 @@ +/* + * 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 org.apache.myrmidon.AbstractProjectTest; +import java.io.File; + +/** + * Test cases for the task. + * + * @author Adam Murdoch + * @version $Revision$ $Date$ + */ +public class CopyFilesTaskTest + extends AbstractProjectTest +{ + public CopyFilesTaskTest( String name ) + { + super( name ); + } + + /** + * A simple smoke test. + */ + public void testCopy() throws Exception + { + final File projectFile = getTestResource( "copy.ant" ); + executeTarget( projectFile, "copy" ); + } +}