def _validate_tree_with_macros(
            self, document: Document,
            xml_tree: etree.ElementTree) -> List[Diagnostic]:
        """Validates the document after loading all the macros referenced and expands them.

        Args:
            document (Document): [description]
            xml_tree (etree.ElementTree): [description]

        Returns:
            List[Diagnostic]: [description]
        """
        error_range = None
        try:
            error_range = self._get_macros_range(document, xml_tree)
            expanded_tool_tree, _ = xml_macros.load_with_references(
                document.path)
            expanded_xml = self._remove_macros(expanded_tool_tree)
            root = expanded_xml.getroot()
            self.xsd_schema.assertValid(root)
            return []
        except etree.DocumentInvalid as e:
            diagnostics = [
                Diagnostic(error_range,
                           f"Validation error on macro: {error.message}",
                           source=self.server_name)
                for error in e.error_log.filter_from_errors()
            ]
            return diagnostics

        except etree.XMLSyntaxError as e:
            result = Diagnostic(error_range,
                                f"Syntax error on macro: {e.msg}",
                                source=self.server_name)
            return [result]
    def _validate_expanded(self,
                           tool: GalaxyToolXmlDocument) -> List[Diagnostic]:
        """Validates the document after loading all the macros referenced and expands them.

        Args:
            document (Document): [description]
            xml_tree (etree.ElementTree): [description]

        Returns:
            List[Diagnostic]: [description]
        """
        try:
            expanded_tool_tree, _ = xml_macros.load_with_references(tool.path)
            expanded_xml = remove_macros(expanded_tool_tree)
            root = expanded_xml.getroot()
            etree.indent(root, space=DEFAULT_INDENTATION)
            content = etree.tostring(root, pretty_print=True, encoding=str)
            formatted_xml = etree.fromstring(content)
            self.xsd_schema.assertValid(formatted_xml)
            return []
        except etree.DocumentInvalid as e:
            diagnostics = self._build_diagnostics_for_expanded_macros(tool, e)
            return diagnostics

        except etree.XMLSyntaxError as e:
            return self._build_diagnostics_for_macros_file_syntax_error(
                tool, e)
 def _get_expanded_tool_document(self, tool_document: GalaxyToolXmlDocument) -> GalaxyToolXmlDocument:
     """If the given tool document uses macros, a new tool document with the expanded macros is returned,
     otherwise, the same document is returned.
     """
     if tool_document.uses_macros:
         try:
             document = tool_document.document
             expanded_tool_tree, _ = xml_macros.load_with_references(document.path)
             expanded_tool_tree = cast(etree._ElementTree, expanded_tool_tree)
             expanded_source = etree.tostring(expanded_tool_tree, encoding=str)
             expanded_document = Document(uri=document.uri, source=expanded_source, version=document.version)
             return GalaxyToolXmlDocument(expanded_document)
         except BaseException:
             return tool_document
     return tool_document