summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/parser/TaskHandler.php
blob: 976aebf26be55cdecda43a6f8baf89026bc31971 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/*
 *  $Id: TaskHandler.php,v 1.10 2005/10/04 19:13:44 hlellelid Exp $
 *
 * 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.10 $
 * @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;
	
	/**
 	 * Wrapper for the parent element, if any. The wrapper for this
	 * element will be added to this wrapper as a child.
	 * @var RuntimeConfigurable
	 */
	private $parentWrapper;
	
	/**
	 * Wrapper for this element which takes care of actually configuring
	 * the element, if this element is contained within a target.
	 * Otherwise the configuration is performed with the configure method.
	 * @see ProjectHelper::configure(Object,AttributeList,Project)
	 */
    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 TaskContainer $container The container object this task is contained in (null for top-level tasks).
	 * @param RuntimeConfigurable $parentWrapper  Wrapper for the parent element, if any.
     * @param Target $target The target object this task is contained in (null for top-level tasks).
     */
    function __construct(AbstractSAXParser $parser, $parentHandler, ProjectConfigurator $configurator, $container = null, $parentWrapper = 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 (($parentWrapper !== null) && !($parentWrapper instanceof RuntimeConfigurable)) {
            throw new Exception("Argument expected to be a RuntimeConfigurable, got something else.");
        }
        if (($target !== null) && !($target instanceof Target)) {
            throw new Exception("Argument expected to be a Target, got something else");
        }

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

    /**
     * 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);
		
		if ($this->container) {
			$this->container->addTask($this->task);
		}
		
        // Top level tasks don't have associated targets
		// FIXME: if we do like Ant 1.6 and create an implicitTarget in the projectconfigurator object
		// then we don't need to check for null here ... but there's a lot of stuff that will break if we
		// do that at this point.
        if ($this->target !== null) {
            $this->task->setOwningTarget($this->target);
            $this->task->init();
            $this->wrapper = $this->task->getRuntimeConfigurableWrapper();
            $this->wrapper->setAttributes($attrs);
			if ($this->parentWrapper !== null) { // this may not make sense only within this if-block, but it
												// seems to address current use cases adequately
		    	$this->parentWrapper->addChild($this->wrapper);
			}
        } 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->task, $this->wrapper, $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);
        }
    }
}