Search in sources :

Example 1 with UnknownElement

use of org.apache.tools.ant.UnknownElement in project ant by apache.

the class MacroInstance method execute.

/**
 * Execute the templates instance.
 * Copies the unknown element, substitutes the attributes,
 * and calls perform on the unknown element.
 */
@Override
public void execute() {
    presentElements = new HashMap<>();
    getNsElements();
    processTasks();
    localAttributes = new Hashtable<>();
    Set<String> copyKeys = new HashSet<>(map.keySet());
    for (Attribute attribute : macroDef.getAttributes()) {
        String value = map.get(attribute.getName());
        if (value == null && "description".equals(attribute.getName())) {
            value = getDescription();
        }
        if (value == null) {
            value = attribute.getDefault();
            value = macroSubs(value, localAttributes);
        }
        if (value == null) {
            throw new BuildException("required attribute %s not set", attribute.getName());
        }
        localAttributes.put(attribute.getName(), value);
        copyKeys.remove(attribute.getName());
    }
    copyKeys.remove("id");
    if (macroDef.getText() != null) {
        if (text == null) {
            String defaultText = macroDef.getText().getDefault();
            if (!macroDef.getText().getOptional() && defaultText == null) {
                throw new BuildException("required text missing");
            }
            text = defaultText == null ? "" : defaultText;
        }
        if (macroDef.getText().getTrim()) {
            text = text.trim();
        }
        localAttributes.put(macroDef.getText().getName(), text);
    } else if (text != null && !text.trim().isEmpty()) {
        throw new BuildException("The \"%s\" macro does not support nested text data.", getTaskName());
    }
    if (!copyKeys.isEmpty()) {
        throw new BuildException("Unknown attribute" + (copyKeys.size() > 1 ? "s " : " ") + copyKeys);
    }
    // need to set the project on unknown element
    UnknownElement c = copy(macroDef.getNestedTask(), false);
    c.init();
    LocalProperties localProperties = LocalProperties.get(getProject());
    localProperties.enterScope();
    try {
        c.perform();
    } catch (BuildException ex) {
        if (macroDef.getBackTrace()) {
            throw ProjectHelper.addLocationToBuildException(ex, getLocation());
        } else {
            ex.setLocation(getLocation());
            throw ex;
        }
    } finally {
        presentElements = null;
        localAttributes = null;
        localProperties.exitScope();
    }
}
Also used : Attribute(org.apache.tools.ant.taskdefs.MacroDef.Attribute) DynamicAttribute(org.apache.tools.ant.DynamicAttribute) UnknownElement(org.apache.tools.ant.UnknownElement) BuildException(org.apache.tools.ant.BuildException) LocalProperties(org.apache.tools.ant.property.LocalProperties) HashSet(java.util.HashSet)

Example 2 with UnknownElement

use of org.apache.tools.ant.UnknownElement in project ant by apache.

the class ProjectHelper2 method parseUnknownElement.

/**
 * Parse an unknown element from a url
 *
 * @param project the current project
 * @param source  the url containing the task
 * @return a configured task
 * @exception BuildException if an error occurs
 */
public UnknownElement parseUnknownElement(Project project, URL source) throws BuildException {
    Target dummyTarget = new Target();
    dummyTarget.setProject(project);
    AntXMLContext context = new AntXMLContext(project);
    context.addTarget(dummyTarget);
    context.setImplicitTarget(dummyTarget);
    parse(context.getProject(), source, new RootHandler(context, elementHandler));
    Task[] tasks = dummyTarget.getTasks();
    if (tasks.length != 1) {
        throw new BuildException("No tasks defined");
    }
    return (UnknownElement) tasks[0];
}
Also used : Target(org.apache.tools.ant.Target) Task(org.apache.tools.ant.Task) UnknownElement(org.apache.tools.ant.UnknownElement) BuildException(org.apache.tools.ant.BuildException)

Example 3 with UnknownElement

use of org.apache.tools.ant.UnknownElement in project ant by apache.

the class DispatchUtils method execute.

/**
 * Determines and Executes the action method for the task.
 * @param task the task to execute.
 * @throws BuildException on error.
 */
