of the name in the classloader, not just the first PR: 24024 Obtained from: Jesse Glick git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@275567 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -1339,6 +1339,13 @@ | |||||
| <selector refid="conditional-patterns"/> | <selector refid="conditional-patterns"/> | ||||
| </javac> | </javac> | ||||
| <!-- Used by AntlibTest.testAntlibResource: --> | |||||
| <jar jarfile="${build.tests}/org/apache/tools/ant/taskdefs/test2-antlib.jar"> | |||||
| <zipfileset dir="${tests.etc.dir}" fullpath="taskdefs/test.antlib.xml"> | |||||
| <include name="taskdefs/test2.antlib.xml"/> | |||||
| </zipfileset> | |||||
| </jar> | |||||
| </target> | </target> | ||||
| <target name="dump-info" depends="dump-sys-properties,run-which"/> | <target name="dump-info" depends="dump-sys-properties,run-which"/> | ||||
| @@ -14,6 +14,22 @@ | |||||
| <mytask/> | <mytask/> | ||||
| </target> | </target> | ||||
| <target name="antlib.resource"> | |||||
| <typedef resource="taskdefs/test.antlib.xml"> | |||||
| <classpath> | |||||
| <!-- To load the task classes: --> | |||||
| <path refid="testclasses"/> | |||||
| <!-- For test.antlib.xml: --> | |||||
| <pathelement location=".."/> | |||||
| <!-- For test2.antlib.xml: --> | |||||
| <pathelement location="${testcases.dir}/org/apache/tools/ant/taskdefs/test2-antlib.jar"/> | |||||
| </classpath> | |||||
| </typedef> | |||||
| <mytask/> | |||||
| <echo>-and-then-</echo> | |||||
| <mytask2/> | |||||
| </target> | |||||
| <target name="ns.current"> | <target name="ns.current"> | ||||
| <typedef file="antlib.current-test.xml" uri="abc"/> | <typedef file="antlib.current-test.xml" uri="abc"/> | ||||
| <x:useecho2 xmlns:x="abc"/> | <x:useecho2 xmlns:x="abc"/> | ||||
| @@ -0,0 +1,6 @@ | |||||
| <?xml version="1.0"?> | |||||
| <antlib> | |||||
| <typedef | |||||
| name="mytask2" onerror="ignore" | |||||
| classname="org.apache.tools.ant.taskdefs.AntlibTest$MyTask2"/> | |||||
| </antlib> | |||||
| @@ -60,6 +60,7 @@ import java.io.InputStream; | |||||
| import java.net.URL; | import java.net.URL; | ||||
| import java.util.Enumeration; | import java.util.Enumeration; | ||||
| import java.util.Locale; | import java.util.Locale; | ||||
| import java.util.NoSuchElementException; | |||||
| import java.util.Properties; | import java.util.Properties; | ||||
| import org.apache.tools.ant.AntTypeDefinition; | import org.apache.tools.ant.AntTypeDefinition; | ||||
| @@ -217,26 +218,40 @@ public abstract class Definer extends DefBase { | |||||
| + "together with file or resource."; | + "together with file or resource."; | ||||
| throw new BuildException(msg, getLocation()); | throw new BuildException(msg, getLocation()); | ||||
| } | } | ||||
| URL url = null; | |||||
| Enumeration/*<URL>*/ urls = null; | |||||
| if (file != null) { | if (file != null) { | ||||
| url = fileToURL(); | |||||
| } | |||||
| if (resource != null) { | |||||
| url = resourceToURL(al); | |||||
| final URL url = fileToURL(); | |||||
| urls = new Enumeration() { | |||||
| private boolean more = true; | |||||
| public boolean hasMoreElements() { | |||||
| return more; | |||||
| } | |||||
| public Object nextElement() throws NoSuchElementException { | |||||
| if (more) { | |||||
| more = false; | |||||
| return url; | |||||
| } else { | |||||
| throw new NoSuchElementException(); | |||||
| } | |||||
| } | |||||
| }; | |||||
| } else { | |||||
| urls = resourceToURLs(al); | |||||
| } | } | ||||
| if (url == null) { | |||||
| return; | |||||
| } | |||||
| while (urls.hasMoreElements()) { | |||||
| URL url = (URL) urls.nextElement(); | |||||
| if (url.toString().toLowerCase(Locale.US).endsWith(".xml")) { | |||||
| format = Format.XML; | |||||
| } | |||||
| int format = this.format; | |||||
| if (url.toString().toLowerCase(Locale.US).endsWith(".xml")) { | |||||
| format = Format.XML; | |||||
| } | |||||
| if (format == Format.PROPERTIES) { | |||||
| loadProperties(al, url); | |||||
| } else { | |||||
| loadAntlib(al, url); | |||||
| if (format == Format.PROPERTIES) { | |||||
| loadProperties(al, url); | |||||
| } else { | |||||
| loadAntlib(al, url); | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -259,9 +274,16 @@ public abstract class Definer extends DefBase { | |||||
| } | } | ||||
| } | } | ||||
| private URL resourceToURL(ClassLoader classLoader) { | |||||
| URL ret = classLoader.getResource(resource); | |||||
| if (ret == null) { | |||||
| private Enumeration/*<URL>*/ resourceToURLs(ClassLoader classLoader) { | |||||
| Enumeration ret; | |||||
| try { | |||||
| ret = classLoader.getResources(resource); | |||||
| } catch (IOException e) { | |||||
| throw new BuildException( | |||||
| "Could not fetch resources named " + resource, | |||||
| e, getLocation()); | |||||
| } | |||||
| if (!ret.hasMoreElements()) { | |||||
| if (onError != OnError.IGNORE) { | if (onError != OnError.IGNORE) { | ||||
| log("Could not load definitions from resource " | log("Could not load definitions from resource " | ||||
| + resource + ". It could not be found.", | + resource + ". It could not be found.", | ||||
| @@ -73,6 +73,15 @@ public class AntlibTest extends BuildFileTest { | |||||
| public void testAntlibFile() { | public void testAntlibFile() { | ||||
| expectLog("antlib.file", "MyTask called"); | expectLog("antlib.file", "MyTask called"); | ||||
| } | } | ||||
| /** | |||||
| * Confirms that all matching resources will be used, so that you | |||||
| * can collect several antlibs in one Definer call. | |||||
| * @see "http://issues.apache.org/bugzilla/show_bug.cgi?id=24024" | |||||
| */ | |||||
| public void testAntlibResource() { | |||||
| expectLog("antlib.resource", "MyTask called-and-then-MyTask2 called"); | |||||
| } | |||||
| public void testNsCurrent() { | public void testNsCurrent() { | ||||
| expectLog("ns.current", "Echo2 inside a macroHello from x:p"); | expectLog("ns.current", "Echo2 inside a macroHello from x:p"); | ||||
| @@ -84,5 +93,11 @@ public class AntlibTest extends BuildFileTest { | |||||
| } | } | ||||
| } | } | ||||
| public static class MyTask2 extends Task { | |||||
| public void execute() { | |||||
| log("MyTask2 called"); | |||||
| } | |||||
| } | |||||
| } | } | ||||