<commit_before>#!/usr/bin/env python
# coding: utf-8

import xmltodict
from StringIO import StringIO
from lxml import etree


class CannotValidate(Exception):
    pass


class Schema(object):

    def __init__(self, xsd_input=None):
        """
        Parse the XSD and attribute
        """
        if xsd_input is not None:
            xmlschema_doc = etree.parse(StringIO(xsd_input))
            self.xmlschema = etree.XMLSchema(xmlschema_doc)

    def _handle_errors(self, errors_list):
        """
        Handles errors list

        Output Format:
        [(DOMIAN, LINE, COLUMN, LEVEL, TYPE_NAME, MESSAGE),]

        Ex.: [(PARSER, 3, 51, FATAL, ERR_TAG_NAME_MISMATCH, Opening and
            ending tag mismatch: statpage line 3 and startpage),
            (SCHEMASV, 2, 0, ERROR, SCHEMAV_CVC_ELT_1, Element 'wizard':
            No matching global declaration available for the validation root)]
        """
        errors = []
        for error in errors_list:
            errors.append((error.domain_name, error.line, error.column,
                error.level_name, error.type_name, error.message))
        return errors

    def get_validation_errors(self, xml_input):
        """
        This method returns a list of validation errors. If there are no errors
        an empty list is returned
        """
        errors = []
        try:
            """
            Trying to validate the parsing and schema validation
            """
            parsed_xml = etree.parse(StringIO(xml_input))
            if self.xmlschema.assertValid(parsed_xml):
                return errors
        except (etree.DocumentInvalid, etree.XMLSyntaxError), e:
            errors = self._handle_errors(e.error_log)<commit_msg>Ajuste nas definições da API, adicionado o retorno dos erros encontrados no XML tanto de parsing quanto na validação do Schema<commit_after>#!/usr/bin/env python
# coding: utf-8

import xmltodict
from StringIO import StringIO
from lxml import etree


class CannotValidate(Exception):
    pass


class Schema(object):

    def __init__(self, xsd_input=None):
        """
        Parse the XSD and attribute
        """
        if xsd_input is not None:
            xmlschema_doc = etree.parse(StringIO(xsd_input))
            self.xmlschema = etree.XMLSchema(xmlschema_doc)

    def _handle_errors(self, errors_list):
        """
        Handles errors list

        Output Format:
        [(DOMIAN, LINE, COLUMN, LEVEL, TYPE_NAME, MESSAGE),]

        Ex.: [(PARSER, 3, 51, FATAL, ERR_TAG_NAME_MISMATCH, Opening and
            ending tag mismatch: statpage line 3 and startpage),
            (SCHEMASV, 2, 0, ERROR, SCHEMAV_CVC_ELT_1, Element 'wizard':
            No matching global declaration available for the validation root)]
        """
        errors = []
        for error in errors_list:
            errors.append((error.domain_name, error.line, error.column,
                error.level_name, error.type_name, error.message))
        return errors

    def get_validation_errors(self, xml_input):
        """
        This method returns a list of validation errors. If there are no errors
        an empty list is returned
        """
        errors = []
        try:
            """
            Trying to validate the parsing and schema
            """
            parsed_xml = etree.parse(StringIO(xml_input))
            if self.xmlschema.assertValid(parsed_xml):
                return errors
        except (etree.DocumentInvalid, etree.XMLSyntaxError), e:
            errors = self._handle_errors(e.error_log)
{'fraction_non_alphanumeric': 0.08366197183098592, 'fraction_numerical': 0.004507042253521127, 'mean_word_length': 2.234061930783242, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, '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': '6142026', 'n_tokens_mistral': 1042, 'n_tokens_neox': 995, 'n_words': 294}