AbstractHandler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /*
  3. * $Id: AbstractHandler.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/parser/ExpatParseException.php';
  22. /**
  23. * This is an abstract class all SAX handler classes must extend
  24. *
  25. * @author Andreas Aderhold <andi@binarycloud.com>
  26. * @copyright © 2001,2002 THYRELL. All rights reserved
  27. * @version $Revision: 905 $
  28. * @package phing.parser
  29. */
  30. abstract class AbstractHandler {
  31. public $parentHandler = null;
  32. public $parser = null;
  33. /**
  34. * Constructs a SAX handler parser.
  35. *
  36. * The constructor must be called by all derived classes.
  37. *
  38. * @param object the parser object
  39. * @param object the parent handler of this handler
  40. */
  41. protected function __construct($parser, $parentHandler) {
  42. $this->parentHandler = $parentHandler;
  43. $this->parser = $parser;
  44. $this->parser->setHandler($this);
  45. }
  46. /**
  47. * Gets invoked when a XML open tag occurs
  48. *
  49. * Must be overloaded by the child class. Throws an ExpatParseException
  50. * if there is no handler registered for an element.
  51. *
  52. * @param string the name of the XML element
  53. * @param array the attributes of the XML element
  54. */
  55. public function startElement($name, $attribs) {
  56. throw new ExpatParseException("Unexpected element $name");
  57. }
  58. /**
  59. * Gets invoked when element closes method.
  60. *
  61. */
  62. protected function finished() {}
  63. /**
  64. * Gets invoked when a XML element ends.
  65. *
  66. * Can be overloaded by the child class. But should not. It hands
  67. * over control to the parentHandler of this.
  68. *
  69. * @param string the name of the XML element
  70. */
  71. public function endElement($name) {
  72. $this->finished();
  73. $this->parser->setHandler($this->parentHandler);
  74. }
  75. /**
  76. * Invoked by occurance of #PCDATA.
  77. *
  78. * @param string the name of the XML element
  79. * @exception ExpatParserException if there is no CDATA but method
  80. * was called
  81. * @access public
  82. */
  83. public function characters($data) {
  84. $s = trim($data);
  85. if (strlen($s) > 0) {
  86. throw new ExpatParseException("Unexpected text '$s'", $this->parser->getLocation());
  87. }
  88. }
  89. }