<commit_before><?php
/* 
 * (c) <NAME> (2013)
 * 
 * CustomXMLElement is a wrapper around SimpleXMLElement to add a quite simple
 * feature not present in SimpleXMLElement: inheritance of namespaces.
 * 
 * My first attempt was to extend the original SimpleXMLElement class, but 
 * unfortunately the constructor is static and cannot be overriden!
 *
 * It's easier to understand with a simple example:
 *
 *     $xml = '<root xmlns:dc="http://purl.org/dc/elements/1.1/">
 *         <dc:a>
 *           <dc:b >
 *             1.9
 *           </dc:a>
 *         </dc:b>
 *       </root>';
 *
 *     $root = new SimpleXMLElement($xml);
 *     $root->registerXPathNamespace('d', 'http://purl.org/dc/elements/1.1/');
 *     $a = $root->xpath('d:a');
 *     $a[0]->registerXPathNamespace('d', 'http://purl.org/dc/elements/1.1/');
 *     $b = $a[0]->xpath('d:b');
 *     echo trim((string)$b[0]);
 *
 * Since namespaces are not inherited, we have to register them over and over again.
 * Using CustomXMLElement instead;
 *
 *     $root = new CustomXMLElement($xml);
 *     $root->registerXPathNamespace('d', 'http://purl.org/dc/elements/1.1/');
 *     $a = $root->xpath('d:a');
 *     $b = $a->xpath('d:b');
 *     echo trim((string)$b[0]);
 * 
 * And while we're at it, we can add a few convenience methods...
 */

class XmlResultSet {
    public $xmlObjs = array();

    public function __construct(array $xmlFiles) {
        foreach ($xmlFiles as $file) {
            $this->xmlObjs[] = new XmlResult($file);
        }
    }
}

class XmlResult
{
    private $xmlObj;

    public function __construct($file)
    {
        try {
            $this->xmlObj = new SimpleXMLElement($file, 0, true);
        }
        catch (Exception $e) {
            throw new MyException("Invalid argument ($this)($file)(" . $e .
            ")", PHP_ERRORS);
        }
    }

    public function otherFunctions()
    {
        return $this->xmlObj->movie['name']; // whatever
    }
}

class CustomXMLElement {

    #
    # See https://github.com/draffter/FollowFunctionPHP/blob/master/_php/SimpleXML.php
    # for list of functions with arguments
    #
    function __construct($elem, $inherit_from=null) {
        /*
        $ns = array(
            'srw' => 'http://www.loc.gov/zing/srw/',
            'marc' => 'http://www.loc.gov/MARC21/slim'
        );*/
        $this->namespaces = array();
        if (gettype($elem) == 'string') {
            $this->el = new SimpleXMLElement($elem);
        } else {
            $this->el = $elem; // assume it's a SimpleXMLElement
        }
        if ($inherit_from != null) {
            foreach ($inherit_from->namespaces as $prefix => $uri) {
                $this->registerXPathNamespace($prefix, $uri);
            }
        }

        /*
        $this->el->registerXPathNamespace('srw', $ns['srw']);<commit_msg>throw error if created without any content<commit_after><?php
/* 
 * (c) <NAME> (2013)
 * 
 * CustomXMLElement is a wrapper around SimpleXMLElement to add a quite simple
 * feature not present in SimpleXMLElement: inheritance of namespaces.
 * 
 * My first attempt was to extend the original SimpleXMLElement class, but 
 * unfortunately the constructor is static and cannot be overriden!
 *
 * It's easier to understand with a simple example:
 *
 *     $xml = '<root xmlns:dc="http://purl.org/dc/elements/1.1/">
 *         <dc:a>
 *           <dc:b >
 *             1.9
 *           </dc:a>
 *         </dc:b>
 *       </root>';
 *
 *     $root = new SimpleXMLElement($xml);
 *     $root->registerXPathNamespace('d', 'http://purl.org/dc/elements/1.1/');
 *     $a = $root->xpath('d:a');
 *     $a[0]->registerXPathNamespace('d', 'http://purl.org/dc/elements/1.1/');
 *     $b = $a[0]->xpath('d:b');
 *     echo trim((string)$b[0]);
 *
 * Since namespaces are not inherited, we have to register them over and over again.
 * Using CustomXMLElement instead;
 *
 *     $root = new CustomXMLElement($xml);
 *     $root->registerXPathNamespace('d', 'http://purl.org/dc/elements/1.1/');
 *     $a = $root->xpath('d:a');
 *     $b = $a->xpath('d:b');
 *     echo trim((string)$b[0]);
 * 
 * And while we're at it, we can add a few convenience methods...
 */

class XmlResultSet {
    public $xmlObjs = array();

    public function __construct(array $xmlFiles) {
        foreach ($xmlFiles as $file) {
            $this->xmlObjs[] = new XmlResult($file);
        }
    }
}

class XmlResult
{
    private $xmlObj;

    public function __construct($file)
    {
        try {
            $this->xmlObj = new SimpleXMLElement($file, 0, true);
        }
        catch (Exception $e) {
            throw new MyException("Invalid argument ($this)($file)(" . $e .
            ")", PHP_ERRORS);
        }
    }

    public function otherFunctions()
    {
        return $this->xmlObj->movie['name']; // whatever
    }
}

class CustomXMLElement {

    #
    # See https://github.com/draffter/FollowFunctionPHP/blob/master/_php/SimpleXML.php
    # for list of functions with arguments
    #
    function __construct($elem, $inherit_from=null) {
        /*
        $ns = array(
            'srw' => 'http://www.loc.gov/zing/srw/',
            'marc' => 'http://www.loc.gov/MARC21/slim'
        );*/
        $this->namespaces = array();

        if ($elem == false) {
            throw new Exception("Cannot create a CustomXmlElement with no content");
        }

        if (gettype($elem) == 'string') {
            $this->el = new SimpleXMLElement($elem);
        } else {
            $this->el = $elem; // assume it's a SimpleXMLElement
        }
        if ($inherit_from != null) {
            foreach ($inherit_from->namespaces as $prefix => $uri) {
                $this->registerXPathNamespace($prefix, $uri);
            }
        }

        /*
        $this->el->registerXPathNamespace('srw', $ns['srw']);
{'fraction_non_alphanumeric': 0.16278272789581905, 'fraction_numerical': 0.007196710075394105, 'mean_word_length': 2.21420704845815, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 59, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 22}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7179667', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1836, 'n_words': 427}