<?php
/*
 *  $Id: TaskHandler.php 4046 2005-04-22 13:41:56Z chregu $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information please see
 * <http://phing.info>.
 */

include_once 'phing/UnknownElement.php';

/**
 * The task handler class.
 *
 * This class handles the occurance of a <task> tag and it's possible
 * nested tags (datatypes and tasks) that may be unknown off bat and are
 * initialized on the fly.
 *
 * @author      Andreas Aderhold <andi@binarycloud.com>
 * @copyright © 2001,2002 THYRELL. All rights reserved
 * @version   $Revision: 1.9 $
 * @package   phing.parser
 */
class TaskHandler extends AbstractHandler {

    /**
     * Reference to the target object that contains the currently parsed
     * task
     * @var object the target instance
     */
    private $target;

    /**
     * Reference to the target object that represents the currently parsed
     * target. This must not necessarily be a target, hence extra variable.
     * @var object the target instance
     */
    private $container;

    /**
     * Reference to the task object that represents the currently parsed
     * target.
     * @var Task
     */
    private $task;

    private $wrapper;

    /**
     * The phing project configurator object
     * @var ProjectConfigurator
     */
    private $configurator;

    /**
     * Constructs a new TaskHandler and sets up everything.
     *
     * @param AbstractSAXParser The ExpatParser object
     * @param object $parentHandler The parent handler that invoked this handler
     * @param ProjectConfigurator $configurator
     * @param object $container The container object this task is contained in (null for top-level tasks).
     * @param object $target The target object this task is contained in (null for top-level tasks).
     */
    function __construct(AbstractSAXParser $parser, $parentHandler, ProjectConfigurator $configurator, $container = null, $target = null) {
        
        parent::__construct($parser, $parentHandler);
    
        if (($container !== null) && !($container instanceof TaskContainer)) {
            throw new Exception("Argument expected to be a TaskContainer, got something else");
        }
        if (($target !== null) && !($target instanceof Target)) {
            throw new Exception("Argument expected to be a Target, got something else");
        }

        $this->container = $container;
        $this->target = $target;
        $this->configurator = $configurator;
    }

    /**
     * Executes initialization actions required to setup the data structures
     * related to the tag.
     * <p>
     * This includes:
     * <ul>
     * <li>creation of the task object</li>
     * <li>calling the setters for attributes</li>
     * <li>adding the task to the container object</li>
     * <li>adding a reference to the task (if id attribute is given)</li>
     * <li>executing the task if the container is the &lt;project&gt;
     * element</li>
     * </ul>
     *
     * @param string $tag The tag that comes in
     * @param array $attrs Attributes the tag carries
     * @throws ExpatParseException if attributes are incomplete or invalid
     */
    function init($tag, $attrs) {
        // shorthands
        try {
            $configurator = $this->configurator;
            $project = $this->configurator->project;
            
            $this->task = $project->createTask($tag);
        } catch (BuildException $be) {
            // swallow here, will be thrown again in
            // UnknownElement->maybeConfigure if the problem persists.
            print("Swallowing exception: ".$be->getMessage() . "\n");
        }

        // the task is not known of bat, try to load it on thy fly
        if ($this->task === null) {
            $this->task = new UnknownElement($tag);
            $this->task->setProject($project);
            $this->task->setTaskType($tag);
            $this->task->setTaskName($tag);
        }

        // add file position information to the task (from parser)
        // should be used in task exceptions to provide details
        $this->task->setLocation($this->parser->getLocation());
        $configurator->configureId($task, $attrs);

        // Top level tasks don't have associated targets
        if ($this->target !== null) {
            $this->task->setOwningTarget($this->target);
            $this->container->addTask($this->task);
            $this->task->init();
            $this->wrapper = $this->task->getRuntimeConfigurableWrapper();
            $this->wrapper->setAttributes($attrs);
        } else {
            $this->task->init();
            $configurator->configure($this->task, $attrs, $project);
        }
    }

    /**
     * Executes the task at once if it's directly beneath the <project> tag.
     */
    protected function finished() {
        if ($this->task !== null && $this->target === null) {
            try {
                $this->task->main();
            } catch (Exception $e) {
                $this->task->log($e->getMessage(), PROJECT_MSG_ERR);
                throw $e;
            }
        }
    }

    /**
     * Handles character data.
     *
     * @param string $data The CDATA that comes in
     */
    function characters($data) {
        if ($this->wrapper === null) {
            $configurator = $this->configurator;
            $project = $this->configurator->project;
            try { // try
                $configurator->addText($project, $this->task, $data);
            } catch (BuildException $exc) {
                throw new ExpatParseException($exc->getMessage(), $this->parser->getLocation());
            }
        } else {
            $this->wrapper->addText($data);
        }
    }

    /**
     * Checks for nested tags within the current one. Creates and calls
     * handlers respectively.
     *
     * @param string $name The tag that comes in
     * @param array $attrs Attributes the tag carries
     */
    function startElement($name, $attrs) {
        $project = $this->configurator->project;
        if ($this->task instanceof TaskContainer) {
            //print("TaskHandler::startElement() (TaskContainer) name = $name, attrs = " . implode(",",$attrs) . "\n");
            $th = new TaskHandler($this->parser, $this, $this->configurator, $this->target);
            $th->init($name, $attrs);
        } else {
            //print("TaskHandler::startElement() name = $name, attrs = " . implode(",",$attrs) . "\n");
            $tmp = new NestedElementHandler($this->parser, $this, $this->configurator, $this->task, $this->wrapper, $this->target);
            $tmp->init($name, $attrs);
        }
    }
}
