<commit_before><?php

namespace Shopware\Components\SwagImportExport\FileIO;

class XmlFileReader implements FileReader
{

    protected $tree;
    protected $iterationPath;
    protected $iterationTag;
    /*
     * @var boolen
     */
    protected $treeStructure = true;
    protected $fileHelper;

    public function __construct($fileHelper)
    {
        $this->fileHelper = $fileHelper;
    }

    public function setTree($tree)
    {
        $this->tree = $tree;
        $this->findIterationNode($tree, array());
    }

    protected function toArrayTree(\DOMElement $node)
    {
        $hasChildren = false;
        $record = array();

        if ($node->hasChildNodes()) {
            foreach ($node->childNodes as $child) {
                if ($child instanceof \DOMElement) {
                    $hasChildren = true;
                    $record[$child->nodeName] = $this->toArrayTree($child);
                }
            }
        }

        if ($node->hasAttributes()) {
            $record["_attributes"] = array();
            foreach ($node->attributes as $attr) {
                $record["_attributes"][$attr->name] = $attr->value;
            }
            if (!$hasChildren) {
                $record["_value"] = $node->nodeValue;
            }
        } else if (!$hasChildren) {
            $record = $node->nodeValue;
        }

        return $record;
    }

    public function readHeader($fileName)
    {
        
    }

    public function readRecords($fileName, $position, $count)
    {
        //todo: add string argument
        $reader = new \XMLReader();
        $reader->open($fileName);

        foreach ($this->iterationPath as $node) {
            $reader->next($node);
            $reader->read();
        }

        // skip records
        $i = 0;
        while ($i < $position && $reader->next($this->iterationTag)) {
            $i++;
        }

        $j = 0;
        $records = array();
        while ($j < $count && $reader->next($this->iterationTag)) {
            $node = $reader->expand();
            $records[] = $this->toArrayTree($node);
            $j++;
        }

        return $records;
    }

    public function readFooter($fileName)
    {
        
    }

    public function hasTreeStructure()
    {
        return $this->treeStructure;
    }

    protected function findIterationNode($node, array $path)
    {
        $path[] = $node["name"];
        foreach ($node['children'] as $child) {
            if ($child['type'] == 'record') {
                $this->iterationPath = $path;
                $this->iterationTag = $child['name'];
                return;
            }

            $this->findIterationNode($child, $path);
        }
    }

    public function getTotalCount($fileName)
    {
        $z = new \XMLReader();
        $z->open($fileName);<commit_msg>Fixed XmlFileReader (multiple iteration nodes support added)<commit_after><?php

namespace Shopware\Components\SwagImportExport\FileIO;

class XmlFileReader implements FileReader
{

    protected $tree;
    protected $iterationPath = array();
    protected $iterationTag = array();
    /*
     * @var boolen
     */
    protected $treeStructure = true;
    protected $fileHelper;

    public function __construct($fileHelper)
    {
        $this->fileHelper = $fileHelper;
    }

    public function setTree($tree)
    {
        $this->tree = $tree;
        $this->findIterationNode($tree, array());
    }

    protected function toArrayTree(\DOMElement $node, $path)
    {
        $hasChildren = false;
        $record = array();
        $currentPath = $path . '/' . $node->nodeName;
        
        if ($node->hasChildNodes()) {
            if (in_array($currentPath, $this->iterationPath)) {
                foreach ($node->childNodes as $child) {
                    if ($child instanceof \DOMElement) {
                        $hasChildren = true;
                        $record[$child->nodeName][] = $this->toArrayTree($child, $currentPath);
                    }
                }
            } else {
                foreach ($node->childNodes as $child) {
                    if ($child instanceof \DOMElement) {
                        $hasChildren = true;
                        $record[$child->nodeName] = $this->toArrayTree($child, $currentPath);
                    }
                }
            }
        }

        if ($node->hasAttributes()) {
            $record["_attributes"] = array();
            foreach ($node->attributes as $attr) {
                $record["_attributes"][$attr->name] = $attr->value;
            }
            if (!$hasChildren) {
                $record["_value"] = $node->nodeValue;
            }
        } else if (!$hasChildren) {
            $record = $node->nodeValue;
        }

        return $record;
    }

    public function readHeader($fileName)
    {
        
    }

    public function readRecords($fileName, $position, $count)
    {
        //todo: add string argument
        $reader = new \XMLReader();
        $reader->open($fileName);

        // find the first iterationNode
        foreach (explode('/', $this->iterationPath[0]) as $node) {
            $reader->next($node);
            $reader->read();
        }

        // skip records
        $i = 0;
        while ($i < $position && $reader->next($this->iterationTag[0])) {
            $i++;
        }

        $j = 0;
        $records = array();
        while ($j < $count && $reader->next($this->iterationTag[0])) {
            $node = $reader->expand();
            $records[] = $this->toArrayTree($node, $this->iterationPath[0]);
        }

        return $records;
    }

    public function readFooter($fileName)
    {
        
    }

    public function hasTreeStructure()
    {
        return $this->treeStructure;
    }

    protected function findIterationNode($node, array $path)
    {
        $path[] = $node["name"];
        foreach ($node['children'] as $child) {
            if (isset($child['adapter'])) {
                $this->iterationPath[] = implode('/', $path);
                $this->iterationTag[] = $child['name'];
            }

            $this->findIterationNode($child, $path);
        }
    }

    public function getTotalCount($fileName)
    {
        $z = new \XMLReader();
        $z->open($fileName);
{'fraction_non_alphanumeric': 0.14453808236603594, 'fraction_numerical': 0.0012720623310542216, 'mean_word_length': 1.6891834117144078, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3516245', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1708, 'n_words': 347}