FileDocCategorySizeDatePackage
ScriptDef.javaAPI DocApache Ant 1.7012367Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs.optional.script

ScriptDef

public class ScriptDef extends org.apache.tools.ant.taskdefs.DefBase
Define a task using a script
since
Ant 1.6

Fields Summary
private org.apache.tools.ant.util.ScriptRunnerHelper
helper
script runner helper
private org.apache.tools.ant.util.ScriptRunnerBase
runner
Used to run the script
private String
name
the name by which this script will be activated
private List
attributes
Attributes definitions of this script
private List
nestedElements
Nested Element definitions of this script
private Set
attributeSet
The attribute names as a set
private Map
nestedElementMap
The nested element definitions indexed by their names
Constructors Summary
Methods Summary
public voidaddAttribute(org.apache.tools.ant.taskdefs.optional.script.ScriptDef$Attribute attribute)
Add an attribute definition to this script.

param
attribute the attribute definition.

        attributes.add(attribute);
    
public voidaddElement(org.apache.tools.ant.taskdefs.optional.script.ScriptDef$NestedElement nestedElement)
Add a nested element definition.

param
nestedElement the nested element definition.

        nestedElements.add(nestedElement);
    
public voidaddText(java.lang.String text)
Set the script text.

param
text a component of the script text to be added.

        helper.addText(text);
    
public java.lang.ObjectcreateNestedElement(java.lang.String elementName)
Create a nested element to be configured.

param
elementName the name of the nested element.
return
object representing the element name.

        NestedElement definition
            = (NestedElement) nestedElementMap.get(elementName);
        if (definition == null) {
            throw new BuildException("<" + name + "> does not support "
                + "the <" + elementName + "> nested element");
        }

        Object instance = null;
        String classname = definition.className;
        if (classname == null) {
            instance = getProject().createTask(definition.type);
            if (instance == null) {
                instance = getProject().createDataType(definition.type);
            }
        } else {
            /*
            // try the context classloader
            ClassLoader loader
                = Thread.currentThread().getContextClassLoader();
            */
            ClassLoader loader = createLoader();

            try {
                instance = ClasspathUtils.newInstance(classname, loader);
            } catch (BuildException e) {
                instance = ClasspathUtils.newInstance(classname, ScriptDef.class.getClassLoader());
            }

            getProject().setProjectReference(instance);
        }

        if (instance == null) {
            throw new BuildException("<" + name + "> is unable to create "
                + "the <" + elementName + "> nested element");
        }
        return instance;
    
public voidexecute()
Define the script.

        if (name == null) {
            throw new BuildException("scriptdef requires a name attribute to "
                + "name the script");
        }

        if (helper.getLanguage() == null) {
            throw new BuildException("<scriptdef> requires a language attribute "
                + "to specify the script language");
        }

        // Check if need to set the loader
        if (getAntlibClassLoader() != null || hasCpDelegate()) {
            helper.setClassLoader(createLoader());
        }

        // Now create the scriptRunner
        runner = helper.getScriptRunner();

        attributeSet = new HashSet();
        for (Iterator i = attributes.iterator(); i.hasNext();) {
            Attribute attribute = (Attribute) i.next();
            if (attribute.name == null) {
                throw new BuildException("scriptdef <attribute> elements "
                    + "must specify an attribute name");
            }

            if (attributeSet.contains(attribute.name)) {
                throw new BuildException("scriptdef <" + name + "> declares "
                    + "the " + attribute.name + " attribute more than once");
            }
            attributeSet.add(attribute.name);
        }

        nestedElementMap = new HashMap();
        for (Iterator i = nestedElements.iterator(); i.hasNext();) {
            NestedElement nestedElement = (NestedElement) i.next();
            if (nestedElement.name == null) {
                throw new BuildException("scriptdef <element> elements "
                    + "must specify an element name");
            }
            if (nestedElementMap.containsKey(nestedElement.name)) {
                throw new BuildException("scriptdef <" + name + "> declares "
                    + "the " + nestedElement.name + " nested element more "
                    + "than once");
            }

            if (nestedElement.className == null
                && nestedElement.type == null) {
                throw new BuildException("scriptdef <element> elements "
                    + "must specify either a classname or type attribute");
            }
            if (nestedElement.className != null
                && nestedElement.type != null) {
                throw new BuildException("scriptdef <element> elements "
                    + "must specify only one of the classname and type "
                    + "attributes");
            }


            nestedElementMap.put(nestedElement.name, nestedElement);
        }

        // find the script repository - it is stored in the project
        Map scriptRepository = null;
        Project p = getProject();
        synchronized (p) {
            scriptRepository =
                (Map) p.getReference(MagicNames.SCRIPT_REPOSITORY);
            if (scriptRepository == null) {
                scriptRepository = new HashMap();
                p.addReference(MagicNames.SCRIPT_REPOSITORY,
                    scriptRepository);
            }
        }

        name = ProjectHelper.genComponentName(getURI(), name);
        scriptRepository.put(name, this);
        AntTypeDefinition def = new AntTypeDefinition();
        def.setName(name);
        def.setClass(ScriptDefBase.class);
        ComponentHelper.getComponentHelper(
            getProject()).addDataTypeDefinition(def);
    
public voidexecuteScript(java.util.Map attributes, java.util.Map elements)
Execute the script.

param
attributes collection of attributes
param
elements a list of nested element values.
deprecated
since 1.7. Use executeScript(attribute, elements, instance) instead.

        executeScript(attributes, elements, null);
    
public voidexecuteScript(java.util.Map attributes, java.util.Map elements, ScriptDefBase instance)
Execute the script. This is called by the script instance to execute the script for this definition.

param
attributes collection of attributes
param
elements a list of nested element values.
param
instance the script instance; can be null

        runner.addBean("attributes", attributes);
        runner.addBean("elements", elements);
        runner.addBean("project", getProject());
        if (instance != null) {
            runner.addBean("self", instance);
        }
        runner.executeScript("scriptdef_" + name);
    
public booleanisAttributeSupported(java.lang.String attributeName)
Indicates whether the task supports a given attribute name

param
attributeName the name of the attribute.
return
true if the attribute is supported by the script.

        return attributeSet.contains(attributeName);
    
public voidsetLanguage(java.lang.String language)
Defines the language (required).

param
language the scripting language name for the script.

        helper.setLanguage(language);
    
public voidsetManager(java.lang.String manager)
Defines the manager.

param
manager the scripting manager.

        helper.setManager(manager);
    
public voidsetName(java.lang.String name)
set the name under which this script will be activated in a build file

param
name the name of the script

        this.name = name;
    
public voidsetProject(org.apache.tools.ant.Project project)
Set the project.

param
project the project that this def belows to.


                     
        
        super.setProject(project);
        helper.setProjectComponent(this);
        helper.setSetBeans(false);
    
public voidsetSrc(java.io.File file)
Load the script from an external file ; optional.

param
file the file containing the script source.

        helper.setSrc(file);