from logging import Logger
from xml.etree.ElementTree import Element
import xml.etree.ElementTree as ET


class XMLReader:

    @staticmethod
    def safe_parse_xml(filepath: str, logger: Logger) -> Element:
        """
            Detect external entities and refuse them.
        """
        with open(filepath, "r", encoding="utf-8") as f:
            content = f.read()
        if "<!DOCTYPE" in content or "<!ENTITY" in content:
            logger.error("Error while parsing XML: XML with DTD/external entities not allowed.")
            raise ValueError("XML with DTD/external entities not allowed.")
        return ET.fromstring(content) # nosec B314