File: process_mixed_content_class.py

package info (click to toggle)
python-xsdata 24.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,936 kB
  • sloc: python: 29,257; xml: 404; makefile: 27; sh: 6
file content (47 lines) | stat: -rw-r--r-- 1,452 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import sys

from xsdata.codegen.mixins import HandlerInterface
from xsdata.codegen.models import Attr, AttrType, Class, Restrictions
from xsdata.models.enums import DataType, NamespaceType, Tag


class ProcessMixedContentClass(HandlerInterface):
    """
    Mixed content handler.

    If the target class supports mixed content, a new wildcard attr will
    replace the originals except any attributes. All the previous attrs
    derived from  xs:element will be moved as choices for the new
    content attr.
    """

    __slots__ = ()

    def process(self, target: Class):
        if not target.is_mixed:
            return

        attrs = []
        choices = []
        for attr in list(target.attrs):
            if attr.is_attribute:
                attrs.append(attr)
            elif not attr.is_any_type:
                choice = attr.clone()
                choice.restrictions.min_occurs = None
                choice.restrictions.max_occurs = None
                choice.restrictions.sequence = None
                choices.append(choice)

        wildcard = Attr(
            name="content",
            types=[AttrType(qname=str(DataType.ANY_TYPE), native=True)],
            tag=Tag.ANY,
            mixed=True,
            choices=choices,
            namespace=NamespaceType.ANY_NS,
            restrictions=Restrictions(min_occurs=0, max_occurs=sys.maxsize),
        )
        attrs.append(wildcard)

        target.attrs = attrs