NestedElementHandler.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /*
  3. * $Id: NestedElementHandler.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. include_once 'phing/IntrospectionHelper.php';
  22. include_once 'phing/TaskContainer.php';
  23. /**
  24. * The nested element handler class.
  25. *
  26. * This class handles the occurance of runtime registered tags like
  27. * datatypes (fileset, patternset, etc) and it's possible nested tags. It
  28. * introspects the implementation of the class and sets up the data structures.
  29. *
  30. * @author Andreas Aderhold <andi@binarycloud.com>
  31. * @copyright © 2001,2002 THYRELL. All rights reserved
  32. * @version $Revision: 905 $ $Date: 2010-10-05 18:28:03 +0200 (Tue, 05 Oct 2010) $
  33. * @access public
  34. * @package phing.parser
  35. */
  36. class NestedElementHandler extends AbstractHandler {
  37. /**
  38. * Reference to the parent object that represents the parent tag
  39. * of this nested element
  40. * @var object
  41. */
  42. private $parent;
  43. /**
  44. * Reference to the child object that represents the child tag
  45. * of this nested element
  46. * @var object
  47. */
  48. private $child;
  49. /**
  50. * Reference to the parent wrapper object
  51. * @var object
  52. */
  53. private $parentWrapper;
  54. /**
  55. * Reference to the child wrapper object
  56. * @var object
  57. */
  58. private $childWrapper;
  59. /**
  60. * Reference to the related target object
  61. * @var object the target instance
  62. */
  63. private $target;
  64. /**
  65. * Constructs a new NestedElement handler and sets up everything.
  66. *
  67. * @param object the ExpatParser object
  68. * @param object the parent handler that invoked this handler
  69. * @param object the ProjectConfigurator object
  70. * @param object the parent object this element is contained in
  71. * @param object the parent wrapper object
  72. * @param object the target object this task is contained in
  73. * @access public
  74. */
  75. function __construct($parser, $parentHandler, $configurator, $parent, $parentWrapper, $target) {
  76. parent::__construct($parser, $parentHandler);
  77. $this->configurator = $configurator;
  78. if ($parent instanceof TaskAdapter) {
  79. $this->parent = $parent->getProxy();
  80. } else {
  81. $this->parent = $parent;
  82. }
  83. $this->parentWrapper = $parentWrapper;
  84. $this->target = $target;
  85. }
  86. /**
  87. * Executes initialization actions required to setup the data structures
  88. * related to the tag.
  89. * <p>
  90. * This includes:
  91. * <ul>
  92. * <li>creation of the nested element</li>
  93. * <li>calling the setters for attributes</li>
  94. * <li>adding the element to the container object</li>
  95. * <li>adding a reference to the element (if id attribute is given)</li>
  96. * </ul>
  97. *
  98. * @param string the tag that comes in
  99. * @param array attributes the tag carries
  100. * @throws ExpatParseException if the setup process fails
  101. * @access public
  102. */
  103. function init($propType, $attrs) {
  104. $configurator = $this->configurator;
  105. $project = $this->configurator->project;
  106. // introspect the parent class that is custom
  107. $parentClass = get_class($this->parent);
  108. $ih = IntrospectionHelper::getHelper($parentClass);
  109. try {
  110. if ($this->parent instanceof UnknownElement) {
  111. $this->child = new UnknownElement(strtolower($propType));
  112. $this->parent->addChild($this->child);
  113. } else {
  114. $this->child = $ih->createElement($project, $this->parent, strtolower($propType));
  115. }
  116. $configurator->configureId($this->child, $attrs);
  117. if ($this->parentWrapper !== null) {
  118. $this->childWrapper = new RuntimeConfigurable($this->child, $propType);
  119. $this->childWrapper->setAttributes($attrs);
  120. $this->parentWrapper->addChild($this->childWrapper);
  121. } else {
  122. $configurator->configure($this->child, $attrs, $project);
  123. $ih->storeElement($project, $this->parent, $this->child, strtolower($propType));
  124. }
  125. } catch (BuildException $exc) {
  126. throw new ExpatParseException("Error initializing nested element <$propType>", $exc, $this->parser->getLocation());
  127. }
  128. }
  129. /**
  130. * Handles character data.
  131. *
  132. * @param string the CDATA that comes in
  133. * @throws ExpatParseException if the CDATA could not be set-up properly
  134. * @access public
  135. */
  136. function characters($data) {
  137. $configurator = $this->configurator;
  138. $project = $this->configurator->project;
  139. if ($this->parentWrapper === null) {
  140. try {
  141. $configurator->addText($project, $this->child, $data);
  142. } catch (BuildException $exc) {
  143. throw new ExpatParseException($exc->getMessage(), $this->parser->getLocation());
  144. }
  145. } else {
  146. $this->childWrapper->addText($data);
  147. }
  148. }
  149. /**
  150. * Checks for nested tags within the current one. Creates and calls
  151. * handlers respectively.
  152. *
  153. * @param string the tag that comes in
  154. * @param array attributes the tag carries
  155. * @access public
  156. */
  157. function startElement($name, $attrs) {
  158. //print(get_class($this) . " name = $name, attrs = " . implode(",",$attrs) . "\n");
  159. if ($this->child instanceof TaskContainer) {
  160. // taskcontainer nested element can contain other tasks - no other
  161. // nested elements possible
  162. $tc = new TaskHandler($this->parser, $this, $this->configurator, $this->child, $this->childWrapper, $this->target);
  163. $tc->init($name, $attrs);
  164. } else {
  165. $neh = new NestedElementHandler($this->parser, $this, $this->configurator, $this->child, $this->childWrapper, $this->target);
  166. $neh->init($name, $attrs);
  167. }
  168. }
  169. }