Package

Source Code of TaskCall$Elements

/*
* Copyright 2006 TaskCall
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*
*/

import java.util.Enumeration;
import java.util.Vector;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ComponentHelper;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.RuntimeConfigurable;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.TaskContainer;
import org.apache.tools.ant.UnknownElement;
import org.apache.tools.ant.taskdefs.AntlibDefinition;

/**
* This task provides a means to call another task by its name at runtime.
*
* @author Mathieu Champlon
* @version $Revision$ $Date$
*/
public class TaskCall extends AntlibDefinition {
        /**
         * The name of the task to call.
         */
        private String name;

        /**
         * Whether to fail the build if the task does not exist or not
         */
        private boolean failOnError = true;

        /**
         * The (optional) value of the text element to call the task with.
         */
        private String text;

        /**
         * The list of attributes to cal the task with.
         */
        private final Vector attributes = new Vector();

        /**
         * The list of elements to call the task with.
         */
        private Elements elements;

        /**
         * Set the text element.
         *
         * @param text
         *                the value of the text element
         */
        public final void setText(final String text) {
                this.text = text;
        }

        /**
         * Set the name of the task to call.
         *
         * @param name
         *                the name of the task to call
         */
        public final void setName(final String name) {
                this.name = name;
        }

        /**
         * Change the behaviour when the task does not exist
         *
         * @param fail
         *                if true the build will fail if the task does not exist
         */
        public final void setFailOnError(final boolean fail) {
                this.failOnError = fail;
        }

        /**
         * Register an attribute for the task to call.
         *
         * @param attribute
         *                an attribute
         */
        public final void addConfiguredAttribute(final Attribute attribute) {
                if (attribute.getName() == null || attribute.getValue() == null)
                        throw new BuildException(
                                        "Missing required 'name' or 'value' attribute for 'attribute' element");
                attributes.add(attribute);
        }

        /**
         * Register the elements for the task to call.
         *
         * @param elements
         *                the elements to call the task with
         */
        public final void addConfiguredElements(final Elements elements) {
                if (this.elements != null)
                        throw new BuildException(
                                        "Only one 'elements' element allowed");
                this.elements = elements;
        }

        private UnknownElement createTask() {
                final UnknownElement result = new UnknownElement(name);
                result.setProject(getProject());
                result.setTaskName(name);
                result.setNamespace("");
                result.setQName(name);
                result.setTaskType(name);
                result.setLocation(getLocation());
                result.setOwningTarget(getOwningTarget());
                final RuntimeConfigurable wrapper = new RuntimeConfigurable(
                                result, name);
                addText(wrapper);
                addAttributes(wrapper);
                addElements(result, wrapper);
                return result;
        }

        private void addText(final RuntimeConfigurable wrapper) {
                if (text != null) {
                        log("text : '" + text + "'", Project.MSG_DEBUG);
                        wrapper.addText(text);
                }
        }

        private void addAttributes(final RuntimeConfigurable wrapper) {
                final Enumeration e = attributes.elements();
                while (e.hasMoreElements()) {
                        final Attribute attribute = (Attribute) e.nextElement();
                        log("attribute : '" + attribute.getName() + "' / '"
                                        + attribute.getValue() + "'",
                                        Project.MSG_DEBUG);
                        wrapper.setAttribute(attribute.getName(), attribute
                                        .getValue());
                }
        }

        private void addElements(final UnknownElement result,
                        final RuntimeConfigurable wrapper) {
                if (elements != null) {
                        final Enumeration e = elements.getTasks();
                        while (e.hasMoreElements()) {
                                final UnknownElement ue = (UnknownElement) e
                                                .nextElement();
                                log("element : '" + ue.getTag() + "'",
                                                Project.MSG_DEBUG);
                                result.addChild(ue);
                                wrapper.addChild(ue.getWrapper());
                        }
                }
        }

        /**
         * {@inheritDoc}
         */
        public final void execute() {
                if (name == null) {
                        throw new BuildException("Name not specified");
                }
                name = ProjectHelper.genComponentName(getURI(), name);
                final UnknownElement task = createTask();
                if (!failOnError) {
                        final ComponentHelper helper = ComponentHelper
                                        .getComponentHelper(getProject());
                        if (helper.getDefinition(name) == null)
                                return;
                }
                log("calling '" + name + "'", Project.MSG_DEBUG);
                task.init();
                try {
                        task.perform();
                } catch (final BuildException ex) {
                        throw ProjectHelper.addLocationToBuildException(ex,
                                        getLocation());
                }
        }

        /**
         * Stands for an attribute of the task to call.
         */
        public static class Attribute {
                private String name;

                private String value;

                public final void setName(final String name) {
                        this.name = name;
                }

                public final void setValue(final String value) {
                        this.value = value;
                }

                public final String getName() {
                        return name;
                }

                public final String getValue() {
                        return value;
                }
        }

        /**
         * Stands for the elements of the task to call.
         */
        public static class Elements implements TaskContainer {
                private final Vector tasks = new Vector();

                public final void addTask(final Task task) {
                        tasks.add(task);
                }

                public final Enumeration getTasks() {
                        return tasks.elements();
                }
        }
}
TOP

Related Classes of TaskCall$Elements

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.