Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_xsd_component(elem, required=True, strict=True):
declarations_iterator = iter_xsd_declarations(elem)
try:
xsd_declaration = next(declarations_iterator)
except StopIteration:
if required:
raise XMLSchemaParseError("missing declaration", elem)
return None
else:
if not strict:
return xsd_declaration
try:
next(declarations_iterator)
except StopIteration:
return xsd_declaration
else:
raise XMLSchemaParseError("too many declarations", elem)
return XsdGroup(
name=xsd_group.name,
elem=elem,
schema=schema,
model=xsd_group.model,
mixed=mixed,
initlist=list(xsd_group)
)
else:
raise XMLSchemaParseError("missing both attributes 'name' and 'ref'", elem)
elif ref is None:
# Global group
group_name = get_qname(schema.target_namespace, name)
content_model = get_xsd_component(elem)
else:
raise XMLSchemaParseError("found both attributes 'name' and 'ref'", elem)
else:
# Local group (SEQUENCE|ALL|CHOICE)
content_model = elem
group_name = None
check_tag(content_model, XSD_SEQUENCE_TAG, XSD_ALL_TAG, XSD_CHOICE_TAG)
if instance is None:
xsd_group = XsdGroup(
name=group_name,
elem=content_model if elem.tag != XSD_GROUP_TAG else elem,
schema=schema,
model=content_model.tag,
mixed=mixed,
is_global=is_global
)
else:
def get_xsd_int_attribute(elem, attribute, minimum=None, **kwargs):
"""
Get an element's attribute converting it to an int(). Throws an
error if the attribute is not found and the default is None.
Checks the value when a minimum is provided.
:param elem: The Element's instance.
:param attribute: The attribute name.
:param minimum: Optional minimum integer value for the attribute.
:return: Integer containing the attribute value.
"""
value = get_xsd_attribute(elem, attribute, **kwargs)
try:
value = int(value)
except (TypeError, ValueError) as err:
raise XMLSchemaParseError("attribute %r error: %r" % (attribute, str(err)), elem)
else:
if minimum is None or value >= minimum:
return value
else:
raise XMLSchemaParseError(
"attribute %r value must be greater or equal to %r" % (attribute, minimum), elem
)
elif base_type is None:
base_type = simple_type_factory(child, schema, **kwargs)
else:
if isinstance(base_type, XsdComplexType) and base_type.admit_simple_restriction():
base_type = XsdComplexType(
content_type=simple_type_factory(child, schema, **kwargs),
attributes=base_type.attributes,
name=None,
elem=elem,
schema=schema,
derivation=base_type.derivation,
mixed=base_type.mixed
)
has_simple_type_child = True
elif child.tag not in schema.FACETS:
raise XMLSchemaParseError("unexpected tag in restriction", child)
elif child.tag in (XSD_ENUMERATION_TAG, XSD_PATTERN_TAG):
try:
facets[child.tag].append(child)
except KeyError:
if child.tag == XSD_ENUMERATION_TAG:
facets[child.tag] = XsdEnumerationFacet(base_type, child, schema)
else:
facets[child.tag] = XsdPatternsFacet(base_type, child, schema)
elif child.tag not in facets:
facets[child.tag] = XsdUniqueFacet(base_type, child, schema)
else:
raise XMLSchemaParseError("multiple %r constraint facet" % local_name(child.tag), elem)
if base_type is None:
raise XMLSchemaParseError("missing base type in simpleType declaration", elem)
:param attribute: The name of the XML attribute.
:param enumeration: Container with the admitted values for the attribute.
:param kwargs: Optional keyword arguments for a default value or for
an enumeration with admitted values.
:return: The attribute value in a string or the default value.
"""
try:
value = elem.attrib[attribute]
except (KeyError, AttributeError) as err:
try:
return kwargs['default']
except KeyError:
raise XMLSchemaParseError("attribute {} expected".format(err), elem)
else:
if enumeration and value not in enumeration:
raise XMLSchemaParseError("wrong value %r for %r attribute" % (value, attribute), elem)
return value
def iter_xsd_declarations(elem):
"""
Get the node's children are XSD declarations, excluding annotations.
"""
counter = 0
for child in elem:
if child.tag == XSD_ANNOTATION_TAG:
if counter > 0:
raise XMLSchemaParseError("XSD annotation not allowed here!", elem)
else:
yield child
counter += 1
can be checked with a list of admitted values.
:param elem: The Element instance.
:param attribute: The name of the XML attribute.
:param enumeration: Container with the admitted values for the attribute.
:param kwargs: Optional keyword arguments for a default value or for
an enumeration with admitted values.
:return: The attribute value in a string or the default value.
"""
try:
value = elem.attrib[attribute]
except (KeyError, AttributeError) as err:
try:
return kwargs['default']
except KeyError:
raise XMLSchemaParseError("attribute {} expected".format(err), elem)
else:
if enumeration and value not in enumeration:
raise XMLSchemaParseError("wrong value %r for %r attribute" % (value, attribute), elem)
return value
)
has_simple_type_child = True
elif child.tag not in schema.FACETS:
raise XMLSchemaParseError("unexpected tag in restriction", child)
elif child.tag in (XSD_ENUMERATION_TAG, XSD_PATTERN_TAG):
try:
facets[child.tag].append(child)
except KeyError:
if child.tag == XSD_ENUMERATION_TAG:
facets[child.tag] = XsdEnumerationFacet(base_type, child, schema)
else:
facets[child.tag] = XsdPatternsFacet(base_type, child, schema)
elif child.tag not in facets:
facets[child.tag] = XsdUniqueFacet(base_type, child, schema)
else:
raise XMLSchemaParseError("multiple %r constraint facet" % local_name(child.tag), elem)
if base_type is None:
raise XMLSchemaParseError("missing base type in simpleType declaration", elem)
if instance is not None:
instance.elem = elem
instance.schema = schema
instance.facets = facets
return instance
return XsdAtomicRestriction(base_type, elem=elem, schema=schema, facets=facets, is_global=is_global)
Content: (annotation?, ((simpleType | complexType)?, alternative*, (unique | key | keyref)*))
"""
simple_type_factory = kwargs.get('simple_type_factory', xsd_simple_type_factory)
complex_type_factory = kwargs.get('complex_type_factory', xsd_complex_type_factory)
element_type = getattr(instance, 'type', None)
qualified = elem.attrib.get('form', schema.element_form_default) == 'qualified'
# Parse element attributes
try:
element_name, namespace = split_reference(elem.attrib['ref'], schema.namespaces)
except KeyError:
# No 'ref' attribute ==> 'name' attribute required.
if 'name' not in elem.attrib:
raise XMLSchemaParseError("invalid element declaration in XSD schema", elem)
element_name = get_qname(schema.target_namespace, elem.attrib['name'])
ref = False
else:
# Reference to a global element
if is_global:
raise XMLSchemaParseError("an element reference can't be global:", elem)
msg = "attribute '{}' is not allowed when element reference is used!"
if 'name' in elem.attrib:
raise XMLSchemaParseError(msg.format('name'), elem)
elif 'type' in elem.attrib:
raise XMLSchemaParseError(msg.format('type'), elem)
ref = True
xsd_element = schema.maps.lookup_element(element_name, **kwargs)
element_type = xsd_element.type
if instance is not None and instance.name != element_name: