Browse Source

Added some experimental VFS data types and tasks: <v-path>, <v-fileset> and <v-copy>.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271459 13f79535-47bb-0310-9956-ffa450edef68
master
adammurdoch 23 years ago
parent
commit
7515a67539
18 changed files with 760 additions and 2 deletions
  1. +1
    -2
      proposal/myrmidon/build.xml
  2. +8
    -0
      proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/copy.ant
  3. +0
    -0
      proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/emptyFile.txt
  4. +1
    -0
      proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/file1.txt
  5. +5
    -0
      proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/subdir1/someFile.html
  6. +175
    -0
      proposal/myrmidon/src/java/org/apache/antlib/vfile/CopyFilesTask.java
  7. +76
    -0
      proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileList.java
  8. +50
    -0
      proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileSetResult.java
  9. +34
    -0
      proposal/myrmidon/src/java/org/apache/antlib/vfile/FileList.java
  10. +34
    -0
      proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSet.java
  11. +28
    -0
      proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSetResult.java
  12. +61
    -0
      proposal/myrmidon/src/java/org/apache/antlib/vfile/PathFileList.java
  13. +114
    -0
      proposal/myrmidon/src/java/org/apache/antlib/vfile/PatternFileSet.java
  14. +7
    -0
      proposal/myrmidon/src/java/org/apache/antlib/vfile/Resources.properties
  15. +39
    -0
      proposal/myrmidon/src/java/org/apache/antlib/vfile/SingletonFileList.java
  16. +57
    -0
      proposal/myrmidon/src/java/org/apache/antlib/vfile/StringToFileObjectConverter.java
  17. +35
    -0
      proposal/myrmidon/src/test/org/apache/antlib/vfile/CopyFilesTaskTest.java
  18. +35
    -0
      proposal/myrmidon/src/testcases/org/apache/antlib/vfile/CopyFilesTaskTest.java

+ 1
- 2
proposal/myrmidon/build.xml View File

@@ -386,11 +386,9 @@ Legal:
<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 @@ Legal:

<!-- 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/**"/>


+ 8
- 0
proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/copy.ant View File

@@ -0,0 +1,8 @@
<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>

+ 0
- 0
proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/emptyFile.txt View File


+ 1
- 0
proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/file1.txt View File

@@ -0,0 +1 @@
A test file.

+ 5
- 0
proposal/myrmidon/etc/testcases/org/apache/antlib/vfile/src/subdir1/someFile.html View File

@@ -0,0 +1,5 @@
<html>
<body>
<p>Yo!</p>
</body>
</html>

+ 175
- 0
proposal/myrmidon/src/java/org/apache/antlib/vfile/CopyFilesTask.java View File

@@ -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 <a href="mailto:adammurdoch@apache.org">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 );
}
}
}

+ 76
- 0
proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileList.java View File

@@ -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 <a href="mailto:adammurdoch@apache.org">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() ] );
}
}

+ 50
- 0
proposal/myrmidon/src/java/org/apache/antlib/vfile/DefaultFileSetResult.java View File

@@ -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 <a href="mailto:adammurdoch@apache.org">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() ] );
}
}

+ 34
- 0
proposal/myrmidon/src/java/org/apache/antlib/vfile/FileList.java View File

@@ -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 <a href="mailto:adammurdoch@apache.org">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;
}

+ 34
- 0
proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSet.java View File

@@ -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 <a href="mailto:adammurdoch@apache.org">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;
}

+ 28
- 0
proposal/myrmidon/src/java/org/apache/antlib/vfile/FileSetResult.java View File

@@ -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 <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
*/
public interface FileSetResult
{
/**
* Returns the files in the result.
*/
FileObject[] getFiles();

/**
* Returns the virtual paths of the files.
*/
String[] getPaths();
}

+ 61
- 0
proposal/myrmidon/src/java/org/apache/antlib/vfile/PathFileList.java View File

@@ -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 <a href="mailto:adammurdoch@apache.org">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;
}
}

+ 114
- 0
proposal/myrmidon/src/java/org/apache/antlib/vfile/PatternFileSet.java View File

@@ -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 <a href="mailto:adammurdoch@apache.org">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 );
}
}
}

+ 7
- 0
proposal/myrmidon/src/java/org/apache/antlib/vfile/Resources.properties View File

@@ -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}".

+ 39
- 0
proposal/myrmidon/src/java/org/apache/antlib/vfile/SingletonFileList.java View File

@@ -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 <a href="mailto:adammurdoch@apache.org">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};
}
}

+ 57
- 0
proposal/myrmidon/src/java/org/apache/antlib/vfile/StringToFileObjectConverter.java View File

@@ -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 <a href="mailto:adammurdoch@apache.org">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 );
}
}
}

+ 35
- 0
proposal/myrmidon/src/test/org/apache/antlib/vfile/CopyFilesTaskTest.java View File

@@ -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 <v-copy> task.
*
* @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
* @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" );
}
}

+ 35
- 0
proposal/myrmidon/src/testcases/org/apache/antlib/vfile/CopyFilesTaskTest.java View File

@@ -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 <v-copy> task.
*
* @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
* @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" );
}
}

Loading…
Cancel
Save