Search in sources :

Example 1 with LocalProperties

use of org.apache.tools.ant.property.LocalProperties 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 LocalProperties

use of org.apache.tools.ant.property.LocalProperties in project ant by apache.

the class Target method execute.

/**
 * Executes the target if the "if" and "unless" conditions are
 * satisfied. Dependency checking should be done before calling this
 * method, as it does no checking of its own. If either the "if"
 * or "unless" test prevents this target from being executed, a verbose
 * message is logged giving the reason. It is recommended that clients
 * of this class call performTasks rather than this method so that
 * appropriate build events are fired.
 *
 * @exception BuildException if any of the tasks fail or if a data type
 *                           configuration fails.
 *
 * @see #performTasks()
 * @see #setIf(String)
 * @see #setUnless(String)
 */
public void execute() throws BuildException {
    if (ifCondition != null && !ifCondition.eval()) {
        project.log(this, "Skipped because property '" + project.replaceProperties(ifString) + "' not set.", Project.MSG_VERBOSE);
        return;
    }
    if (unlessCondition != null && unlessCondition.eval()) {
        project.log(this, "Skipped because property '" + project.replaceProperties(unlessString) + "' set.", Project.MSG_VERBOSE);
        return;
    }
    LocalProperties localProperties = LocalProperties.get(getProject());
    localProperties.enterScope();
    try {
        // as children can be added dynamically as in RhinoScriptTest where a target is adding work for itself
        for (int i = 0; i < children.size(); i++) {
            Object o = children.get(i);
            if (o instanceof Task) {
                Task task = (Task) o;
                task.perform();
            } else {
                ((RuntimeConfigurable) o).maybeConfigure(project);
            }
        }
    } finally {
        localProperties.exitScope();
    }
}
Also used : LocalProperties(org.apache.tools.ant.property.LocalProperties)

Example 3 with LocalProperties

use of org.apache.tools.ant.property.LocalProperties in project ant by apache.

the class Sequential method execute.

/**
 * Execute all nestedTasks.
 *
 * @throws BuildException if one of the nested tasks fails.
 */
@Override
public void execute() throws BuildException {
    LocalProperties localProperties = LocalProperties.get(getProject());
    localProperties.enterScope();
    try {
        nestedTasks.forEach(Task::perform);
    } finally {
        localProperties.exitScope();
    }
}
Also used : Task(org.apache.tools.ant.Task) LocalProperties(org.apache.tools.ant.property.LocalProperties)

Aggregations

LocalProperties (org.apache.tools.ant.property.LocalProperties)3 HashSet (java.util.HashSet)1 BuildException (org.apache.tools.ant.BuildException)1 DynamicAttribute (org.apache.tools.ant.DynamicAttribute)1 Task (org.apache.tools.ant.Task)1 UnknownElement (org.apache.tools.ant.UnknownElement)1 Attribute (org.apache.tools.ant.taskdefs.MacroDef.Attribute)1