use of org.apache.tools.ant.ProjectHelperRepository in project ant by apache.
the class Antlib method createAntlib.
/**
* Static method to read an ant lib definition from
* a url.
*
* @param project the current project
* @param antlibUrl the url to read the definitions from
* @param uri the uri that the antlib is to be placed in
* @return the ant lib task
*/
public static Antlib createAntlib(Project project, URL antlibUrl, String uri) {
// Check if we can contact the URL
try {
URLConnection conn = antlibUrl.openConnection();
conn.setUseCaches(false);
conn.connect();
} catch (IOException ex) {
throw new BuildException("Unable to find " + antlibUrl, ex);
}
ComponentHelper helper = ComponentHelper.getComponentHelper(project);
helper.enterAntLib(uri);
URLResource antlibResource = new URLResource(antlibUrl);
try {
// Should be safe to parse
ProjectHelper parser = null;
Object p = project.getReference(ProjectHelper.PROJECTHELPER_REFERENCE);
if (p instanceof ProjectHelper) {
parser = (ProjectHelper) p;
if (!parser.canParseAntlibDescriptor(antlibResource)) {
parser = null;
}
}
if (parser == null) {
ProjectHelperRepository helperRepository = ProjectHelperRepository.getInstance();
parser = helperRepository.getProjectHelperForAntlib(antlibResource);
}
UnknownElement ue = parser.parseAntlibDescriptor(project, antlibResource);
// Check name is "antlib"
if (!TAG.equals(ue.getTag())) {
throw new BuildException("Unexpected tag " + ue.getTag() + " expecting " + TAG, ue.getLocation());
}
Antlib antlib = new Antlib();
antlib.setProject(project);
antlib.setLocation(ue.getLocation());
antlib.setTaskName("antlib");
antlib.init();
ue.configure(antlib);
return antlib;
} finally {
helper.exitAntLib();
}
}
Aggregations