test invalid xml

This commit is contained in:
Emmanuel Garette 2020-12-24 15:40:01 +01:00
parent 81d30612e8
commit f8959ef16f
7 changed files with 18 additions and 4 deletions

View file

@ -3,7 +3,7 @@ from os.path import join, isfile, basename, isdir
from os import listdir
#from io import BytesIO
from lxml.etree import DTD, parse, tostring # , XMLParser
from lxml.etree import DTD, parse, tostring, XMLSyntaxError
from .i18n import _
from .error import DictConsistencyError
@ -34,10 +34,12 @@ class XMLReflector(object):
:returns: the root element tree object
"""
# document = parse(BytesIO(xmlfile), XMLParser(remove_blank_text=True))
document = parse(xmlfile)
try:
document = parse(xmlfile)
except XMLSyntaxError as err:
raise DictConsistencyError(_(f'{xmlfile} is not an XML file: {err}'), 52)
if not self.dtd.validate(document):
raise DictConsistencyError(_(f'"{xmlfile}" not a valid xml file: {self.dtd.error_log.filter_from_errors()[0]}'), 43)
raise DictConsistencyError(_(f'"{xmlfile}" not a valid XML file: {self.dtd.error_log.filter_from_errors()[0]}'), 43)
return document.getroot()
def load_xml_from_folders(self, xmlfolders):

View file

@ -0,0 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<services>
<unknown>
</unknown>
</services>
</rougail>

View file

@ -0,0 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<rougail>
<services/>
</services>
</rougail>