1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * The generic element handler class.
22
 *
23
 * This class handles the occurrence of runtime registered tags like
24
 * datatypes (fileset, patternset, etc) and it's possible nested tags. It
25
 * introspects the implementation of the class and sets up the data structures.
26
 *
27
 * @author    Michiel Rook <mrook@php.net>
28
 * @copyright 2001,2002 THYRELL. All rights reserved
29
 * @package   phing.parser
30
 */
31
class ElementHandler extends AbstractHandler
32
{
33

34
    /**
35
     * Reference to the parent object that represents the parent tag
36
     * of this nested element
37
     *
38
     * @var object
39
     */
40
    private $parent;
41

42
    /**
43
     * Reference to the child object that represents the child tag
44
     * of this nested element
45
     *
46
     * @var UnknownElement
47
     */
48
    private $child;
49

50
    /**
51
     *  Reference to the parent wrapper object
52
     *
53
     * @var RuntimeConfigurable
54
     */
55
    private $parentWrapper;
56

57
    /**
58
     *  Reference to the child wrapper object
59
     *
60
     * @var RuntimeConfigurable
61
     */
62
    private $childWrapper;
63

64
    /**
65
     *  Reference to the related target object
66
     *
67
     * @var Target the target instance
68
     */
69
    private $target;
70

71
    /**
72
     * @var ProjectConfigurator
73
     */
74
    private $configurator;
75

76
    /**
77
     *  Constructs a new NestedElement handler and sets up everything.
78
     *
79
     * @param AbstractSAXParser $parser the ExpatParser object
80
     * @param AbstractHandler $parentHandler the parent handler that invoked this handler
81
     * @param ProjectConfigurator $configurator the ProjectConfigurator object
82
     * @param UnknownElement $parent the parent object this element is contained in
83
     * @param RuntimeConfigurable $parentWrapper the parent wrapper object
84
     * @param Target $target the target object this task is contained in
85
     */
86 1
    public function __construct(
87
        AbstractSAXParser $parser,
88
        AbstractHandler $parentHandler,
89
        ProjectConfigurator $configurator,
90
        UnknownElement $parent = null,
91
        RuntimeConfigurable $parentWrapper = null,
92
        Target $target = null
93
    ) {
94 1
        parent::__construct($parser, $parentHandler);
95 1
        $this->configurator = $configurator;
96 1
        if ($parentWrapper != null) {
97 1
            $this->parent = $parentWrapper->getProxy();
98
        } else {
99 1
            $this->parent = $parent;
100
        }
101 1
        $this->parentWrapper = $parentWrapper;
102 1
        $this->target = $target;
103
    }
104

105
    /**
106
     * Executes initialization actions required to setup the data structures
107
     * related to the tag.
108
     * <p>
109
     * This includes:
110
     * <ul>
111
     * <li>creation of the nested element</li>
112
     * <li>calling the setters for attributes</li>
113
     * <li>adding the element to the container object</li>
114
     * <li>adding a reference to the element (if id attribute is given)</li>
115
     * </ul>
116
     *
117
     * @param  string $tag the tag that comes in
118
     * @param  array $attrs attributes the tag carries
119
     * @throws ExpatParseException if the setup process fails
120
     */
121 1
    public function init($tag, $attrs)
122
    {
123 1
        $configurator = $this->configurator;
124 1
        $project = $this->configurator->project;
125

126
        try {
127 1
            $this->child = new UnknownElement(strtolower($tag));
128 1
            $this->child->setTaskName($tag);
129 1
            $this->child->setTaskType($tag);
130 1
            $this->child->setProject($project);
131 1
            $this->child->setLocation($this->parser->getLocation());
132

133 1
            if ($this->target !== null) {
134 1
                $this->child->setOwningTarget($this->target);
135
            }
136

137 1
            if ($this->parent !== null) {
138 1
                $this->parent->addChild($this->child);
139 1
            } elseif ($this->target !== null) {
140 1
                $this->target->addTask($this->child);
141
            }
142

143 1
            $configurator->configureId($this->child, $attrs);
144

145 1
            $this->childWrapper = new RuntimeConfigurable($this->child, $tag);
146 1
            $this->childWrapper->setAttributes($attrs);
147

148 1
            if ($this->parentWrapper !== null) {
149 1
                $this->parentWrapper->addChild($this->childWrapper);
150
            }
151 0
        } catch (BuildException $exc) {
152 0
            throw new ExpatParseException(
153 0
                "Error initializing nested element <$tag>",
154
                $exc,
155 0
                $this->parser->getLocation()
156
            );
157
        }
158
    }
159

160
    /**
161
     * Handles character data.
162
     *
163
     * @param  string $data the CDATA that comes in
164
     * @throws ExpatParseException if the CDATA could not be set-up properly
165
     */
166 1
    public function characters($data)
167
    {
168 1
        $this->childWrapper->addText($data);
169
    }
170

171
    /**
172
     * Checks for nested tags within the current one. Creates and calls
173
     * handlers respectively.
174
     *
175
     * @param string $name the tag that comes in
176
     * @param array $attrs attributes the tag carries
177
     */
178 1
    public function startElement($name, $attrs)
179
    {
180 1
        $eh = new ElementHandler(
181 1
            $this->parser,
182
            $this,
183 1
            $this->configurator,
184 1
            $this->child,
185 1
            $this->childWrapper,
186 1
            $this->target
187
        );
188 1
        $eh->init($name, $attrs);
189
    }
190
}

Read our documentation on viewing source code .

Loading