Exemplo n.º 1
0
def _get_target_namespace(xml_schema):
    """Get the target namespace.
    Args:
        xml_schema:  XML representation of the schema.

    Returns:
        The target namespace.

    """
    try:
        xsd_tree = XSDTree.transform_to_xml(xml_schema)
    except Exception as e:
        raise exceptions.XMLError(str(e))

    root = xsd_tree.find(".")
    if "targetNamespace" in root.attrib:
        target_namespace = root.attrib["targetNamespace"]
        if target_namespace not in list(root.nsmap.values()):
            message = "The use of a targetNamespace without an associated prefix is not supported."
            raise oai_pmh_exceptions.OAIAPILabelledException(
                message=message, status_code=status.HTTP_400_BAD_REQUEST)
    else:
        target_namespace = "http://www.w3.org/2001/XMLSchema"

    return target_namespace
Exemplo n.º 2
0
    def test_generate_extension_with_single_child_attribute_returns_expected_json_dict(
        self, ):
        xsd_files = join("attribute", "single")
        xsd_tree = self.extension_data_handler.get_xsd(xsd_files)
        xsd_element = xsd_tree.xpath(
            "/xs:schema/xs:element/xs:complexType/xs:simpleContent/xs:extension",
            namespaces=self.namespaces,
        )[0]

        xml_tree = self.extension_data_handler.get_xml(xsd_files)
        xml_data = XSDTree.tostring(xml_tree)
        edit_data_tree = XSDTree.transform_to_xml(xml_data)

        # Generate result dict
        result_dict = self.parser.generate_extension(
            xsd_element,
            xsd_tree,
            full_path="/root",
            edit_data_tree=edit_data_tree,
            default_value="entry0",
        )

        # Load expected dictionary and compare with result
        expected_dict = self.extension_data_handler.get_json(xsd_files +
                                                             ".reload")
        self.assertDictEqual(expected_dict, result_dict)
Exemplo n.º 3
0
    def transform(self, xml_inputs):
        """ Transforms the input to a json content

        Args:
            xml_inputs: xml files

        Returns:

        """
        results_transform = []
        # loops on all xml input
        for xml_item in xml_inputs:
            # generate the title document with the sha
            document_name_with_sha = AbstractExporter.get_title_document(
                xml_item['title'], xml_item['xml_content'])
            transform_result = TransformResult()
            # set the document name to the collection
            transform_result.source_document_name = document_name_with_sha
            # for an XML transformation there is a list of one element
            transform_result_content = TransformResultContent()
            transform_result_content.file_name = document_name_with_sha
            # sets the content and extension
            dom = XSDTree.transform_to_xml(xml_item['xml_content'])
            transform_result_content.content_converted = str(
                self.transformation(dom))
            transform_result_content.content_extension = self.extension
            # add the content to the list of content
            transform_result.transform_result_content.append(
                transform_result_content)
            # add the result to the list of result
            results_transform.append(transform_result)
        return results_transform
Exemplo n.º 4
0
    def get_xml_file(filename):
        """Get XML file

        Args:
            filename:

        Returns:

        """
        file_string = ""
        is_in_tag = False

        with open(filename, "r") as file_content:
            file_lines = [
                line.strip("\r\n\t ") for line in file_content.readlines()
            ]

            for line in file_lines:
                if is_in_tag:  # Add space if we are in the tag
                    file_string += " "

                file_string += line

                # Leave the tag if we have one more closing that opening
                if is_in_tag and line.count(">") == line.count("<") + 1:
                    is_in_tag = False
                elif line.count("<") != line.count(
                        ">"
                ):  # In tag if opening and closing count are different
                    is_in_tag = True

                # In any other cases the tag flag doesn't change

        return XSDTree.transform_to_xml(file_string)
Exemplo n.º 5
0
    def test_reload_simple_content_basic(self):
        xsd_files = join("simple_content", "basic")
        xsd_tree = self.complex_type_data_handler.get_xsd(xsd_files)
        xsd_element = xsd_tree.xpath(
            "/xs:schema/xs:complexType", namespaces=self.namespaces
        )[0]

        xml_tree = self.complex_type_data_handler.get_xml(xsd_files)
        xml_data = XSDTree.tostring(xml_tree)
        edit_data_tree = XSDTree.transform_to_xml(xml_data)

        xml_value = xml_tree.xpath("/root", namespaces=self.namespaces)[0].text

        # Generate result dict
        result_string = self.parser.generate_complex_type(
            xsd_element,
            xsd_tree,
            full_path="/root",
            edit_data_tree=edit_data_tree,
            default_value=xml_value,
        )

        # Load expected dictionary and compare with result
        expected_dict = self.complex_type_data_handler.get_json("%s.reload" % xsd_files)
        self.assertDictEqual(result_string, expected_dict)
Exemplo n.º 6
0
    def test_reload_complex_type_basic(self):
        xsd_files = join("complex_type", "basic")
        xsd_tree = self.element_data_handler.get_xsd(xsd_files)
        xsd_element = xsd_tree.xpath(
            "/xs:schema/xs:element", namespaces=self.namespaces
        )[0]

        xml_tree = self.element_data_handler.get_xml(xsd_files)
        xml_data = XSDTree.tostring(xml_tree)
        edit_data_tree = XSDTree.transform_to_xml(xml_data)

        # Generate result dict
        result_dict = self.parser.generate_element(
            xsd_element, xsd_tree, full_path="", edit_data_tree=edit_data_tree
        )

        # Load expected dictionary and compare with result
        expected_dict = self.element_data_handler.get_json(xsd_files + ".reload")
        self.assertDictEqual(expected_dict, result_dict)
Exemplo n.º 7
0
    def test_reload_element_basic(self):
        xsd_files = join("element", "basic")
        xsd_tree = self.choice_data_handler.get_xsd(xsd_files)
        xsd_element = xsd_tree.xpath("/xs:schema/xs:complexType/xs:choice",
                                     namespaces=self.namespaces)[0]

        xml_tree = self.choice_data_handler.get_xml(xsd_files)
        xml_data = XSDTree.tostring(xml_tree)
        edit_data_tree = XSDTree.transform_to_xml(xml_data)

        result_string = self.parser.generate_choice(
            xsd_element,
            xsd_tree,
            full_path="/root",
            edit_data_tree=edit_data_tree)

        expected_element = self.choice_data_handler.get_json(xsd_files +
                                                             ".reload")

        self.assertDictEqual(expected_element, result_string)
Exemplo n.º 8
0
    def _run_test(self, xsd_files):
        xsd_tree = self.sequence_data_handler.get_xsd(xsd_files)
        xsd_element = xsd_tree.xpath("/xs:schema/xs:complexType/xs:sequence",
                                     namespaces=self.namespaces)[0]

        xml_tree = self.sequence_data_handler.get_xml(xsd_files)
        xml_data = XSDTree.tostring(xml_tree)
        edit_data_tree = XSDTree.transform_to_xml(xml_data)

        # Generate result dict
        result_dict = self.parser.generate_sequence(
            xsd_element,
            xsd_tree,
            full_path="/root",
            edit_data_tree=edit_data_tree)

        # Load expected dictionary and compare with result
        expected_dict = self.sequence_data_handler.get_json(xsd_files +
                                                            ".reload")

        return result_dict, expected_dict