Browse Source

Allow typedef resource to pick up all the resources

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-ffa450edef68
master
Peter Reilly 21 years ago
parent
commit
86a183cc1f
5 changed files with 84 additions and 18 deletions
  1. +7
    -0
      build.xml
  2. +16
    -0
      src/etc/testcases/taskdefs/antlib.xml
  3. +6
    -0
      src/etc/testcases/taskdefs/test2.antlib.xml
  4. +40
    -18
      src/main/org/apache/tools/ant/taskdefs/Definer.java
  5. +15
    -0
      src/testcases/org/apache/tools/ant/taskdefs/AntlibTest.java

+ 7
- 0
build.xml View File

@@ -1339,6 +1339,13 @@

<selector refid="conditional-patterns"/>
</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 name="dump-info" depends="dump-sys-properties,run-which"/>


+ 16
- 0
src/etc/testcases/taskdefs/antlib.xml View File

@@ -14,6 +14,22 @@
<mytask/>
</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">
<typedef file="antlib.current-test.xml" uri="abc"/>
<x:useecho2 xmlns:x="abc"/>


+ 6
- 0
src/etc/testcases/taskdefs/test2.antlib.xml View File

@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<antlib>
<typedef
name="mytask2" onerror="ignore"
classname="org.apache.tools.ant.taskdefs.AntlibTest$MyTask2"/>
</antlib>

+ 40
- 18
src/main/org/apache/tools/ant/taskdefs/Definer.java View File

@@ -60,6 +60,7 @@ import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Properties;

import org.apache.tools.ant.AntTypeDefinition;
@@ -217,26 +218,40 @@ public abstract class Definer extends DefBase {
+ "together with file or resource.";
throw new BuildException(msg, getLocation());
}
URL url = null;
Enumeration/*<URL>*/ urls = 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) {
log("Could not load definitions from resource "
+ resource + ". It could not be found.",


+ 15
- 0
src/testcases/org/apache/tools/ant/taskdefs/AntlibTest.java View File

@@ -73,6 +73,15 @@ public class AntlibTest extends BuildFileTest {
public void testAntlibFile() {
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() {
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");
}
}

}


Loading…
Cancel
Save