PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 19require_once 'PEAR/XMLParser.php';
 20require_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 */
 32class PEAR_PackageFile_Parser_v2 extends PEAR_XMLParser
 33{
 34    var $_config;
 35    var $_logger;
 36    var $_registry;
 37
 38    function setConfig(&$c)
 39    {
 40        $this->_config = &$c;
 41        $this->_registry = &$c->getRegistry();
 42    }
 43
 44    function setLogger(&$l)
 45    {
 46        $this->_logger = &$l;
 47    }
 48    /**
 49     * Unindent given string
 50     *
 51     * @param string $str The string that has to be unindented.
 52     * @return string
 53     * @access private
 54     */
 55    function _unIndent($str)
 56    {
 57        // remove leading newlines
 58        $str = preg_replace('/^[\r\n]+/', '', $str);
 59        // find whitespace at the beginning of the first line
 60        $indent_len = strspn($str, " \t");
 61        $indent = substr($str, 0, $indent_len);
 62        $data = '';
 63        // remove the same amount of whitespace from following lines
 64        foreach (explode("\n", $str) as $line) {
 65            if (substr($line, 0, $indent_len) == $indent) {
 66                $data .= substr($line, $indent_len) . "\n";
 67            } else {
 68                $data .= $line . "\n";
 69            }
 70        }
 71        return $data;
 72    }
 73
 74    /**
 75     * post-process data
 76     *
 77     * @param string $data
 78     * @param string $element element name
 79     */
 80    function postProcess($data, $element)
 81    {
 82        if ($element == 'notes') {
 83            return trim($this->_unIndent($data));
 84        }
 85        return trim($data);
 86    }
 87
 88    /**
 89     * @param string
 90     * @param string file name of the package.xml
 91     * @param string|false name of the archive this package.xml came from, if any
 92     * @param string class name to instantiate and return.  This must be PEAR_PackageFile_v2 or
 93     *               a subclass
 94     * @return PEAR_PackageFile_v2
 95     */
 96    function &parse($data, $file, $archive = false, $class = 'PEAR_PackageFile_v2')
 97    {
 98        if (PEAR::isError($err = parent::parse($data, $file))) {
 99            return $err;
100        }
101
102        $ret = new $class;
103        $ret->encoding = $this->encoding;
104        $ret->setConfig($this->_config);
105        if (isset($this->_logger)) {
106            $ret->setLogger($this->_logger);
107        }
108
109        $ret->fromArray($this->_unserializedData);
110        $ret->setPackagefile($file, $archive);
111        return $ret;
112    }
113}