Browse Source

resource collection support for unzip/jar/war/tar

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@292261 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 20 years ago
parent
commit
f027ced8e7
2 changed files with 92 additions and 38 deletions
  1. +42
    -17
      src/main/org/apache/tools/ant/taskdefs/Expand.java
  2. +50
    -21
      src/main/org/apache/tools/ant/taskdefs/Untar.java

+ 42
- 17
src/main/org/apache/tools/ant/taskdefs/Expand.java View File

@@ -24,6 +24,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

import org.apache.tools.ant.BuildException;
@@ -33,6 +34,10 @@ import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Mapper;
import org.apache.tools.ant.types.PatternSet;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.resources.Union;
import org.apache.tools.ant.types.selectors.SelectorUtils;
import org.apache.tools.ant.util.FileNameMapper;
import org.apache.tools.ant.util.FileUtils;
@@ -56,7 +61,7 @@ public class Expand extends Task {
private boolean overwrite = true;
private Mapper mapperElement = null;
private Vector patternsets = new Vector();
private Vector filesets = new Vector();
private Union resources = new Union();

private static final String NATIVE_ENCODING = "native-encoding";

@@ -76,8 +81,8 @@ public class Expand extends Task {
log("!! expand is deprecated. Use unzip instead. !!");
}

if (source == null && filesets.size() == 0) {
throw new BuildException("src attribute and/or filesets must be "
if (source == null && resources.size() == 0) {
throw new BuildException("src attribute and/or resources must be "
+ "specified");
}

@@ -98,19 +103,19 @@ public class Expand extends Task {
expandFile(FILE_UTILS, source, dest);
}
}
if (filesets.size() > 0) {
for (int j = 0, size = filesets.size(); j < size; j++) {
FileSet fs = (FileSet) filesets.elementAt(j);
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
File fromDir = fs.getDir(getProject());
String[] files = ds.getIncludedFiles();
for (int i = 0; i < files.length; ++i) {
File file = new File(fromDir, files[i]);
expandFile(FILE_UTILS, file, dest);
}
}
}
Iterator iter = resources.iterator();
while (iter.hasNext()) {
Resource r = (Resource) iter.next();
if (!r.isExists()) {
continue;
}
if (r instanceof FileResource) {
expandFile(FILE_UTILS, ((FileResource) r).getFile(), dest);
} else {
expandResource(r, dest);
}
}
}

/**
@@ -143,6 +148,17 @@ public class Expand extends Task {
}
}

/**
* This method is to be overridden by extending unarchival tasks.
*
* @param r the source resource
* @param dir the destination directory
*/
protected void expandResource(Resource srcR, File dir) {
throw new BuildException("only filesystem based resources are"
+ " supported by this task.");
}

/**
* get a mapper for a file
* @return a filenamemapper for a file
@@ -322,7 +338,16 @@ public class Expand extends Task {
* @param set a file set
*/
public void addFileset(FileSet set) {
filesets.addElement(set);
add(set);
}

/**
* Add a resource collection.
* @param rc a resource collection.
* @since Ant 1.7
*/
public void add(ResourceCollection rc) {
resources.add(rc);
}

/**


+ 50
- 21
src/main/org/apache/tools/ant/taskdefs/Untar.java View File

@@ -27,6 +27,7 @@ import java.util.zip.GZIPInputStream;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.EnumeratedAttribute;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.util.FileNameMapper;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.bzip2.CBZip2InputStream;
@@ -91,33 +92,61 @@ public class Untar extends Expand {
*/
protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
FileInputStream fis = null;
TarInputStream tis = null;
try {
log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
fis = new FileInputStream(srcF);
tis = new TarInputStream(
compression.decompress(srcF, new BufferedInputStream(fis)));
TarEntry te = null;
FileNameMapper mapper = getMapper();
while ((te = tis.getNextEntry()) != null) {
extractFile(fileUtils, srcF, dir, tis,
te.getName(), te.getModTime(),
te.isDirectory(), mapper);
}
log("expand complete", Project.MSG_VERBOSE);

fis = new FileInputStream(srcF);
expandStream(srcF.getPath(), fis, dir);
} catch (IOException ioe) {
throw new BuildException("Error while expanding " + srcF.getPath(),
ioe, getLocation());
} finally {
FileUtils.close(tis);
if (tis == null) {
FileUtils.close(fis);
}
FileUtils.close(fis);
}
}

/**
* This method is to be overridden by extending unarchival tasks.
*
* @param r the source resource
* @param dir the destination directory
* @since Ant 1.7
*/
protected void expandResource(Resource srcR, File dir) {
InputStream i = null;
try {
i = srcR.getInputStream();
expandStream(srcR.getName(), i, dir);
} catch (IOException ioe) {
throw new BuildException("Error while expanding " + srcR.getName(),
ioe, getLocation());
} finally {
FileUtils.close(i);
}
}

/**
* @since Ant 1.7
*/
private void expandStream(String name, InputStream stream, File dir)
throws IOException {
TarInputStream tis = null;
try {
tis =
new TarInputStream(compression.decompress(name,
new BufferedInputStream(stream)));
log("Expanding: " + name + " into " + dir, Project.MSG_INFO);
TarEntry te = null;
FileNameMapper mapper = getMapper();
while ((te = tis.getNextEntry()) != null) {
extractFile(FileUtils.getFileUtils(), null, dir, tis,
te.getName(), te.getModTime(),
te.isDirectory(), mapper);
}
log("expand complete", Project.MSG_VERBOSE);
} finally {
FileUtils.close(tis);
}
}

/**
* Valid Modes for Compression attribute to Untar Task
*
@@ -168,7 +197,7 @@ public class Untar extends Expand {
* @exception BuildException thrown if bzip stream does not
* start with expected magic values
*/
private InputStream decompress(final File file,
private InputStream decompress(final String name,
final InputStream istream)
throws IOException, BuildException {
final String v = getValue();
@@ -180,7 +209,7 @@ public class Untar extends Expand {
for (int i = 0; i < magic.length; i++) {
if (istream.read() != magic[i]) {
throw new BuildException(
"Invalid bz2 file." + file.toString());
"Invalid bz2 file." + name);
}
}
return new CBZip2InputStream(istream);


Loading…
Cancel
Save