示例#1
0
def get_acrn_config_element(xsd_file):
    """
    return the root element for the xsd file
    :param xsd_file: the input xsd schema file
    :return: the root element of the xsd file
    """
    # schema = XMLSchema11(xsd_file)
    xsd_doc = etree.parse(xsd_file)
    xsd_doc.xinclude()
    schema = XMLSchema11(etree.tostring(xsd_doc, encoding="unicode"))

    xsd_element_root = schema.root_elements[0]
    acrn_config_element_root = xsd_2_acrn_config_element(xsd_element_root)
    # doc_dict = acrn_config_element_2_doc_dict(acrn_config_element_root, {})
    # enum_dict = acrn_config_element_2_enum_dict(acrn_config_element_root, {})
    # xpath_dict = acrn_config_element_2_xpath_dict(acrn_config_element_root, {})

    # from pprint import pprint
    # pprint(acrn_config_element_root)
    # pprint(xpath_dict)

    return acrn_config_element_root
示例#2
0
 def setUpClass(cls):
     cls.types = XMLSchema11.builtin_types()
示例#3
0
        def check_xsd_file(self):
            if expected_errors > 0:
                xs = schema_class(xsd_file,
                                  validation='lax',
                                  locations=locations,
                                  defuse=defuse,
                                  loglevel=loglevel)
            else:
                xs = schema_class(xsd_file,
                                  locations=locations,
                                  defuse=defuse,
                                  loglevel=loglevel)
            self.errors.extend(xs.maps.all_errors)

            if inspect:
                components_ids = set(
                    [id(c) for c in xs.maps.iter_components()])
                components_ids.update(
                    id(c) for c in xs.meta_schema.iter_components())
                missing = [
                    c for c in SchemaObserver.components
                    if id(c) not in components_ids
                ]
                if missing:
                    raise ValueError("schema missing %d components: %r" %
                                     (len(missing), missing))

            # Pickling test (only for Python 3, skip inspected schema classes test)
            if not inspect:
                try:
                    obj = pickle.dumps(xs)
                    deserialized_schema = pickle.loads(obj)
                except pickle.PicklingError:
                    # Don't raise if some schema parts (eg. a schema loaded from remote)
                    # are built with the SafeXMLParser that uses pure Python elements.
                    for e in xs.maps.iter_components():
                        elem = getattr(e, 'elem', getattr(e, 'root', None))
                        if isinstance(elem, py_etree_element):
                            break
                    else:
                        raise
                else:
                    self.assertTrue(
                        isinstance(deserialized_schema, XMLSchemaBase))
                    self.assertEqual(xs.built, deserialized_schema.built)

            # XPath API tests
            if not inspect and not self.errors:
                context = XMLSchemaContext(xs)
                elements = [x for x in xs.iter()]
                context_elements = [
                    x for x in context.iter() if isinstance(x, XsdValidator)
                ]
                self.assertEqual(context_elements,
                                 [x for x in context.iter_descendants()])
                self.assertEqual(context_elements, elements)

            # Checks on XSD types
            for xsd_type in xs.maps.iter_components(xsd_classes=XsdType):
                self.assertNotEqual(xsd_type.content_type_label, 'unknown')

            # Check that the schema is valid also with XSD 1.1 validator
            if not expected_errors and schema_class.XSD_VERSION == '1.0':
                try:
                    XMLSchema11(xsd_file,
                                locations=locations,
                                defuse=defuse,
                                loglevel=loglevel)
                except XMLSchemaParseError as err:
                    if not isinstance(err.validator, Xsd11ComplexType) or \
                            "is simple or has a simple content" not in str(err):
                        raise  # Not a case of forbidden complex content extension

                    xs = schema_class(xsd_file,
                                      validation='lax',
                                      locations=locations,
                                      defuse=defuse,
                                      loglevel=loglevel)
                    for error in xs.all_errors:
                        if not isinstance(err.validator, Xsd11ComplexType) or \
                                "is simple or has a simple content" not in str(err):
                            raise error