Example #1
0
    def data_type(cls, attrs: Dict, ns_map: Dict) -> DataType:
        """Convert the xsi:type attribute to a DataType, defaults to
        DataType.STRING."""
        xsi_type = cls.xsi_type(attrs, ns_map)
        datatype = DataType.STRING
        if xsi_type:
            datatype = DataType.from_qname(xsi_type) or datatype

        return datatype
Example #2
0
    def is_xsi_type(cls, key: str, value: Any) -> bool:
        """
        Return whether the value is an xsi:type or not based on the given
        attribute name/value.

        :param key: Attribute name
        :param value: Attribute value
        """

        if isinstance(value, str) and value.startswith("{"):
            return key == QNames.XSI_TYPE or DataType.from_qname(value) is not None

        return False
Example #3
0
    def find_types(self, qname: str) -> List[Type[T]]:
        """
        Find all classes that match the given xsi:type qname.

        - Ignores native schema types, xs:string, xs:float, xs:int, ...
        - Rebuild cache if new modules were imported since last run

        :param qname: Qualified name
        """
        if not DataType.from_qname(qname):
            self.build_xsi_cache()
            if qname in self.xsi_cache:
                return self.xsi_cache[qname]

        return []
Example #4
0
    def build_data_type(cls,
                        target: Class,
                        name: str,
                        forward: bool = False) -> AttrType:
        """Create an attribute type for the target class."""
        prefix, suffix = text.split(name)
        namespace = target.ns_map.get(prefix, target.target_namespace)
        qname = build_qname(namespace, suffix)
        datatype = DataType.from_qname(qname)

        return AttrType(
            qname=qname,
            native=datatype is not None,
            forward=forward,
        )
Example #5
0
    def build_node(self, var: XmlVar, attrs: Dict, ns_map: Dict,
                   position: int) -> Optional[XmlNode]:
        if var.is_clazz_union:
            return UnionNode(
                var=var,
                attrs=attrs,
                ns_map=ns_map,
                context=self.context,
                position=position,
            )

        xsi_type = ParserUtils.xsi_type(attrs, ns_map)

        if var.clazz:
            return self.build_element_node(
                var.clazz,
                attrs,
                ns_map,
                position,
                var.derived,
                xsi_type,
            )

        if not var.any_type and not var.wildcard:
            return PrimitiveNode(var, ns_map)

        datatype = DataType.from_qname(xsi_type) if xsi_type else None
        derived = var.derived or var.wildcard
        if datatype:
            return StandardNode(datatype, ns_map, derived, var.nillable)

        node = None
        clazz = None
        if xsi_type:
            clazz = self.context.find_type(xsi_type)

        if clazz:
            node = self.build_element_node(clazz, attrs, ns_map, position,
                                           derived, xsi_type)

        if node:
            return node

        return WildcardNode(var=var,
                            attrs=attrs,
                            ns_map=ns_map,
                            position=position)
Example #6
0
 def datatype(self) -> Optional[DataType]:
     return DataType.from_qname(self.qname) if self.native else None