/gespac/config/PEAR/PEAR/PackageFile/Parser/v2.php

http://gespac.googlecode.com/ · PHP · 113 lines · 55 code · 6 blank · 52 comment · 7 complexity · a5f847fdba24116593efc223425a9609 MD5 · raw file

  1. <?php
  2. /**
  3. * package.xml parsing class, package.xml version 2.0
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * @category pear
  8. * @package PEAR
  9. * @author Greg Beaver <cellog@php.net>
  10. * @copyright 1997-2009 The Authors
  11. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  12. * @version CVS: $Id: v2.php 276385 2009-02-24 23:46:03Z dufuz $
  13. * @link http://pear.php.net/package/PEAR
  14. * @since File available since Release 1.4.0a1
  15. */
  16. /**
  17. * base xml parser class
  18. */
  19. require_once 'PEAR/XMLParser.php';
  20. require_once 'PEAR/PackageFile/v2.php';
  21. /**
  22. * Parser for package.xml version 2.0
  23. * @category pear
  24. * @package PEAR
  25. * @author Greg Beaver <cellog@php.net>
  26. * @copyright 1997-2009 The Authors
  27. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  28. * @version Release: @PEAR-VER@
  29. * @link http://pear.php.net/package/PEAR
  30. * @since Class available since Release 1.4.0a1
  31. */
  32. class PEAR_PackageFile_Parser_v2 extends PEAR_XMLParser
  33. {
  34. var $_config;
  35. var $_logger;
  36. var $_registry;
  37. function setConfig(&$c)
  38. {
  39. $this->_config = &$c;
  40. $this->_registry = &$c->getRegistry();
  41. }
  42. function setLogger(&$l)
  43. {
  44. $this->_logger = &$l;
  45. }
  46. /**
  47. * Unindent given string
  48. *
  49. * @param string $str The string that has to be unindented.
  50. * @return string
  51. * @access private
  52. */
  53. function _unIndent($str)
  54. {
  55. // remove leading newlines
  56. $str = preg_replace('/^[\r\n]+/', '', $str);
  57. // find whitespace at the beginning of the first line
  58. $indent_len = strspn($str, " \t");
  59. $indent = substr($str, 0, $indent_len);
  60. $data = '';
  61. // remove the same amount of whitespace from following lines
  62. foreach (explode("\n", $str) as $line) {
  63. if (substr($line, 0, $indent_len) == $indent) {
  64. $data .= substr($line, $indent_len) . "\n";
  65. } else {
  66. $data .= $line . "\n";
  67. }
  68. }
  69. return $data;
  70. }
  71. /**
  72. * post-process data
  73. *
  74. * @param string $data
  75. * @param string $element element name
  76. */
  77. function postProcess($data, $element)
  78. {
  79. if ($element == 'notes') {
  80. return trim($this->_unIndent($data));
  81. }
  82. return trim($data);
  83. }
  84. /**
  85. * @param string
  86. * @param string file name of the package.xml
  87. * @param string|false name of the archive this package.xml came from, if any
  88. * @param string class name to instantiate and return. This must be PEAR_PackageFile_v2 or
  89. * a subclass
  90. * @return PEAR_PackageFile_v2
  91. */
  92. function &parse($data, $file, $archive = false, $class = 'PEAR_PackageFile_v2')
  93. {
  94. if (PEAR::isError($err = parent::parse($data, $file))) {
  95. return $err;
  96. }
  97. $ret = new $class;
  98. $ret->encoding = $this->encoding;
  99. $ret->setConfig($this->_config);
  100. if (isset($this->_logger)) {
  101. $ret->setLogger($this->_logger);
  102. }
  103. $ret->fromArray($this->_unserializedData);
  104. $ret->setPackagefile($file, $archive);
  105. return $ret;
  106. }
  107. }