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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
|
#
# Copyright (c), 2016-2020, SISSA (International School for Advanced Studies).
# All rights reserved.
# This file is distributed under the terms of the MIT License.
# See the file 'LICENSE' in the root directory of the present
# distribution, or http://opensource.org/licenses/MIT.
#
# @author Davide Brunato <brunato@sissa.it>
#
from collections.abc import Callable, Iterator, Iterable
from copy import copy
from typing import Any, Optional, Union
from elementpath import SchemaElementNode, build_schema_node_tree
from xmlschema.exceptions import XMLSchemaValueError
from xmlschema.names import XSI_NAMESPACE, XSD_ANY, XSD_ANY_ATTRIBUTE, \
XSD_OPEN_CONTENT, XSD_DEFAULT_OPEN_CONTENT, XSI_TYPE
from xmlschema.aliases import ElementType, SchemaType, SchemaElementType, SchemaAttributeType, \
ModelGroupType, ModelParticleType, AtomicValueType, DecodedValueType, OccursCounterType
from xmlschema.translation import gettext as _
from xmlschema.utils.qnames import get_namespace
from xmlschema.utils.decoding import EmptyType, Empty, raw_encode_value
from xmlschema.xpath import XMLSchemaProxy, ElementPathMixin
from .validation import DecodeContext, EncodeContext, ValidationMixin
from .xsdbase import XsdComponent
from .particles import ParticleMixin
from . import elements
class XsdWildcard(XsdComponent):
process_contents: str
namespace: set[str]
not_namespace: Union[tuple[()], set[str]] = ()
not_qname: Union[tuple[()], set[str]] = ()
# For compatibility with protocol of XSD elements/attributes
type = None
default = None
fixed = None
__slots__ = ('process_contents', 'namespace')
def __repr__(self) -> str:
if self.not_namespace:
return '%s(not_namespace=%r, process_contents=%r)' % (
self.__class__.__name__, sorted(self.not_namespace), self.process_contents
)
else:
return '%s(namespace=%r, process_contents=%r)' % (
self.__class__.__name__, sorted(self.namespace), self.process_contents
)
def __copy__(self) -> 'XsdWildcard':
wildcard: XsdWildcard = object.__new__(self.__class__)
wildcard.__dict__.update(self.__dict__)
for attr in self._mro_slots():
object.__setattr__(wildcard, attr, getattr(self, attr))
wildcard.errors = self.errors.copy()
wildcard.namespace = self.namespace.copy()
if isinstance(self.not_namespace, set):
wildcard.not_namespace = self.not_namespace.copy()
if isinstance(self.not_qname, set):
wildcard.not_qname = self.not_qname.copy()
return wildcard
def _parse(self) -> None:
# Parse namespace and processContents
namespace = self.elem.attrib.get('namespace', '##any').strip()
if namespace in ('##any', '##other'):
self.namespace = {namespace}
elif not namespace:
self.namespace = set() # an empty value means no namespace allowed!
elif namespace == '##local':
self.namespace = {''}
elif namespace == '##targetNamespace':
self.namespace = {self.target_namespace}
else:
self.namespace = set()
for ns in namespace.split():
if ns == '##local':
self.namespace.add('')
elif ns == '##targetNamespace':
self.namespace.add(self.target_namespace)
elif ns.startswith('##'):
msg = _("wrong value %r in 'namespace' attribute")
self.parse_error(msg % ns)
else:
self.namespace.add(ns)
self.process_contents = self.elem.attrib.get('processContents', 'strict')
if self.process_contents not in ('strict', 'lax', 'skip'):
msg = _("wrong value %r for 'processContents' attribute")
self.parse_error(msg % self.process_contents)
self.process_contents = 'strict'
def _parse_not_constraints(self) -> None:
if 'notNamespace' not in self.elem.attrib:
pass
elif 'namespace' in self.elem.attrib:
msg = _("'namespace' and 'notNamespace' attributes are mutually exclusive")
self.parse_error(msg)
else:
self.namespace.clear()
self.not_namespace = set()
for ns in self.elem.attrib['notNamespace'].strip().split():
if ns == '##local':
self.not_namespace.add('')
elif ns == '##targetNamespace':
self.not_namespace.add(self.target_namespace)
elif ns.startswith('##'):
msg = _("wrong value %r in 'notNamespace' attribute")
self.parse_error(msg % ns)
else:
self.not_namespace.add(ns)
# Parse notQName attribute
if 'notQName' not in self.elem.attrib:
return
not_qname = self.elem.attrib['notQName'].strip().split()
if isinstance(self, XsdAnyAttribute) and \
not all(not s.startswith('##') or s == '##defined'
for s in not_qname) or \
not all(not s.startswith('##') or s in {'##defined', '##definedSibling'}
for s in not_qname):
self.parse_error(_("wrong value for 'notQName' attribute"))
return
try:
names = {x if x.startswith('##') else self.schema.resolve_qname(x, False)
for x in not_qname}
except KeyError as err:
msg = _("unmapped QName in 'notQName' attribute: %s")
self.parse_error(msg % str(err))
return
except ValueError as err:
msg = _("wrong QName format in 'notQName' attribute: %s")
self.parse_error(msg % str(err))
return
if self.not_namespace:
if any(not x.startswith('##') for x in names) and \
all(get_namespace(x) in self.not_namespace
for x in names if not x.startswith('##')):
msg = _("the namespace of each QName in notQName is allowed by notNamespace")
self.parse_error(msg)
elif any(not self.is_namespace_allowed(get_namespace(x))
for x in names if not x.startswith('##')):
msg = _("names in notQName must be in namespaces that are allowed")
self.parse_error(msg)
self.not_qname = names
@property
def value_constraint(self) -> Optional[str]:
return None
def is_matching(self, name: Optional[str],
default_namespace: Optional[str] = None,
**kwargs: Any) -> bool:
if name is None:
return False
elif not name or name[0] == '{':
return self.is_namespace_allowed(get_namespace(name))
elif not default_namespace:
return self.is_namespace_allowed('')
else:
return self.is_namespace_allowed(default_namespace)
def is_namespace_allowed(self, namespace: str) -> bool:
if self.not_namespace:
return namespace not in self.not_namespace
elif '##any' in self.namespace or namespace == XSI_NAMESPACE:
return True
elif '##other' in self.namespace:
if not namespace:
return False
return namespace != self.target_namespace
else:
return namespace in self.namespace
def deny_namespaces(self, namespaces: list[str]) -> bool:
if self.not_namespace:
return all(x in self.not_namespace for x in namespaces)
elif '##any' in self.namespace:
return False
elif '##other' in self.namespace:
return all(x == self.target_namespace for x in namespaces)
else:
return all(x not in self.namespace for x in namespaces)
def deny_qnames(self, names: Iterable[str]) -> bool:
if self.not_namespace:
return all(x in self.not_qname or get_namespace(x) in self.not_namespace
for x in names)
elif '##any' in self.namespace:
return all(x in self.not_qname for x in names)
elif '##other' in self.namespace:
return all(x in self.not_qname or get_namespace(x) == self.target_namespace
for x in names)
else:
return all(x in self.not_qname or get_namespace(x) not in self.namespace
for x in names)
def _has_occurs_restriction(self, other: 'XsdWildcard') -> bool:
return True
def is_restriction(self, other: Union[ModelParticleType, 'XsdAnyAttribute'],
check_occurs: bool = True) -> bool:
if not isinstance(other, self.__class__):
return False
elif check_occurs and not self._has_occurs_restriction(other):
return False
assert isinstance(other, XsdWildcard)
if other.process_contents == 'strict' and self.process_contents != 'strict':
return False
elif other.process_contents == 'lax' and self.process_contents == 'skip':
return False
if not self.not_qname and not other.not_qname:
pass
elif '##defined' in other.not_qname and '##defined' not in self.not_qname:
return False
elif '##definedSibling' in other.not_qname and '##definedSibling' not in self.not_qname:
return False
elif other.not_qname:
if not self.deny_qnames(x for x in other.not_qname if not x.startswith('##')):
return False
elif any(not other.is_namespace_allowed(get_namespace(x))
for x in self.not_qname if not x.startswith('##')):
return False
if self.not_namespace:
if other.not_namespace:
return all(ns in self.not_namespace for ns in other.not_namespace)
elif '##any' in other.namespace:
return True
elif '##other' in other.namespace:
return '' in self.not_namespace and other.target_namespace in self.not_namespace
else:
return False
elif other.not_namespace:
if '##any' in self.namespace:
return False
elif '##other' in self.namespace:
return other.not_namespace.issubset(('', other.target_namespace))
else:
return all(ns not in other.not_namespace for ns in self.namespace)
if self.namespace == other.namespace:
return True
elif '##any' in other.namespace:
return True
elif '##any' in self.namespace or '##other' in self.namespace:
return False
elif '##other' in other.namespace:
return other.target_namespace not in self.namespace and '' not in self.namespace
else:
return all(ns in other.namespace for ns in self.namespace)
def union(self, other: Union['XsdAnyElement', 'XsdAnyAttribute']) -> None:
"""Update an XSD wildcard with the union of itself and another XSD wildcard."""
if not self.not_qname:
self.not_qname = copy(other.not_qname)
else:
self.not_qname = {
x for x in self.not_qname
if x in other.not_qname or not other.is_namespace_allowed(get_namespace(x))
}
if self.not_namespace:
if other.not_namespace:
self.not_namespace.intersection_update(other.not_namespace)
elif '##any' in other.namespace:
self.not_namespace = set()
self.namespace.clear()
self.namespace.add('##any')
return
elif '##other' in other.namespace:
self.not_namespace.intersection_update({'', other.target_namespace})
else:
self.not_namespace.difference_update(other.namespace)
if not self.not_namespace:
self.namespace.clear()
self.namespace.add('##any')
return
elif other.not_namespace:
if '##any' in self.namespace:
return
elif '##other' in self.namespace:
self.not_namespace = other.not_namespace & {'', self.target_namespace}
else:
self.not_namespace = other.not_namespace - self.namespace
self.namespace.clear()
if not self.not_namespace:
self.namespace.add('##any')
return
w1: XsdWildcard
w2: XsdWildcard
if not other.namespace or '##any' in self.namespace or self.namespace == other.namespace:
return
elif '##any' in other.namespace:
self.namespace.clear()
self.namespace.add('##any')
return
elif '##other' in other.namespace:
w1, w2 = other, self
elif '##other' in self.namespace:
w1, w2 = self, other
else:
self.namespace.update(other.namespace)
return
if w1.target_namespace in w2.namespace and '' in w2.namespace:
self.namespace.clear()
self.namespace.add('##any')
elif '' not in w2.namespace and w1.target_namespace == w2.target_namespace:
self.namespace.clear()
self.namespace.add('##other')
elif self.xsd_version == '1.0':
msg = _("not expressible wildcard namespace union: {0!r} V {1!r}:")
raise XMLSchemaValueError(msg.format(other.namespace, self.namespace))
else:
self.namespace.clear()
self.not_namespace = {'', w1.target_namespace}
def intersection(self, other: Union['XsdAnyElement', 'XsdAnyAttribute']) -> None:
"""Update an XSD wildcard with the intersection of itself and another XSD wildcard."""
if self.not_qname:
self.not_qname.update(other.not_qname)
else:
self.not_qname = copy(other.not_qname)
if self.not_namespace:
if other.not_namespace:
self.not_namespace.update(other.not_namespace)
elif '##any' in other.namespace:
pass
elif '##other' not in other.namespace:
self.namespace = other.namespace - self.not_namespace
self.not_namespace.clear()
else:
self.not_namespace.add('')
self.not_namespace.add(other.target_namespace)
return
elif other.not_namespace:
if '##any' in self.namespace:
self.not_namespace = other.not_namespace.copy()
self.namespace.clear()
elif '##other' not in self.namespace:
self.namespace.difference_update(other.not_namespace)
else:
self.not_namespace = other.not_namespace.copy()
self.not_namespace.add('')
self.not_namespace.add(other.target_namespace)
self.namespace.clear()
return
if self.namespace == other.namespace:
return
elif '##any' in other.namespace:
return
elif '##any' in self.namespace:
self.namespace.clear()
self.namespace.update(other.namespace)
elif '##other' in self.namespace:
self.namespace.clear()
self.namespace.update(other.namespace)
self.namespace.discard(other.target_namespace)
self.namespace.discard('')
elif '##other' not in other.namespace:
self.namespace.intersection_update(other.namespace)
else:
self.namespace.discard(other.target_namespace)
self.namespace.discard('')
class XsdAnyElement(XsdWildcard, ParticleMixin,
ElementPathMixin[SchemaElementType],
ValidationMixin[ElementType, Any]):
"""
Class for XSD 1.0 *any* wildcards.
.. <any
id = ID
maxOccurs = (nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace|##local)) ) : ##any
processContents = (lax | skip | strict) : strict
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</any>
"""
_ADMITTED_TAGS = XSD_ANY,
precedences: dict[ModelGroupType, list[ModelParticleType]]
copy: Callable[['XsdAnyElement'], 'XsdAnyElement']
__slots__ = ('min_occurs', 'max_occurs', 'skip', 'precedences')
def __init__(self, elem: ElementType, schema: SchemaType, parent: XsdComponent) -> None:
self.min_occurs = self.max_occurs = 1
self.skip = False
self.precedences = {}
super().__init__(elem, schema, parent)
def __repr__(self) -> str:
if self.namespace:
return '%s(namespace=%r, process_contents=%r, occurs=%r)' % (
self.__class__.__name__, sorted(self.namespace),
self.process_contents, list(self.occurs)
)
else:
return '%s(not_namespace=%r, process_contents=%r, occurs=%r)' % (
self.__class__.__name__, sorted(self.not_namespace),
self.process_contents, list(self.occurs)
)
@property
def xpath_proxy(self) -> XMLSchemaProxy:
return XMLSchemaProxy(self.schema, self)
# noinspection PyTypeChecker
@property
def xpath_node(self) -> SchemaElementNode:
schema_node = self.schema.xpath_node
node = schema_node.get_element_node(self)
if isinstance(node, SchemaElementNode):
return node
return build_schema_node_tree(
root=self,
elements=schema_node.elements,
global_elements=schema_node.children,
)
def _parse(self) -> None:
super()._parse()
self._parse_particle(self.elem)
if self.process_contents == 'skip':
self.skip = True
def match(self, name: Optional[str], default_namespace: Optional[str] = None,
resolve: bool = False, **kwargs: Any) -> Optional[SchemaElementType]:
"""
Returns the element wildcard if name is matching the name provided
as argument, `None` otherwise.
:param name: a local or fully-qualified name.
:param default_namespace: used when it's not `None` and not empty for \
completing local name arguments.
:param resolve: when `True` it doesn't return the wildcard but try to \
resolve and return the element matching the name.
:param kwargs: additional options used by XSD 1.1 xs:any wildcards.
"""
if not name or not self.is_matching(name, default_namespace, **kwargs):
return None
elif not resolve:
return self
try:
if name[0] != '{' and default_namespace:
return self.maps.elements[f'{{{default_namespace}}}{name}']
else:
return self.maps.elements[name]
except KeyError:
return None
def __iter__(self) -> Iterator[Any]:
return iter(())
def _has_occurs_restriction(self, other: XsdWildcard) -> bool:
return self.max_occurs == 0 or isinstance(other, XsdAnyElement) and \
self.has_occurs_restriction(other)
def iter(self, tag: Optional[str] = None) -> Iterator[Any]:
return iter(())
def iterchildren(self, tag: Optional[str] = None) -> Iterator[Any]:
return iter(())
@staticmethod
def iter_substitutes() -> Iterator[Any]:
return iter(())
def raw_decode(self, obj: ElementType, validation: str, context: DecodeContext) -> Any:
if not self.is_matching(obj.tag):
reason = _("element {!r} is not allowed here").format(obj)
context.validation_error(validation, self, reason, obj)
if self.process_contents == 'skip' and not context.process_skipped:
return Empty
namespace = get_namespace(obj.tag)
if not self.maps.loader.load_namespace(namespace):
reason = _("unavailable namespace {!r}").format(namespace)
else:
try:
xsd_element = self.maps.elements[obj.tag]
except KeyError:
reason = f"element {obj.tag!r} not found"
else:
return xsd_element.raw_decode(obj, validation, context)
if XSI_TYPE in obj.attrib:
if self.process_contents == 'strict':
xsd_element = self.builders.create_element(
obj.tag, self.maps.validator, parent=self, form='unqualified'
)
else:
xsd_element = self.builders.create_element(
obj.tag, self.maps.validator, self,
nillable='true', form='unqualified'
)
return xsd_element.raw_decode(obj, validation, context)
if validation != 'skip' and self.process_contents == 'strict':
context.validation_error(validation, self, reason, obj)
xsd_element = self.builders.create_element(
obj.tag, self.maps.validator, parent=self, form='unqualified'
)
return xsd_element.raw_decode(obj, validation, context)
def raw_encode(self, obj: tuple[str, ElementType], validation: str,
context: EncodeContext) -> Any:
name, value = obj
namespace = get_namespace(name)
if not self.is_namespace_allowed(namespace):
reason = _("element {!r} is not allowed here").format(name)
context.validation_error(validation, self, reason, value)
if self.process_contents == 'skip' and not context.process_skipped:
return Empty
if not self.maps.loader.load_namespace(namespace):
reason = _("unavailable namespace {!r}").format(namespace)
else:
try:
xsd_element = self.maps.elements[name]
except KeyError:
reason = f"element {name!r} not found"
else:
return xsd_element.raw_encode(value, validation, context)
# Check if there is a xsi:type attribute, but it has to extract
# attributes using the converter instance.
if self.process_contents == 'strict':
xsd_element = self.builders.create_element(
name, self.maps.validator, parent=self, form='unqualified'
)
else:
xsd_element = self.builders.create_element(
name, self.maps.validator, parent=self, nillable='true', form='unqualified'
)
try:
element_data = context.converter.element_encode(value, xsd_element)
except (ValueError, TypeError) as err:
if validation != 'skip' and self.process_contents == 'strict':
context.validation_error(validation, self, err, value)
else:
if XSI_TYPE in element_data.attributes:
return xsd_element.raw_encode(value, validation, context)
if validation != 'skip' and self.process_contents == 'strict':
context.validation_error(validation, self, reason)
return self.any_type.raw_encode(obj, validation, context)
def is_overlap(self, other: ModelParticleType) -> bool:
if not isinstance(other, XsdAnyElement):
if isinstance(other, elements.XsdElement):
return other.is_overlap(self)
return False
if self.not_namespace:
if other.not_namespace:
return True
elif '##any' in other.namespace:
return True
elif '##other' in other.namespace:
return True
else:
return any(ns not in self.not_namespace for ns in other.namespace)
elif other.not_namespace:
if '##any' in self.namespace:
return True
elif '##other' in self.namespace:
return True
else:
return any(ns not in other.not_namespace for ns in self.namespace)
elif self.namespace == other.namespace:
return True
elif '##any' in self.namespace or '##any' in other.namespace:
return True
elif '##other' in self.namespace:
return any(ns and ns != self.target_namespace for ns in other.namespace)
elif '##other' in other.namespace:
return any(ns and ns != other.target_namespace for ns in self.namespace)
else:
return any(ns in self.namespace for ns in other.namespace)
def is_consistent(self, other: SchemaElementType, **kwargs: Any) -> bool:
return True
class XsdAnyAttribute(XsdWildcard, ValidationMixin[tuple[str, str], DecodedValueType]):
"""
Class for XSD 1.0 *anyAttribute* wildcards.
.. <anyAttribute
id = ID
namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) )
processContents = (lax | skip | strict) : strict
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</anyAttribute>
"""
copy: Callable[['XsdAnyAttribute'], 'XsdAnyAttribute']
_ADMITTED_TAGS = XSD_ANY_ATTRIBUTE,
# Added for compatibility with protocol of XSD attributes
use = None
inheritable = False # XSD 1.1 attributes
def match(self, name: Optional[str], default_namespace: Optional[str] = None,
resolve: bool = False, **kwargs: Any) -> Optional[SchemaAttributeType]:
"""
Returns the attribute wildcard if name is matching the name provided
as argument, `None` otherwise.
:param name: a local or fully-qualified name.
:param default_namespace: used when it's not `None` and not empty for \
completing local name arguments.
:param resolve: when `True` it doesn't return the wildcard but try to \
resolve and return the attribute matching the name.
:param kwargs: additional options that can be used by certain components.
"""
if not name or not self.is_matching(name, default_namespace, **kwargs):
return None
elif not resolve:
return self
try:
if name[0] != '{' and default_namespace:
return self.maps.attributes[f'{{{default_namespace}}}{name}']
else:
return self.maps.attributes[name]
except KeyError:
return None
def raw_decode(self, obj: tuple[str, str], validation: str,
context: DecodeContext) -> Union[DecodedValueType, EmptyType]:
name, value = obj
if not self.is_matching(name):
reason = _("attribute %r not allowed") % name
context.validation_error(validation, self, reason, obj)
if self.process_contents == 'skip' and not context.process_skipped:
return Empty
namespace = get_namespace(name)
if self.maps.loader.load_namespace(namespace):
try:
xsd_attribute = self.maps.attributes[name]
except KeyError:
if validation != 'skip' and self.process_contents == 'strict':
reason = _("attribute %r not found") % name
context.validation_error(validation, self, reason, obj)
else:
return xsd_attribute.raw_decode(value, validation, context)
elif validation != 'skip' and self.process_contents == 'strict':
reason = _("unavailable namespace {!r}").format(get_namespace(name))
context.validation_error(validation, self, reason)
return value
def raw_encode(self, obj: tuple[str, AtomicValueType], validation: str,
context: EncodeContext) -> Union[str, None, EmptyType]:
name, value = obj
namespace = get_namespace(name)
if not self.is_namespace_allowed(namespace):
reason = _("attribute %r not allowed") % name
context.validation_error(validation, self, reason, obj)
if self.process_contents == 'skip' and not context.process_skipped:
return Empty
if self.maps.loader.load_namespace(namespace):
try:
xsd_attribute = self.maps.attributes[name]
except KeyError:
if validation != 'skip' and self.process_contents == 'strict':
reason = _("attribute %r not found") % name
context.validation_error(validation, self, reason, obj)
else:
return xsd_attribute.raw_encode(value, validation, context)
elif validation != 'skip' and self.process_contents == 'strict':
reason = _("unavailable namespace {!r}").format(namespace)
context.validation_error(validation, self, reason)
return raw_encode_value(value)
class Xsd11AnyElement(XsdAnyElement):
"""
Class for XSD 1.1 *any* declarations.
.. <any
id = ID
maxOccurs = (nonNegativeInteger | unbounded) : 1
minOccurs = nonNegativeInteger : 1
namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) )
notNamespace = List of (anyURI | (##targetNamespace | ##local))
notQName = List of (QName | (##defined | ##definedSibling))
processContents = (lax | skip | strict) : strict
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</any>
"""
def _parse(self) -> None:
super()._parse()
self._parse_not_constraints()
def is_matching(self, name: Optional[str],
default_namespace: Optional[str] = None,
group: Optional[ModelGroupType] = None,
occurs: Optional[OccursCounterType] = None,
**kwargs: Any) -> bool:
"""
Returns `True` if the component name is matching the name provided as argument,
`False` otherwise.
:param name: a local or fully-qualified name.
:param default_namespace: used by the XPath processor for completing \
the name argument in case it's a local name.
:param group: used only by XSD 1.1 any element wildcards to verify siblings in \
case of ##definedSibling value in notQName attribute.
:param occurs: a Counter instance for verify model occurrences counting.
"""
if name is None:
return False
elif not name or name[0] == '{':
if not self.is_namespace_allowed(get_namespace(name)):
return False
elif not default_namespace:
if not self.is_namespace_allowed(''):
return False
else:
name = f'{{{default_namespace}}}{name}'
if not self.is_namespace_allowed(default_namespace):
return False
if group in self.precedences:
if occurs is None:
if any(e.is_matching(name) for e in self.precedences[group]):
return False
elif any(e.is_matching(name) and not e.is_over(occurs)
for e in self.precedences[group]):
return False
if '##defined' in self.not_qname and name in self.maps.elements:
return False
if group and '##definedSibling' in self.not_qname:
if any(e.is_matching(name) for e in group.iter_elements()
if not isinstance(e, XsdAnyElement)):
return False
return name not in self.not_qname
def is_consistent(self, other: SchemaElementType, **kwargs: Any) -> bool:
if isinstance(other, XsdAnyElement) or self.process_contents == 'skip':
return True
xsd_element = self.match(other.name, other.default_namespace, resolve=True)
return xsd_element is None or other.is_consistent(xsd_element, strict=False)
def add_precedence(self, other: ModelParticleType, group: ModelGroupType) -> None:
try:
self.precedences[group].append(other)
except KeyError:
self.precedences[group] = [other]
class Xsd11AnyAttribute(XsdAnyAttribute):
"""
Class for XSD 1.1 *anyAttribute* declarations.
.. <anyAttribute
id = ID
namespace = ((##any | ##other) | List of (anyURI | (##targetNamespace | ##local)) )
notNamespace = List of (anyURI | (##targetNamespace | ##local))
notQName = List of (QName | ##defined)
processContents = (lax | skip | strict) : strict
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</anyAttribute>
"""
def _parse(self) -> None:
super()._parse()
self._parse_not_constraints()
def is_matching(self, name: Optional[str],
default_namespace: Optional[str] = None,
**kwargs: Any) -> bool:
if name is None:
return False
elif not name or name[0] == '{':
namespace = get_namespace(name)
elif not default_namespace:
namespace = ''
else:
name = f'{{{default_namespace}}}{name}'
namespace = default_namespace
if '##defined' in self.not_qname and name in self.maps.attributes:
xsd_attribute = self.maps.attributes[name]
if isinstance(xsd_attribute, tuple):
if xsd_attribute[1] is self.schema:
return False
elif xsd_attribute.schema is self.schema:
return False
return name not in self.not_qname and self.is_namespace_allowed(namespace)
class XsdOpenContent(XsdComponent):
"""
Class for XSD 1.1 *openContent* model definitions.
.. <openContent
id = ID
mode = (none | interleave | suffix) : interleave
{any attributes with non-schema namespace . . .}>
Content: (annotation?), (any?)
</openContent>
"""
_ADMITTED_TAGS = XSD_OPEN_CONTENT,
mode = 'interleave'
any_element = None # type: Xsd11AnyElement
def __init__(self, elem: ElementType, schema: SchemaType, parent: XsdComponent) -> None:
super().__init__(elem, schema, parent)
def __repr__(self) -> str:
return '%s(mode=%r)' % (self.__class__.__name__, self.mode)
def _parse(self) -> None:
try:
self.mode = self.elem.attrib['mode']
except KeyError:
pass
else:
if self.mode not in ('none', 'interleave', 'suffix'):
msg = _("wrong value %r for 'mode' attribute")
self.parse_error(msg % self.mode)
child = self._parse_child_component(self.elem)
if self.mode == 'none':
if child is not None and child.tag == XSD_ANY:
msg = _("an openContent with mode='none' cannot "
"have an <xs:any> child declaration")
self.parse_error(msg)
elif child is None or child.tag != XSD_ANY:
self.parse_error(_("an <xs:any> child declaration is required"))
else:
self.any_element = Xsd11AnyElement(child, self.schema, self)
def is_restriction(self, other: 'XsdOpenContent') -> bool:
if other is None or other.mode == 'none':
return self.mode == 'none'
elif self.mode == 'interleave' and other.mode == 'suffix':
return False
else:
return self.any_element.is_restriction(other.any_element)
class XsdDefaultOpenContent(XsdOpenContent):
"""
Class for XSD 1.1 *defaultOpenContent* model definitions.
.. <defaultOpenContent
appliesToEmpty = boolean : false
id = ID
mode = (interleave | suffix) : interleave
{any attributes with non-schema namespace . . .}>
Content: (annotation?, any)
</defaultOpenContent>
"""
_ADMITTED_TAGS = XSD_DEFAULT_OPEN_CONTENT,
applies_to_empty = False
def __init__(self, elem: ElementType, schema: SchemaType) -> None:
super(XsdOpenContent, self).__init__(elem, schema)
def _parse(self) -> None:
super()._parse()
if self.parent is not None:
msg = _("defaultOpenContent must be a child of the schema")
self.parse_error(msg)
if self.mode == 'none':
msg = _("the attribute 'mode' of a defaultOpenContent cannot be 'none'")
self.parse_error(msg)
if self._parse_child_component(self.elem) is None:
msg = _("a defaultOpenContent declaration cannot be empty")
self.parse_error(msg)
if 'appliesToEmpty' in self.elem.attrib:
if self.elem.attrib['appliesToEmpty'].strip() in ('true', '1'):
self.applies_to_empty = True
|