<commit_before>########################################################################
# amara/writers/xmlwriter.py
"""
XML writer for XSLT output
"""

import operator
import itertools

from amara.lib import xmlstring
from amara.namespaces import XML_NAMESPACE, XMLNS_NAMESPACE
from amara.writers import WriterError, streamwriter
from amara.writers._xmlprinters import xmlprinter, xmlprettyprinter

DEFAULT_GENERATED_PREFIX = u"org.4suite.4xslt.ns"


class xmlwriter(streamwriter):
    """
    Takes events such as those generated by an XSLT processor and
    invokes a serializer to produce XML.
    """
    GENERATED_PREFIX = DEFAULT_GENERATED_PREFIX + "%s"

    _printer = None
    _canonical_form = None
    _need_doctype = True
    _element_name = None
    _element_namespace = None
    _namespaces = None
    _attributes = None

    def _complete_element(self,
                          _value_getter=operator.itemgetter(1),
                          _imap=itertools.imap):
        if self._element_name:
            if self._canonical_form:
                namespaces = sorted(self._namespaces[-1].items())
                attributes = sorted(self._attributes.items())
                attributes = _imap(_value_getter, attributes)
            else:
                namespaces = self._namespaces[-1].iteritems()
                attributes = self._attributes.itervalues()
            # Create a genexp of new namespaces for the printer
            inscope_namespaces = self._namespaces[-2]
            namespaces = ( (prefix, namespace or u'')
                           for prefix, namespace in namespaces
                           if prefix not in inscope_namespaces
                           or inscope_namespaces[prefix] != namespace
                           )
            self._printer.start_element(self._element_namespace,
                                        self._element_name, namespaces,
                                        attributes)

            self._element_name = self._element_namespace = None
            self._attributes = {}
        return

    def start_document(self):
        params = self.output_parameters
        version = params.setdefault('version', '1.0')
        media_type = params.setdefault('media_type', 'text/xml')
        self._canonical_form = params.setdefault('canonical_form', False)
        if self._canonical_form:
            self._need_doctype = False
            if version != '1.0':
                raise WriterError()
            # Override user-supplied values
            encoding = params.encoding = 'UTF-8'
            indent = params.indent = False
            bom = params.byte_order_mark = False
            omit_decl = params.omit_xml_declaration = True
        else:
            encoding = params.setdefault('encoding', 'UTF-8').encode('ascii')
            indent = params.setdefault('indent', False)
            bom = params.byte_order_mark
            omit_decl = params.setdefault('omit_xml_declaration', False)

        if indent:
            printer_class = xmlprettyprinter
        else:
            printer_class = xmlprinter
        self._printer = printer_class(self.stream, encoding, bom,
                                      self._canonical_form)

        if not omit_decl:
            self._printer.start_document(version.encode('ascii'),
                                         self.output_parameters.standalone)

        self._namespaces = [{None: None,
                             'xml': XML_NAMESPACE,
                             'xmlns': XMLNS_NAMESPACE}]
        self._attributes = {}
        return

    def end_document(self):
        self._complete_element()
        self._printer.end_document()
        return

    def text(self, data, disable_escaping=False):
        self._complete_element()
        if self._canonical_form:
            data = data.replace('\r\n', '\r')
        self._printer.text(data, disable_escaping)<commit_msg>Normalize `standalone` to proper values<commit_after>########################################################################
# amara/writers/xmlwriter.py
"""
XML writer for XSLT output
"""

import operator
import itertools

from amara.lib import xmlstring
from amara.namespaces import XML_NAMESPACE, XMLNS_NAMESPACE
from amara.writers import WriterError, streamwriter
from amara.writers._xmlprinters import xmlprinter, xmlprettyprinter

DEFAULT_GENERATED_PREFIX = u"org.4suite.4xslt.ns"


class xmlwriter(streamwriter):
    """
    Takes events such as those generated by an XSLT processor and
    invokes a serializer to produce XML.
    """
    GENERATED_PREFIX = DEFAULT_GENERATED_PREFIX + "%s"

    _printer = None
    _canonical_form = None
    _need_doctype = True
    _element_name = None
    _element_namespace = None
    _namespaces = None
    _attributes = None

    def _complete_element(self,
                          _value_getter=operator.itemgetter(1),
                          _imap=itertools.imap):
        if self._element_name:
            if self._canonical_form:
                namespaces = sorted(self._namespaces[-1].items())
                attributes = sorted(self._attributes.items())
                attributes = _imap(_value_getter, attributes)
            else:
                namespaces = self._namespaces[-1].iteritems()
                attributes = self._attributes.itervalues()
            # Create a genexp of new namespaces for the printer
            inscope_namespaces = self._namespaces[-2]
            namespaces = ( (prefix, namespace or u'')
                           for prefix, namespace in namespaces
                           if prefix not in inscope_namespaces
                           or inscope_namespaces[prefix] != namespace
                           )
            self._printer.start_element(self._element_namespace,
                                        self._element_name, namespaces,
                                        attributes)

            self._element_name = self._element_namespace = None
            self._attributes = {}
        return

    def start_document(self):
        params = self.output_parameters
        version = params.setdefault('version', '1.0')
        media_type = params.setdefault('media_type', 'text/xml')
        self._canonical_form = params.setdefault('canonical_form', False)
        if self._canonical_form:
            self._need_doctype = False
            if version != '1.0':
                raise WriterError()
            # Override user-supplied values
            encoding = params.encoding = 'UTF-8'
            indent = params.indent = False
            bom = params.byte_order_mark = False
            omit_decl = params.omit_xml_declaration = True
        else:
            encoding = params.setdefault('encoding', 'UTF-8').encode('ascii')
            indent = params.setdefault('indent', False)
            bom = params.byte_order_mark
            omit_decl = params.setdefault('omit_xml_declaration', False)

        if indent:
            printer_class = xmlprettyprinter
        else:
            printer_class = xmlprinter
        self._printer = printer_class(self.stream, encoding, bom,
                                      self._canonical_form)

        if not omit_decl:
            standalone = self.output_parameters.standalone
            if standalone is not None:
                standalone = standalone and 'yes' or 'no'
            self._printer.start_document(version.encode('ascii'), standalone)

        self._namespaces = [{None: None,
                             'xml': XML_NAMESPACE,
                             'xmlns': XMLNS_NAMESPACE}]
        self._attributes = {}
        return

    def end_document(self):
        self._complete_element()
        self._printer.end_document()
        return

    def text(self, data, disable_escaping=False):
        self._complete_element()
        if self._canonical_form:
            data = data.replace('\r\n', '\r')
        self._printer.text(data, disable_escaping)
{'fraction_non_alphanumeric': 0.0972709063595393, 'fraction_numerical': 0.0030045067601402104, 'mean_word_length': 1.9468830689782368, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 26}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3712655', 'n_tokens_mistral': 2078, 'n_tokens_neox': 2008, 'n_words': 452}