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