Skip to content

Commit

Permalink
updates xpath validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolea PLESCO authored and Kolea PLESCO committed Apr 8, 2024
1 parent 91fe65d commit be2814f
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions ted_sws/notice_validator/adapters/xpath_coverage_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import List, Set, Dict

from jinja2 import Environment, PackageLoader
from saxonche import PySaxonProcessor, PySaxonApiError
from saxonche import PySaxonProcessor, PySaxonApiError, PyXPathProcessor

from ted_sws.core.model.manifestation import XPATHCoverageValidationReport, XPATHCoverageValidationAssertion, \
XPATHCoverageValidationResult
Expand Down Expand Up @@ -51,8 +51,9 @@ def xpath_coverage_validation_report(self, notice: Notice) -> XPATHCoverageValid
mapping_suite_identifier=self.mapping_suite_id)

xpaths: List[str] = []
xp = self.init_xp_processor(notice)
for xpath in self.get_all_conceptual_xpaths():
if self.check_xpath_expression_with_xml(notice.xml_manifestation.object_data, xpath):
if self.check_xpath_expression(xpath, xp):
xpaths.append(xpath)
notice_xpaths: XPathDict = {notice.ted_id: xpaths}
self.validate_xpath_coverage_report(report, notice_xpaths, xpaths)
Expand All @@ -77,19 +78,25 @@ def extract_namespaces(cls, xml_content):
return namespaces

@classmethod
def check_xpath_expression_with_xml(cls, xml_content, xpath_expression) -> bool:
def init_xp_processor(cls, notice: Notice) -> PyXPathProcessor:
xml_content = notice.xml_manifestation.object_data
namespaces = cls.extract_namespaces(xml_content)
with PySaxonProcessor(license=False) as proc:
xp = proc.new_xpath_processor()
for prefix, ns_uri in namespaces.items():
xp.declare_namespace(prefix, ns_uri)
document = proc.parse_xml(xml_text=xml_content)
xp.set_context(xdm_item=document)
try:
item = xp.evaluate_single(xpath_expression)
return True if item else False
except PySaxonApiError:
return False
proc = PySaxonProcessor(license=False)
xp = proc.new_xpath_processor()
for prefix, ns_uri in namespaces.items():
xp.declare_namespace(prefix, ns_uri)
document = proc.parse_xml(xml_text=xml_content)
xp.set_context(xdm_item=document)

return xp

@classmethod
def check_xpath_expression(cls, xpath_expression: str, xp: PyXPathProcessor) -> bool:
try:
item = xp.evaluate_single(xpath_expression)
return True if item else False
except PySaxonApiError:
return False

def xpath_assertions(
self, notice_xpaths: XPathDict, xpaths_list: List[str]
Expand Down Expand Up @@ -130,10 +137,9 @@ def xpath_coverage_validation_summary_report(
for report_notice in notices:
notice = report_notice.notice
xpaths: List[str] = []
xp = self.init_xp_processor(report_notice.notice)
for xpath in self.get_all_conceptual_xpaths():
if self.check_xpath_expression_with_xml(
report_notice.notice.xml_manifestation.object_data, xpath
):
if self.check_xpath_expression(xpath, xp):
xpaths.append(xpath)

notice_xpaths[notice.ted_id] = xpaths
Expand Down

0 comments on commit be2814f

Please sign in to comment.