ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r832990 - in /ant/core/trunk/src: main/org/apache/tools/ant/helper/ProjectHelper2.java main/org/apache/tools/ant/taskdefs/ImportTask.java tests/antunit/taskdefs/import-test.xml tests/antunit/taskdefs/include-test.xml
Date Thu, 05 Nov 2009 04:38:17 GMT
Author: bodewig
Date: Thu Nov  5 04:38:16 2009
New Revision: 832990

URL: http://svn.apache.org/viewvc?rev=832990&view=rev
Log:
allow import of FileProvider or URLProvider resources

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java
    ant/core/trunk/src/tests/antunit/taskdefs/import-test.xml
    ant/core/trunk/src/tests/antunit/taskdefs/include-test.xml

Modified: ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java?rev=832990&r1=832989&r2=832990&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/helper/ProjectHelper2.java Thu Nov  5 04:38:16
2009
@@ -28,6 +28,7 @@
 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 @@
 
         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");

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java?rev=832990&r1=832989&r2=832990&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/ImportTask.java Thu Nov  5 04:38:16
2009
@@ -22,6 +22,8 @@
 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 @@
     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 @@
         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 @@
             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 @@
             }
         }
 
-        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 @@
             setProjectHelperProps(prefix, prefixSeparator,
                                   isInIncludeMode());
 
-            helper.parse(getProject(), importedFile);
+            helper.parse(getProject(), importedResource);
         } catch (BuildException ex) {
             throw ProjectHelper.addLocationToBuildException(
                 ex, getLocation());

Modified: ant/core/trunk/src/tests/antunit/taskdefs/import-test.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/import-test.xml?rev=832990&r1=832989&r2=832990&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/import-test.xml (original)
+++ ant/core/trunk/src/tests/antunit/taskdefs/import-test.xml Thu Nov  5 04:38:16 2009
@@ -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"/>

Modified: ant/core/trunk/src/tests/antunit/taskdefs/include-test.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/include-test.xml?rev=832990&r1=832989&r2=832990&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/include-test.xml (original)
+++ ant/core/trunk/src/tests/antunit/taskdefs/include-test.xml Thu Nov  5 04:38:16 2009
@@ -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 -->



Mime
View raw message