<commit_before><?php
/**
 * Pimcore
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.pimcore.org/license
 *
 * @copyright  Copyright (c) 2009-2014 pimcore GmbH (http://www.pimcore.org)
 * @license    http://www.pimcore.org/license     New BSD License
 */

namespace Pimcore\Tool;

class XmlWriter extends \Zend_Config_Writer_Xml {

    /**
     * name of the root element
     *
     * @var string
     */
    protected $rootElementName = 'data';

    /**
     * Attributes for the root element
     *
     * @var array
     */
    protected $rootElementAttributes = array();

    /**
     * @var string
     */
    protected $encoding = 'utf-8';

    /**
     * array of options to set
     *
     * @param array $options
     */
    public function __construct($options = array()){
        foreach($options as $key => $value){
            $setter = "set" . ucfirst($key);
            if(method_exists($this,$setter)){
                $this->$setter($value);
            }
        }
    }

    /**
     * @return array
     */
    public function getRootElementAttributes()
    {
        return $this->rootElementAttributes;
    }

    /**
     * @param array $rootElementAttributes
     *
     * @return $this
     */
    public function setRootElementAttributes($rootElementAttributes)
    {
        $this->rootElementAttributes = $rootElementAttributes;
        return $this;
    }

    /**
     * @param string $name
     *
     * @return $this
     */
    public function setRootElementName($name){
        $this->rootElementName = $name;
        return $this;
    }

    /**
     * @param \Zend_Config $config
     *
     * @return $this|\Zend_Config_Writer
     */
    public function setConfig($config)
    {
        if(is_array($config)){
            $config = new \Zend_Config($config);
        }
        $this->_config = $config;

        return $this;
    }

    /**
     * @return string
     */
    public function getRootElementName(){
        return $this->rootElementName;
    }

    /**
     * @param $encoding
     *
     * @return $this
     */
    public function setEncoding($encoding){
        $this->encoding  = $encoding;
        return $this;
    }

    /**
     * @return string
     */
    public function getEncoding(){
        return $this->encoding;
    }

    /**
     * returns the XML string
     *
     * @return string
     * @throws \Zend_Config_Exception
     */
    public function render()
    {

        $xml         = new \SimpleXMLElement('<'.$this->getRootElementName().' encoding="' . $this->getEncoding().'"/>');
        if($this->_config){
            $extends     = $this->_config->getExtends();
            $sectionName = $this->_config->getSectionName();
            if (is_string($sectionName)) {
                $child = $xml->addChild($sectionName);

                $this->_addBranch($this->_config, $child, $xml);
            } else {
                foreach ($this->_config as $sectionName => $data) {
                    if (!($data instanceof \Zend_Config)) {
                        $xml->addChild($sectionName, (string) $data);
                    } else {
                        $child = $xml->addChild($sectionName);

                        if (isset($extends[$sectionName])) {
                            $child->addAttribute('zf:extends', $extends[$sectionName], \Zend_Config_Xml::XML_NAMESPACE);
                        }

                        $this->_addBranch($data, $child, $xml);
                    }
                }
            }
        }

        $dom = dom_import_simplexml($xml)->ownerDocument;
        $dom->formatOutput = true;

        $xmlString = $dom->saveXML();<commit_msg>Updated XMLWriter to set encoding to the xml-prolog and added option to enable/disable output formatting<commit_after><?php
/**
 * Pimcore
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.pimcore.org/license
 *
 * @copyright  Copyright (c) 2009-2014 pimcore GmbH (http://www.pimcore.org)
 * @license    http://www.pimcore.org/license     New BSD License
 */

namespace Pimcore\Tool;

class XmlWriter extends \Zend_Config_Writer_Xml {

    /**
     * name of the root element
     *
     * @var string
     */
    protected $rootElementName = 'data';

    /**
     * Attributes for the root element
     *
     * @var array
     */
    protected $rootElementAttributes = array();

    /**
     * @var string
     */
    protected $encoding = 'utf-8';

    /**
     * array of options to set
     *
     * @param array $options
     */
    public function __construct($options = array()){
        foreach($options as $key => $value){
            $setter = "set" . ucfirst($key);
            if(method_exists($this,$setter)){
                $this->$setter($value);
            }
        }
    }

    /**
     * @var bool
     */
    protected $formatOutput = true;

    /**
     * @return array
     */
    public function getRootElementAttributes()
    {
        return $this->rootElementAttributes;
    }

    /**
     * @param array $rootElementAttributes
     *
     * @return $this
     */
    public function setRootElementAttributes($rootElementAttributes)
    {
        $this->rootElementAttributes = $rootElementAttributes;
        return $this;
    }

    /**
     * @param string $name
     *
     * @return $this
     */
    public function setRootElementName($name){
        $this->rootElementName = $name;
        return $this;
    }

    /**
     * @param \Zend_Config $config
     *
     * @return $this|\Zend_Config_Writer
     */
    public function setConfig($config)
    {
        if(is_array($config)){
            $config = new \Zend_Config($config);
        }
        $this->_config = $config;

        return $this;
    }

    /**
     * @return string
     */
    public function getRootElementName(){
        return $this->rootElementName;
    }

    /**
     * @param $encoding
     *
     * @return $this
     */
    public function setEncoding($encoding){
        $this->encoding  = $encoding;
        return $this;
    }

    /**
     * @return string
     */
    public function getEncoding(){
        return $this->encoding;
    }

    /**
     * @return boolean
     */
    public function getFormatOutput()
    {
        return $this->formatOutput;
    }

    /**
     * @param boolean $formatOutput
     *
     * @return $this
     */
    public function setFormatOutput($formatOutput)
    {
        $this->formatOutput = $formatOutput;
        return $this;
    }



    /**
     * returns the XML string
     *
     * @return string
     * @throws \Zend_Config_Exception
     */
    public function render()
    {

        $xml         = new \SimpleXMLElement('<'.$this->getRootElementName().' />');
        if($this->_config){
            $extends     = $this->_config->getExtends();
            $sectionName = $this->_config->getSectionName();
            if (is_string($sectionName)) {
                $child = $xml->addChild($sectionName);

                $this->_addBranch($this->_config, $child, $xml);
            } else {
                foreach ($this->_config as $sectionName => $data) {
                    if (!($data instanceof \Zend_Config)) {
                        $xml->addChild($sectionName, (string) $data);
                    } else {
                        $child = $xml->addChild($sectionName);

                        if (isset($extends[$sectionName])) {
                            $child->addAttribute('zf:extends', $extends[$sectionName], \Zend_Config_Xml::XML_NAMESPACE);
                        }

                        $this->_addBranch($data, $child, $xml);
                    }
                }
            }
        }

        $dom = dom_import_simplexml($xml)->ownerDocument;
        if($encoding = $this->getEncoding()){
            $dom->encoding = $encoding;
        }

        $dom->formatOutput = $this->getFormatOutput();

        $xmlString = $dom->saveXML();
{'fraction_non_alphanumeric': 0.13317132442284327, 'fraction_numerical': 0.002187120291616039, 'mean_word_length': 1.7391014975041597, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 19}, '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': '2750274', 'n_tokens_mistral': 2374, 'n_tokens_neox': 2287, 'n_words': 495}