Skip to content

# Architecture

The ResourceTransformer is the orchestrator of the code generation procedure.

    graph LR
        A[Load Resources] --> B(Parse transfer objects)
        B --> C[Convert to classes]
        C --> D[Analyze classes]
        D--> E[Write Output]

## Load Resource

The code generator accepts URIs indicating either local or remote file locations.

The resource type (xsd, wsdl, dtd, xml, json) is identified based on the file extension, if present. If the resource lacks an extension, the loader will attempt to locate specific syntax markings associated with the resource type.

If a resource cannot be accessed, a warning is issued, and the program continues its normal flow. In the case of circular imports, resources are loaded only once.

## Parse transfer objects

A resource-specific parser is utilized to bind document information to transfer objects. Additionally, the parsers are responsible for assigning common values required for later analysis, such as locations, a namespace prefix-URI map, and common namespaces like xsi and xlink.

- XSD: SchemaParser
- DTD: DtdParser
- WSDL: DefinitionsParser
- XML: TreeParser
- JSON: [json.loads][]

## Convert to classes

A resource-specific parser is utilized to convert the transfer objects to codegen classes. These mappers encapsulate the pertinent logic detailing how the resource types should be interpreted.

- XSD: SchemaMapper
- DTD: DtdMapper
- WSDL: DefinitionsMapper
- XML: ElementMapper
- JSON: DictMapper

## Analyze classes

    graph LR
        A[Validate classes] --> B(Process classes)
        B --> C[Validate class references]

### Validate Classes

- Remove types with unknown references

    <xs:element name="root" ref="xs:missingOrUnknown"/>

- Remove duplicate types: Keep the last definition

    <xs:element name="root" ref="RootType"/>
    <xs:element name="root" ref="RootType"/>

- Remove duplicate overridden types:

    <xs:override schemaLocation="over005a.xsd">
        <xs:attribute name="code" type="xs:date"/>
    </xs:override>

- Merge redefined types:

    <xs:redefine schemaLocation="schZ006.xsd">
        <xs:group name="GCustomDimProps">
            <xs:sequence>
                <xs:element name="DisplayInfo"  type="xs:unsignedInt"/>
            </xs:sequence>
        </xs:group>
    </xs:redefine>

API: xsdata.codegen.validator.ClassValidator

### Analyze Classes

The classes are wrapped in a ClassContainer instance. It includes some easy finder methods and orchestrates flattening/filtering processes.

The process is divided into multiple steps and handlers per step. All classes have to pass through each step before next one starts. The order of the steps is very important!

### Step: Ungroup

- FlattenAttributeGroups

### Step: Flatten

- CalculateAttributePaths
- FlattenClassExtensions
- SanitizeEnumerationClass
- UpdateAttributesEffectiveChoice
- UnnestInnerClasses
- AddAttributeSubstitutions
- ProcessAttributeTypes
- MergeAttributes
- ProcessMixedContentClass

### Step: Filer

- FilterClasses

### Step: Sanitize

- ResetAttributeSequences
- RenameDuplicateAttributes

### Step: Resolve

- ValidateAttributesOverrides

### Step: Vacuum

- VacuumInnerClasses

### Step: Finalize

- DetectCircularReferences
- CreateCompoundFields
- CreateWrapperFields
- DisambiguateChoices
- SanitizeAttributesDefaultValue
- ResetAttributeSequenceNumbers

### Step: Designate

- RenameDuplicateClasses
- ValidateReferences
- DesignateClassPackages