adammurdoch 02/02/20 19:26:23
Modified: proposal/myrmidon build.xml
Added: proposal/myrmidon/etc/testcases/org/apache/antlib/vfile
copy.ant
proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src
emptyFile.txt file1.txt
proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/subdir1
someFile.html
proposal/myrmidon/src/java/org/apache/antlib/vfile
CopyFilesTask.java DefaultFileList.java
DefaultFileSetResult.java FileList.java
FileSet.java FileSetResult.java PathFileList.java
PatternFileSet.java Resources.properties
SingletonFileList.java
StringToFileObjectConverter.java
proposal/myrmidon/src/testcases/org/apache/antlib/vfile
CopyFilesTaskTest.java
Log:
Added some experimental VFS data types and tasks: <v-path>, <v-fileset> and
<v-copy>.
Revision Changes Path
1.61 +1 -2 jakarta-ant/proposal/myrmidon/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-ant/proposal/myrmidon/build.xml,v
retrieving revision 1.60
retrieving revision 1.61
diff -u -r1.60 -r1.61
--- build.xml 20 Feb 2002 11:31:52 -0000 1.60
+++ build.xml 21 Feb 2002 03:26:23 -0000 1.61
@@ -386,11 +386,9 @@
<property name="antlib.name" value="sound"/>
</ant>
- <!--
<ant antfile="antlib.xml">
<property name="antlib.name" value="vfile"/>
</ant>
- -->
<antlib-jar jarfile="${build.lib}/selftest.atl"
basedir="${build.classes}"
@@ -434,6 +432,7 @@
<!-- Prepare the VFS tests -->
<property name="test.vfs.dir"
location="${test.working.dir}/org/apache/aut/vfs"/>
+ <mkdir dir="${test.vfs.dir}/write-tests"/>
<mkdir dir="${test.vfs.dir}/basedir/emptydir"/>
<zip zipfile="${test.vfs.dir}/test.zip">
<fileset dir="${test.vfs.dir}" includes="basedir/**"/>
1.1
jakarta-ant/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/copy.ant
Index: copy.ant
===================================================================
<project version="2.0">
<target name="copy">
<v-fileset id="src-files" dir="src"/>
<v-copy todir="dest">
<v-fileset-ref id="src-files"/>
</v-copy>
</target>
</project>
1.1
jakarta-ant/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/emptyFile.txt
<<Binary file>>
1.1
jakarta-ant/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/file1.txt
Index: file1.txt
===================================================================
A test file.
1.1
jakarta-ant/proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/subdir1/someFile.html
Index: someFile.html
===================================================================
<html>
<body>
<p>Yo!</p>
</body>
</html>
1.1
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/vfile/CopyFilesTask.java
Index: CopyFilesTask.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.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 <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @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 );
}
}
}
1.1
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileList.java
Index: DefaultFileList.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.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 <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @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() ] );
}
}
1.1
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileSetResult.java
Index: DefaultFileSetResult.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.vfile;
import java.util.ArrayList;
import java.util.List;
import org.apache.aut.vfs.FileObject;
/**
* An implementation of a file set result.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
*/
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() ] );
}
}
1.1
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/vfile/FileList.java
Index: FileList.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.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 <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @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;
}
1.1
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSet.java
Index: FileSet.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.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 <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @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;
}
1.1
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSetResult.java
Index: FileSetResult.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.vfile;
import org.apache.aut.vfs.FileObject;
/**
* The contents of a [EMAIL PROTECTED] FileSet}.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
*/
public interface FileSetResult
{
/**
* Returns the files in the result.
*/
FileObject[] getFiles();
/**
* Returns the virtual paths of the files.
*/
String[] getPaths();
}
1.1
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/vfile/PathFileList.java
Index: PathFileList.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.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 <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
*/
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;
}
}
1.1
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/vfile/PatternFileSet.java
Index: PatternFileSet.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.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 <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @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 );
}
}
}
1.1
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/vfile/Resources.properties
Index: Resources.properties
===================================================================
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}".
1.1
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/vfile/SingletonFileList.java
Index: SingletonFileList.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.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 <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @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};
}
}
1.1
jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/vfile/StringToFileObjectConverter.java
Index: StringToFileObjectConverter.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.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 [EMAIL PROTECTED] FileObject}
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @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 );
}
}
}
1.1
jakarta-ant/proposal/myrmidon/src/testcases/org/apache/antlib/vfile/CopyFilesTaskTest.java
Index: CopyFilesTaskTest.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.antlib.vfile;
import org.apache.myrmidon.AbstractProjectTest;
import java.io.File;
/**
* Test cases for the <v-copy> task.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/02/21 03:26:23 $
*/
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" );
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>