Search in sources :

Example 1 with ProjectHelperRepository

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();
    }
}
Also used : URLResource(org.apache.tools.ant.types.resources.URLResource) ProjectHelper(org.apache.tools.ant.ProjectHelper) UnknownElement(org.apache.tools.ant.UnknownElement) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) URLConnection(java.net.URLConnection) ProjectHelperRepository(org.apache.tools.ant.ProjectHelperRepository) ComponentHelper(org.apache.tools.ant.ComponentHelper)

Aggregations

IOException (java.io.IOException)1 URLConnection (java.net.URLConnection)1 BuildException (org.apache.tools.ant.BuildException)1 ComponentHelper (org.apache.tools.ant.ComponentHelper)1 ProjectHelper (org.apache.tools.ant.ProjectHelper)1 ProjectHelperRepository (org.apache.tools.ant.ProjectHelperRepository)1 UnknownElement (org.apache.tools.ant.UnknownElement)1 URLResource (org.apache.tools.ant.types.resources.URLResource)1