xsdata.analyzer module

class xsdata.analyzer.ClassAnalyzer(processed=<factory>, class_index=<factory>, substitutions_index=<factory>)[source]

Bases: xsdata.utils.classes.ClassUtils

Class analyzer is responsible to minize the final classes footprint by merging and flattening extensions and attributes.

Also promotes the classes necessary for generation and demotes the classes to be used as common types for future runs.

Parameters
MAX_DEPENDENCY_CHECK_DEPTH = 5
processed: List = None
class_index: Dict[QName, List[Class]] = None
substitutions_index: Dict[QName, List[Attr]] = None
process(classes)[source]

Process class list in steps.

Steps:
  • Create a class index.

  • Handle duplicate types.

  • Create a substitution index.

  • Flatten classes.

  • Return a final class list for code generators.

Parameters

classes (List[Class]) –

Return type

List[Class]

handle_duplicate_classes()[source]

Remove if possible classes with the same qualified name.

Steps:
  1. Remove classes with missing extension type.

  2. Merge redefined classes.

  3. Fix implied abstract flags.

remove_invalid_classes(classes)[source]

Remove from the given class list any class with missing extension type.

Parameters

classes (List[Class]) –

fetch_classes_for_generation()[source]

Return the qualified classes for code generation.

Return all if no classes are derived from xs:element or xs:complexType.

Return type

List[Class]

create_class_index(classes)[source]

Group classes by their fully qualified name.

Parameters

classes (List[Class]) –

create_substitutions_index()[source]

Create reference attributes for all the classes substitutions and group them by their fully qualified name.

find_attr_type(source, attr_type)[source]

Find the source class for the given class and attribute type.

Parameters
Return type

Optional[Class]

attr_type_is_missing(source, attr_type)[source]

Check if given type declaration is not native and is missing.

Parameters
Return type

bool

find_attr_simple_type(source, attr_type)[source]

Find the source class for the given class and attribute type, excluding enumerations, complex types and self references.

Parameters
Return type

Optional[Class]

find_simple_class(qname)[source]

Find an enumeration or simple type source class for the given qualified name.

Parameters

qname (QName) –

Return type

Optional[Class]

find_class(qname, condition=None)[source]

Find the flattened source class for the given qualified name.

Parameters
Return type

Optional[Class]

flatten_classes()[source]

Flatten the class index objects once.

flatten_class(target)[source]

Simplify class footprint by flattening class extensions, attributes and inner classes.

Steps:
  • Expand attribute groups

  • Flatten extensions

  • Flatten attribute types

  • Add substitution attributes

  • Merge duplicate attributes

  • Create mixed content attribute

  • Flatten inner classes

Parameters

target (Class) –

flatten_enumeration_unions(target)[source]

Convert simple types with a single field which is a union of enums to a standalone enumeration.

Parameters

target (Class) –

flatten_extension(target, extension)[source]

Flatten target class extension based on the extension type.

Types:
  1. Native primitive type (int, str, float, etc)

  2. Simple source type (simpleType, Extension)

  3. Complex source type (ComplexType, Element)

  4. Unknown type

Parameters
flatten_extension_native(target, extension)[source]

Native type flatten extension handler, ignore enumerations.

Parameters
flatten_extension_simple(source, target, ext)[source]

Simple flatten extension handler for common classes eg SimpleType, Restriction.

Steps:
  1. If target is source: drop the extension.

  2. If source is enumeration and target isn’t create default value attribute.

  3. If both source and target are enumerations copy all attributes.

  4. If both source and target are not enumerations copy all attributes.

  5. If target is enumeration: drop the extension.

Parameters
flatten_extension_complex(source, target, ext)[source]

Complex flatten extension handler for primary classes eg ComplexType, Element.

Drop extension when:
  • source includes all target attributes

Copy all attributes when:
  • source includes some of the target attributes

  • source has suffix attribute and target has at least one attribute

  • target has at least one suffix attribute

  • source or target class is abstract

Parameters
expand_attribute_group(target, attr)[source]

Expand a group attribute with the source class attributes.

Clone the attributes and apply the group restrictions as well.

Parameters
flatten_attribute_types(target, attr)[source]

Loop over the the given attribute types to flatten simple definitions.

Notes:
  • xs:pattern is not yet supported reset all native types to xs:string.

  • skip over forward references aka inner classes.

  • Copy all parent extensions if class is enumeration.

Parameters
flatten_attribute_type(target, attr, attr_type)[source]

Flatten attribute type if it’s a simple type otherwise check for circular reference or missing type.

Parameters
add_substitution_attrs(target, attr)[source]

Find all the substitution attributes for the given attribute and add them to the target class.

Exclude enumerations and wildcard attributes.

Parameters
sanitize_classes()[source]

Sanitize the class index objects.

sanitize_class(target)[source]

Sanitize the attributes of the given class. After applying all the flattening handlers the attributes need to be further sanitized to squash common issues like duplicate attribute names.

Steps:
  1. Sanitize inner classes

  2. Sanitize attributes default value

  3. Sanitize attributes name

  4. Sanitize attributes sequential flag

  5. Sanitize duplicate attribute names

Parameters

target (Class) –

sanitize_attribute_default_value(target, attr)[source]

Sanitize attribute default value.

Cases:
  1. List fields can not have a fixed value.

  2. Optional fields or xsi:type can not have a default or fixed value.

  3. Convert string literal default value for enum fields.

Parameters
sanitize_attribute_default_enum(target, attr)[source]

Convert string literal default value for enum fields.

Loop through all attributes types and search for enum sources. If an enum source exist map the default string literal value to a qualified name. Inner enum references are ignored.

Parameters
class_depends_on(source, target, depth=1)[source]

Check if any source dependencies recursively match the target class.

Parameters
Return type

bool