Browse Source

allow import of FileProvider or URLProvider resources

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@832990 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 15 years ago
parent
commit
2ef9a200ad
4 changed files with 68 additions and 19 deletions
  1. +18
    -4
      src/main/org/apache/tools/ant/helper/ProjectHelper2.java
  2. +34
    -11
      src/main/org/apache/tools/ant/taskdefs/ImportTask.java
  3. +8
    -2
      src/tests/antunit/taskdefs/import-test.xml
  4. +8
    -2
      src/tests/antunit/taskdefs/include-test.xml

+ 18
- 4
src/main/org/apache/tools/ant/helper/ProjectHelper2.java View File

@@ -28,6 +28,7 @@ import org.apache.tools.ant.TargetGroup;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.UnknownElement;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.resources.FileProvider;
import org.apache.tools.ant.types.resources.URLProvider;
import org.apache.tools.ant.util.FileUtils;
import org.apache.tools.ant.util.JAXPUtils;
@@ -197,14 +198,27 @@ public class ProjectHelper2 extends ProjectHelper {

if (source instanceof File) {
buildFile = (File) source;
} else if (source instanceof URL) {
url = (URL) source;
} else if (source instanceof Resource) {
FileProvider fp =
(FileProvider) ((Resource) source).as(FileProvider.class);
if (fp != null) {
buildFile = fp.getFile();
} else {
URLProvider up =
(URLProvider) ((Resource) source).as(URLProvider.class);
if (up != null) {
url = up.getURL();
}
}
}
if (buildFile != null) {
buildFile = FILE_UTILS.normalize(buildFile.getAbsolutePath());
context.setBuildFile(buildFile);
buildFileName = buildFile.toString();
// } else if (source instanceof InputStream ) {
} else if (source instanceof URL) {
url = (URL) source;
} else if (url != null) {
buildFileName = url.toString();
// } else if (source instanceof InputSource ) {
} else {
throw new BuildException("Source " + source.getClass().getName()
+ " not supported by this plugin");


+ 34
- 11
src/main/org/apache/tools/ant/taskdefs/ImportTask.java View File

@@ -22,6 +22,8 @@ import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.util.FileUtils;

import java.io.File;
@@ -57,6 +59,7 @@ public class ImportTask extends Task {
private boolean optional;
private String targetPrefix;
private String prefixSeparator = ".";
private Resource resource = null;
private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();

/**
@@ -99,9 +102,19 @@ public class ImportTask extends Task {
prefixSeparator = s;
}

/**
* The resource to import.
*
* @since Ant 1.8.0
*/
public void add(Resource r) {
resource = r;
}

public void execute() {
if (file == null) {
throw new BuildException("import requires file attribute");
if (file == null && resource == null) {
throw new BuildException("import requires file attribute or"
+ " nested resource");
}
if (getOwningTarget() == null
|| !"".equals(getOwningTarget().getName())) {
@@ -129,21 +142,27 @@ public class ImportTask extends Task {
throw new BuildException("Unable to get location of import task");
}

Resource importedResource = resource;
File importedFile = null;
if (resource == null) {

File buildFile = new File(getLocation().getFileName()).getAbsoluteFile();

// Paths are relative to the build file they're imported from,
// *not* the current directory (same as entity includes).

File buildFileParent = new File(buildFile.getParent());
File importedFile = FILE_UTILS.resolveFile(buildFileParent, file);
importedFile = FILE_UTILS.resolveFile(buildFileParent, file);
importedResource = new FileResource(importedFile);
}

getProject().log("Importing file " + importedFile + " from "
+ buildFile.getAbsolutePath(), Project.MSG_VERBOSE);
getProject().log("Importing file " + importedResource + " from "
+ getLocation().getFileName(), Project.MSG_VERBOSE);

if (!importedFile.exists()) {
if (!importedResource.isExists()) {
String message =
"Cannot find " + file + " imported from "
+ buildFile.getAbsolutePath();
"Cannot find " + importedResource + " imported from "
+ getLocation().getFileName();
if (optional) {
getProject().log(message, Project.MSG_VERBOSE);
return;
@@ -152,10 +171,14 @@ public class ImportTask extends Task {
}
}

if (!isInIncludeMode() && importStack.contains(importedFile)) {
if (!isInIncludeMode() &&
(importStack.contains(importedResource)
|| (importedFile != null && importStack.contains(importedFile))
)
) {
getProject().log(
"Skipped already imported file:\n "
+ importedFile + "\n", Project.MSG_VERBOSE);
+ importedResource + "\n", Project.MSG_VERBOSE);
return;
}

@@ -173,7 +196,7 @@ public class ImportTask extends Task {
setProjectHelperProps(prefix, prefixSeparator,
isInIncludeMode());

helper.parse(getProject(), importedFile);
helper.parse(getProject(), importedResource);
} catch (BuildException ex) {
throw ProjectHelper.addLocationToBuildException(
ex, getLocation());


+ 8
- 2
src/tests/antunit/taskdefs/import-test.xml View File

@@ -18,7 +18,9 @@
<project default="antunit" xmlns:au="antlib:org.apache.ant.antunit">
<import file="../antunit-base.xml" />

<import file="importtests/a.xml"/>
<import>
<file file="importtests/a.xml"/>
</import>
<import file="importtests/b.xml" as="c"/>

<target name="testNoExplicitPrefix" depends="a.a">
@@ -37,7 +39,11 @@
<au:assertEquals expected="baz" actual="${foo}"/>
</target>

<import file="importtests/override.xml"/>
<import>
<javaresource name="override.xml">
<classpath location="importtests"/>
</javaresource>
</import>

<target name="setProperty">
<property name="prop" value="in including/importing"/>


+ 8
- 2
src/tests/antunit/taskdefs/include-test.xml View File

@@ -18,7 +18,9 @@
<project default="antunit" xmlns:au="antlib:org.apache.ant.antunit">
<import file="../antunit-base.xml" />

<include file="importtests/a.xml"/>
<include>
<file file="importtests/a.xml"/>
</include>
<include file="importtests/b.xml" as="c"/>

<target name="testNoExplicitPrefix" depends="a.a">
@@ -39,7 +41,11 @@
<au:assertEquals expected="in included/imported" actual="${prop}"/>
</target>

<include file="importtests/nested.xml" as="nested"/>
<include as="nested">
<javaresource name="nested.xml">
<classpath location="importtests"/>
</javaresource>
</include>

<!-- really only tests that the targets have the expected names by
forcing an exception if the dependencies don't exist -->


Loading…
Cancel
Save