HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers84/zuiderbos.nl/vendor/nqxcode/phpmorphy/src/phpMorphy/Dict/Writer/Xml.php
<?php
/*
* This file is part of phpMorphy project
*
* Copyright (c) 2007-2012 Kamaev Vladimir <heromantor@users.sourceforge.net>
*
*     This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
*     This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
*     You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/


class phpMorphy_Dict_Writer_Xml extends phpMorphy_Dict_Writer_WriterAbstract {
    const DUMP_EVERY_FLEXIA_MODEL = 64;
    const DUMP_EVERY_LEMMA = 1024;

    private $path;

    function __construct($outPath) {
        parent::__construct();
        $this->path = $outPath;
    }

    function write(phpMorphy_Dict_Source_SourceInterface $source) {
        $this->getObserver()->onStart();

        try {
            $source = phpMorphy_Dict_Source_ValidatingSource::wrap($source);

            $xml_opts = $this->getXmlOptions();
            $writer = $this->createXmlWriter($this->path);

            $writer->startDocument($xml_opts['xml_version'], $xml_opts['xml_encoding']);
            {
                //$writer->writeDtd('phpmorphy', $xml_opts['dtd_pub_id'], $xml_opts['dtd_sys_id']);
                $writer->writeComment('This file generated with ' . __CLASS__ . ' at ' . date('r'));

                // morphy
                $writer->startElement('phpmorphy');
                {
                    $this->writeOptions($writer, $source);
                    $this->writePoses($writer, $source->getPoses());
                    $this->writeGrammems($writer, $source->getGrammems());
                    $this->writeAncodes($writer, $source->getAncodes());
                    $this->writeFlexias($writer, $source->getFlexias());
                    $this->writePrefixes($writer, $source->getPrefixes());
                    $this->writeLemmas($writer, $source->getLemmas());
                }
                $writer->endElement();
            }

            $writer->endDocument();
        } catch (Exception $e) {
            $this->getObserver()->onEnd();
            throw $e;
        }

        $this->getObserver()->onEnd();
    }

    private function writeOptions(XMLWriter $writer, phpMorphy_Dict_Source_SourceInterface $source) {
        $this->log(__METHOD__);

        $writer->startElement('options');
        {
            $writer->startElement('locale');
            {
                $writer->writeAttribute('name', $source->getLanguage());
            }
            $writer->endElement();
        }
        $writer->endElement();
    }

    private function writeFlexias(XMLWriter $writer, $it) {
        $this->log(__METHOD__);
        if(!$this->checkForIteratorNotEmpty($it)) return;

        $count = 0;

        $writer->startElement('flexias');
        {
            foreach($it as $flexia_model) {
                $writer->startElement('flexia_model');
                {
                    $writer->writeAttribute('id', $flexia_model->getId());

                    foreach($flexia_model as $flexia) {
                        $writer->startElement('flexia');
                        {
                            $writer->writeAttribute('prefix', $flexia->getPrefix());
                            $writer->writeAttribute('suffix', $flexia->getSuffix());
                            $writer->writeAttribute('ancode_id', $flexia->getAncodeId());
                        }
                        $writer->endElement();
                    }
                }
                $writer->endElement();

                $count++;

                if(0 == ($count % self::DUMP_EVERY_FLEXIA_MODEL)) {
                    $this->log("$count flexia models done");
                }
            }
        }
        $writer->endElement();

        return $count;
    }

    private function writePrefixes(XMLWriter $writer, $it) {
        $this->log(__METHOD__);
        if(!$this->checkForIteratorNotEmpty($it)) return;

        $count = 0;

        $writer->startElement('prefixes');
        {
            foreach($it as $prefix_set) {
                $writer->startElement('prefix_model');
                {
                    $writer->writeAttribute('id', $prefix_set->getId());

                    foreach($prefix_set as $prefix) {
                        $writer->startElement('prefix');
                        {
                            $writer->writeAttribute('value', $prefix);
                        }
                        $writer->endElement();
                    }
                }
                $writer->endElement();

                $count++;
            }
        }
        $writer->endElement();

        return $count;
    }

    private function writeAccents(XMLWriter $writer, $it) {
        $this->log(__METHOD__);
        if(!$this->checkForIteratorNotEmpty($it)) return;

        $count = 0;

        $writer->startElement('accents');
        {
            foreach($it as $accent_model) {
                $writer->startElement('accent_model');
                {
                    $writer->writeAttribute('id', $accent_model->getId());

                    foreach($accent_model as $accent) {
                        $writer->startElement('accent');
                        {
                            if(!$accent_model->isEmptyAccent($accent)) {
                                $writer->writeAttribute('value', $accent);
                            }
                        }
                        $writer->endElement();
                    }
                }
                $writer->endElement();

                $count++;
            }
        }
        $writer->endElement();

        return $count;
    }

    private function writeLemmas(XMLWriter $writer, $it) {
        $this->log(__METHOD__);
        if(!$this->checkForIteratorNotEmpty($it)) return;

        $count = 0;

        $writer->startElement('lemmas');
        {
            foreach($it as $lemma) {
                $writer->startElement('lemma');
                {
                    $writer->writeAttribute('base', $lemma->getBase());
                    $writer->writeAttribute('flexia_id', $lemma->getFlexiaId());

                    if($lemma->hasPrefixId()) {
                        $writer->writeAttribute('prefix_id', $lemma->getPrefixId());
                    }

                    if($lemma->hasAncodeId()) {
                        $writer->writeAttribute('ancode_id', $lemma->getAncodeId());
                    }
                }
                $writer->endElement();

                $count++;

                if(0 == ($count % self::DUMP_EVERY_LEMMA)) {
                    $this->log("$count lemmas done");
                }
            }
        }
        $writer->endElement();
    }

    private function writePoses(XMLWriter $writer, $it) {
        $this->log(__METHOD__);
        if(!$this->checkForIteratorNotEmpty($it)) return;

        $writer->startElement('poses');
        {
            foreach($it as $pos) {
                $writer->startElement('pos');
                {
                    $writer->writeAttribute('id', $pos->getId());
                    $writer->writeAttribute('name', $pos->getName());
                    $writer->writeAttribute('is_predict', $pos->isPredict() ? '1' : '0');
                }
                $writer->endElement();
            }
        }
        $writer->endElement();
    }

    private function writeGrammems(XMLWriter $writer, $it) {
        $this->log(__METHOD__);
        if(!$this->checkForIteratorNotEmpty($it)) return;

        $writer->startElement('grammems');
        {
            foreach($it as $grammem) {
                $writer->startElement('grammem');
                {
                    $writer->writeAttribute('id', $grammem->getId());
                    $writer->writeAttribute('name', $grammem->getName());
                    $writer->writeAttribute('shift', $grammem->getShift());
                }
                $writer->endElement();
            }
        }
        $writer->endElement();
    }

    private function writeAncodes(XMLWriter $writer, $it) {
        $this->log(__METHOD__);
        if(!$this->checkForIteratorNotEmpty($it)) return;

        $writer->startElement('ancodes');
        {
            foreach($it as $ancode) {
                $writer->startElement('ancode');
                {
                    $writer->writeAttribute('id', $ancode->getId());
                    $writer->writeAttribute('name', $ancode->getName());
                    $writer->writeAttribute('pos_id', $ancode->getPartOfSpeechId());

                    foreach($ancode->getGrammemsIds() as $id) {
                        $writer->startElement('grammem');
                        {
                            $writer->writeAttribute('id', $id);
                        }
                        $writer->endElement();
                    }
                }
                $writer->endElement();
            }
        }
        $writer->endElement();
    }

    private function checkForIteratorNotEmpty($it) {
        if(is_array($it)) {
            return count($it) > 0;
        } else if($it instanceof Traversable) {
            $it->rewind();
            return $it->valid();
        }

        throw new phpMorphy_Exception("Not traversable object given");
    }

    private function getXmlOptions() {
        return array(
            'xml_version' => '1.0',
            'xml_encoding' => 'utf-8',
            'dtd_pub_id' => false,
            'dtd_sys_id' => false, //'morphy.dtd'
        );
    }

    private function createXmlWriter($fileName) {
        $writer = new XMLWriter();

        if(false === $writer->openUri($fileName)) {
            throw new Exception("Can`t create $fileName xml file for XMLWriter");
        }

        $writer->setIndentString("\t");
        $writer->setIndent(4);

        return $writer;
    }
}