public static final void execute(Object task) throws BuildException {
    String methodName = "execute";
    Dispatchable dispatchable = null;
    try {
        if (task instanceof Dispatchable) {
            dispatchable = (Dispatchable) task;
        } else if (task instanceof UnknownElement) {
            UnknownElement ue = (UnknownElement) task;
            Object realThing = ue.getRealThing();
            if (realThing instanceof Dispatchable && realThing instanceof Task) {
                dispatchable = (Dispatchable) realThing;
            }
        }
        if (dispatchable != null) {
            String mName = null;
            try {
                final String name = dispatchable.getActionParameterName();
                if (name != null && name.trim().length() > 0) {
                    mName = "get" + name.trim().substring(0, 1).toUpperCase();
                    if (name.length() > 1) {
                        mName += name.substring(1);
                    }
                    final Class<? extends Dispatchable> c = dispatchable.getClass();
                    final Method actionM = c.getMethod(mName);
                    if (actionM != null) {
                        final Object o = actionM.invoke(dispatchable, (Object[]) null);
                        if (o != null) {
                            final String s = o.toString();
                            if (s.trim().length() > 0) {
                                methodName = s.trim();
                                Method executeM = null;
                                executeM = dispatchable.getClass().getMethod(methodName);
                                if (executeM == null) {
                                    throw new BuildException("No public " + methodName + "() in " + dispatchable.getClass());
                                }
                                executeM.invoke(dispatchable, (Object[]) null);
                                if (task instanceof UnknownElement) {
                                    ((UnknownElement) task).setRealThing(null);
                                }
                            } else {
                                throw new BuildException("Dispatchable Task attribute '" + name.trim() + "' not set or value is empty.");
                            }
                        } else {
                            throw new BuildException("Dispatchable Task attribute '" + name.trim() + "' not set or value is empty.");
                        }
                    }
                } else {
                    throw new BuildException("Action Parameter Name must not be empty for Dispatchable Task.");
                }
            } catch (NoSuchMethodException nsme) {
                throw new BuildException("No public " + mName + "() in " + task.getClass());
            }
        } else {
            Method executeM = null;
            executeM = task.getClass().getMethod(methodName);
            if (executeM == null) {
                throw new BuildException("No public " + methodName + "() in " + task.getClass());
            }
            executeM.invoke(task);
            if (task instanceof UnknownElement) {
                ((UnknownElement) task).setRealThing(null);
            }
        }
    } catch (InvocationTargetException ie) {
        Throwable t = ie.getTargetException();
        if (t instanceof BuildException) {
            throw ((BuildException) t);
        } else {
            throw new BuildException(t);
        }
    } catch (NoSuchMethodException e) {
        throw new BuildException(e);
    } catch (IllegalAccessException e) {
        throw new BuildException(e);
    }
}
Also used : Task(org.apache.tools.ant.Task) UnknownElement(org.apache.tools.ant.UnknownElement) Method(java.lang.reflect.Method) BuildException(org.apache.tools.ant.BuildException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with UnknownElement

use of org.apache.tools.ant.UnknownElement 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)

Example 5 with UnknownElement

use of org.apache.tools.ant.UnknownElement in project ant by apache.

the class Antlib method execute.

/**
 * Execute the nested tasks, setting the classloader for
 * any tasks that derive from Definer.
 */
@Override
public void execute() {
    for (Task task : tasks) {
        UnknownElement ue = (UnknownElement) task;
        setLocation(ue.getLocation());
        ue.maybeConfigure();
        Object configuredObject = ue.getRealThing();
        if (configuredObject == null) {
            continue;
        }
        if (!(configuredObject instanceof AntlibDefinition)) {
            throw new BuildException("Invalid task in antlib %s %s does not extend %s", ue.getTag(), configuredObject.getClass(), AntlibDefinition.class.getName());
        }
        AntlibDefinition def = (AntlibDefinition) configuredObject;
        def.setURI(uri);
        def.setAntlibClassLoader(getClassLoader());
        def.init();
        def.execute();
    }
}
Also used : Task(org.apache.tools.ant.Task) UnknownElement(org.apache.tools.ant.UnknownElement) BuildException(org.apache.tools.ant.BuildException)

Aggregations

UnknownElement (org.apache.tools.ant.UnknownElement)13 BuildException (org.apache.tools.ant.BuildException)8 Task (org.apache.tools.ant.Task)8 Method (java.lang.reflect.Method)2 Log (org.apache.commons.logging.Log)2 RuntimeConfigurable (org.apache.tools.ant.RuntimeConfigurable)2 Target (org.apache.tools.ant.Target)2 File (java.io.File)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URLConnection (java.net.URLConnection)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 ComponentHelper (org.apache.tools.ant.ComponentHelper)1 DynamicAttribute (org.apache.tools.ant.DynamicAttribute)1 Project (org.apache.tools.ant.Project)1 ProjectHelper (org.apache.tools.ant.ProjectHelper)1