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
|
#
# Copyright (c), 2016-2021, 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>
#
"""
This module contains declarations and classes for XML Schema constraint facets.
"""
import re
import math
import operator
from abc import abstractmethod
from typing import TYPE_CHECKING, cast, Any, List, Optional, Pattern, Union, \
MutableSequence, overload, Tuple
from elementpath import XPath2Parser, XPathContext, ElementPathError, \
translate_pattern, RegexError
from ..names import XSD_LENGTH, XSD_MIN_LENGTH, XSD_MAX_LENGTH, XSD_ENUMERATION, \
XSD_INTEGER, XSD_WHITE_SPACE, XSD_PATTERN, XSD_MAX_INCLUSIVE, XSD_MAX_EXCLUSIVE, \
XSD_MIN_INCLUSIVE, XSD_MIN_EXCLUSIVE, XSD_TOTAL_DIGITS, XSD_FRACTION_DIGITS, \
XSD_ASSERTION, XSD_DECIMAL, XSD_EXPLICIT_TIMEZONE, XSD_NOTATION_TYPE, XSD_QNAME, \
XSD_ANNOTATION
from ..etree import etree_element
from ..aliases import ElementType, SchemaType, AtomicValueType, BaseXsdType
from ..helpers import count_digits, local_name
from .exceptions import XMLSchemaValidationError, XMLSchemaDecodeError
from .xsdbase import XsdComponent, XsdAnnotation
if TYPE_CHECKING:
from .simple_types import XsdList, XsdAtomicRestriction
LaxDecodeType = Tuple[Any, List[XMLSchemaValidationError]]
class XsdFacet(XsdComponent):
"""
XML Schema constraining facets base class.
"""
value: Optional[AtomicValueType]
base_type: Optional[BaseXsdType]
base_value: Optional[AtomicValueType]
fixed = False
def __init__(self, elem: ElementType,
schema: SchemaType,
parent: Union['XsdList', 'XsdAtomicRestriction'],
base_type: Optional[BaseXsdType]) -> None:
self.base_type = base_type
super(XsdFacet, self).__init__(elem, schema, parent)
def __repr__(self) -> str:
return '%s(value=%r, fixed=%r)' % (self.__class__.__name__, self.value, self.fixed)
def __call__(self, value: Any) -> None:
try:
self._validator(value)
except TypeError:
reason = "invalid type {!r} provided".format(type(value))
raise XMLSchemaValidationError(self, value, reason) from None
@staticmethod
def _validator(_: Any) -> None:
return
def _parse(self) -> None:
if 'fixed' in self.elem.attrib and self.elem.attrib['fixed'] in ('true', '1'):
self.fixed = True
base_facet = self.base_facet
self.base_value = None if base_facet is None else base_facet.value
try:
self._parse_value(self.elem)
except (KeyError, ValueError, XMLSchemaDecodeError) as err:
self.value = None
self.parse_error(err)
else:
if base_facet is not None and base_facet.fixed and \
base_facet.value is not None and self.value != base_facet.value:
self.parse_error("{!r} facet value is fixed to {!r}"
.format(local_name(self.elem.tag), base_facet.value))
def _parse_value(self, elem: ElementType) -> Union[None, AtomicValueType, Pattern[str]]:
self.value = elem.attrib['value'] # pragma: no cover
return None
@property
def built(self) -> bool:
return True # pragma: no cover
@property
def base_facet(self) -> Optional['XsdFacet']:
"""
An object of the same type if the instance has a base facet, `None` otherwise.
"""
base_type: Optional[BaseXsdType] = self.base_type
tag = self.elem.tag
while True:
if base_type is None:
return None
try:
base_facet = base_type.facets[tag] # type: ignore[union-attr]
except (AttributeError, KeyError):
base_type = base_type.base_type
else:
assert isinstance(base_facet, self.__class__)
return base_facet
class XsdWhiteSpaceFacet(XsdFacet):
"""
XSD *whiteSpace* facet.
.. <whiteSpace
fixed = boolean : false
id = ID
value = (collapse | preserve | replace)
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</whiteSpace>
"""
value: str
_ADMITTED_TAGS = XSD_WHITE_SPACE,
def _parse_value(self, elem: ElementType) -> None:
self.value = elem.attrib['value']
if self.value == 'collapse':
self._validator = self.collapse_white_space_validator # type: ignore[assignment]
elif self.value == 'replace':
if self.base_value == 'collapse':
self.parse_error("facet value can be only 'collapse'")
self._validator = self.replace_white_space_validator # type: ignore[assignment]
elif self.base_value == 'collapse':
self.parse_error("facet value can be only 'collapse'")
elif self.base_value == 'replace':
self.parse_error("facet value can be only 'replace' or 'collapse'")
def replace_white_space_validator(self, value: str) -> None:
if '\t' in value or '\n' in value:
raise XMLSchemaValidationError(
self, value, "value contains tabs or newlines"
)
def collapse_white_space_validator(self, value: str) -> None:
if '\t' in value or '\n' in value or ' ' in value:
raise XMLSchemaValidationError(
self, value, "value contains non collapsed white spaces"
)
class XsdLengthFacet(XsdFacet):
"""
XSD *length* facet.
.. <length
fixed = boolean : false
id = ID
value = nonNegativeInteger
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</length>
"""
value: int
base_type: BaseXsdType
base_value: Optional[int]
_ADMITTED_TAGS = XSD_LENGTH,
def _parse_value(self, elem: ElementType) -> None:
self.value = int(elem.attrib['value'])
if self.base_value is not None and self.value != self.base_value:
self.parse_error("base facet has a different length ({})".format(self.base_value))
primitive_type = getattr(self.base_type, 'primitive_type', None)
if primitive_type is None or primitive_type.name not in {XSD_QNAME, XSD_NOTATION_TYPE}:
# See: https://www.w3.org/Bugs/Public/show_bug.cgi?id=4009
self._validator = self._length_validator # type: ignore[assignment]
def _length_validator(self, value: Any) -> None:
if len(value) != self.value:
reason = "length has to be {!r}".format(self.value)
raise XMLSchemaValidationError(self, value, reason)
class XsdMinLengthFacet(XsdFacet):
"""
XSD *minLength* facet.
.. <minLength
fixed = boolean : false
id = ID
value = nonNegativeInteger
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</minLength>
"""
value: int
base_type: BaseXsdType
base_value: Optional[int]
_ADMITTED_TAGS = XSD_MIN_LENGTH,
def _parse_value(self, elem: ElementType) -> None:
self.value = int(elem.attrib['value'])
if self.base_value is not None and self.value < self.base_value:
self.parse_error("base facet has a greater min length ({})".format(self.base_value))
primitive_type = getattr(self.base_type, 'primitive_type', None)
if primitive_type is None or primitive_type.name not in {XSD_QNAME, XSD_NOTATION_TYPE}:
# See: https://www.w3.org/Bugs/Public/show_bug.cgi?id=4009
self._validator = self._min_length_validator # type: ignore[assignment]
def _min_length_validator(self, value: Any) -> None:
if len(value) < self.value:
reason = "value length cannot be lesser than {!r}".format(self.value)
raise XMLSchemaValidationError(self, value, reason)
class XsdMaxLengthFacet(XsdFacet):
"""
XSD *maxLength* facet.
.. <maxLength
fixed = boolean : false
id = ID
value = nonNegativeInteger
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</maxLength>
"""
value: int
base_type: BaseXsdType
base_value: Optional[int]
_ADMITTED_TAGS = XSD_MAX_LENGTH,
def _parse_value(self, elem: ElementType) -> None:
self.value = int(elem.attrib['value'])
if self.base_value is not None and self.value > self.base_value:
self.parse_error("base type has a lesser max length ({})".format(self.base_value))
primitive_type = getattr(self.base_type, 'primitive_type', None)
if primitive_type is None or primitive_type.name not in {XSD_QNAME, XSD_NOTATION_TYPE}:
# See: https://www.w3.org/Bugs/Public/show_bug.cgi?id=4009
self._validator = self._max_length_validator # type: ignore[assignment]
def _max_length_validator(self, value: Any) -> None:
if len(value) > self.value:
reason = "value length cannot be greater than {!r}".format(self.value)
raise XMLSchemaValidationError(self, value, reason)
class XsdMinInclusiveFacet(XsdFacet):
"""
XSD *minInclusive* facet.
.. <minInclusive
fixed = boolean : false
id = ID
value = anySimpleType
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</minInclusive>
"""
base_type: BaseXsdType
_ADMITTED_TAGS = XSD_MIN_INCLUSIVE,
def _parse_value(self, elem: ElementType) -> None:
value = elem.attrib['value']
self.value, errors = cast(LaxDecodeType, self.base_type.decode(value, 'lax'))
for e in errors:
self.parse_error("invalid restriction: {}".format(e.reason))
def __call__(self, value: Any) -> None:
try:
if value < self.value:
reason = "value has to be greater or equal than {!r}".format(self.value)
raise XMLSchemaValidationError(self, value, reason)
except TypeError as err:
raise XMLSchemaValidationError(self, value, str(err)) from None
class XsdMinExclusiveFacet(XsdFacet):
"""
XSD *minExclusive* facet.
.. <minExclusive
fixed = boolean : false
id = ID
value = anySimpleType
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</minExclusive>
"""
base_type: BaseXsdType
_ADMITTED_TAGS = XSD_MIN_EXCLUSIVE,
def _parse_value(self, elem: ElementType) -> None:
value = elem.attrib['value']
self.value, errors = cast(LaxDecodeType, self.base_type.decode(value, 'lax'))
for e in errors:
if not isinstance(e.validator, self.__class__) or e.validator.value != self.value:
self.parse_error("invalid restriction: {}".format(e.reason))
facet: Any = self.base_type.get_facet(XSD_MAX_INCLUSIVE)
if facet is not None and facet.value == self.value:
self.parse_error("invalid restriction: {} is also the maximum".format(self.value))
def __call__(self, value: Any) -> None:
try:
if value <= self.value:
reason = "value has to be greater than {!r}".format(self.value)
raise XMLSchemaValidationError(self, value, reason)
except TypeError as err:
raise XMLSchemaValidationError(self, value, str(err)) from None
class XsdMaxInclusiveFacet(XsdFacet):
"""
XSD *maxInclusive* facet.
.. <maxInclusive
fixed = boolean : false
id = ID
value = anySimpleType
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</maxInclusive>
"""
base_type: BaseXsdType
_ADMITTED_TAGS = XSD_MAX_INCLUSIVE,
def _parse_value(self, elem: ElementType) -> None:
value = elem.attrib['value']
self.value, errors = cast(LaxDecodeType, self.base_type.decode(value, 'lax'))
for e in errors:
self.parse_error("invalid restriction: {}".format(e.reason))
def __call__(self, value: Any) -> None:
try:
if value > self.value:
reason = "value has to be lesser or equal than {!r}".format(self.value)
raise XMLSchemaValidationError(self, value, reason)
except TypeError as err:
raise XMLSchemaValidationError(self, value, str(err)) from None
class XsdMaxExclusiveFacet(XsdFacet):
"""
XSD *maxExclusive* facet.
.. <maxExclusive
fixed = boolean : false
id = ID
value = anySimpleType
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</maxExclusive>
"""
base_type: BaseXsdType
_ADMITTED_TAGS = XSD_MAX_EXCLUSIVE,
def _parse_value(self, elem: ElementType) -> None:
value = elem.attrib['value']
self.value, errors = cast(LaxDecodeType, self.base_type.decode(value, 'lax'))
for e in errors:
if not isinstance(e.validator, self.__class__) or e.validator.value != self.value:
self.parse_error("invalid restriction: {}".format(e.reason))
facet: Any = self.base_type.get_facet(XSD_MIN_INCLUSIVE)
if facet is not None and facet.value == self.value:
self.parse_error("invalid restriction: {} is also the minimum".format(self.value))
def __call__(self, value: Any) -> None:
try:
if value >= self.value:
reason = "value has to be lesser than {!r}".format(self.value)
raise XMLSchemaValidationError(self, value, reason)
except TypeError as err:
raise XMLSchemaValidationError(self, value, str(err)) from None
class XsdTotalDigitsFacet(XsdFacet):
"""
XSD *totalDigits* facet.
.. <totalDigits
fixed = boolean : false
id = ID
value = positiveInteger
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</totalDigits>
"""
value: int
base_type: BaseXsdType
_ADMITTED_TAGS = XSD_TOTAL_DIGITS,
def _parse_value(self, elem: ElementType) -> None:
# Errors are detected by meta-schema validation. For schemas with
# 'lax' validation mode use 9999 in case of an invalid value.
try:
self.value = int(elem.attrib['value'])
except (ValueError, KeyError):
self.value = 9999
else:
if self.value < 1:
self.value = 9999
facet: Any = self.base_type.get_facet(XSD_TOTAL_DIGITS)
if facet is not None and facet.value < self.value:
self.parse_error(
"invalid restriction: base value is lower ({})".format(facet.value)
)
def __call__(self, value: Any) -> None:
try:
if operator.add(*count_digits(value)) <= self.value:
return
except (TypeError, ValueError, ArithmeticError) as err:
raise XMLSchemaValidationError(self, value, str(err)) from None
else:
reason = "the number of digits has to be lesser or equal " \
"than {!r}".format(self.value)
raise XMLSchemaValidationError(self, value, reason)
class XsdFractionDigitsFacet(XsdFacet):
"""
XSD *fractionDigits* facet.
.. <fractionDigits
fixed = boolean : false
id = ID
value = nonNegativeInteger
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</fractionDigits>
"""
value: int
base_type: BaseXsdType
_ADMITTED_TAGS = XSD_FRACTION_DIGITS,
def __init__(self, elem: ElementType,
schema: SchemaType,
parent: 'XsdAtomicRestriction',
base_type: BaseXsdType) -> None:
super(XsdFractionDigitsFacet, self).__init__(elem, schema, parent, base_type)
if not base_type.is_derived(self.maps.types[XSD_DECIMAL]):
self.parse_error(
"fractionDigits facet can be applied only to types derived from xs:decimal"
)
def _parse_value(self, elem: ElementType) -> None:
# Errors are detected by meta-schema validation. For schemas with
# 'lax' validation mode use 9999 in case of an invalid value.
try:
self.value = int(elem.attrib['value'])
except (ValueError, KeyError):
self.value = 9999
else:
if self.value < 0:
self.value = 9999
elif self.value > 0 and self.base_type.is_derived(self.maps.types[XSD_INTEGER]):
raise ValueError("fractionDigits facet value has to be 0 "
"for types derived from xs:integer.")
facet: Any = self.base_type.get_facet(XSD_FRACTION_DIGITS)
if facet is not None and facet.value < self.value:
self.parse_error(
"invalid restriction: base value is lower ({})".format(facet.value)
)
def __call__(self, value: Any) -> None:
try:
if count_digits(value)[1] <= self.value:
return
except (TypeError, ValueError, ArithmeticError) as err:
raise XMLSchemaValidationError(self, value, str(err)) from None
else:
reason = "the number of fraction digits has to be lesser " \
"or equal than {!r}".format(self.value)
raise XMLSchemaValidationError(self, value, reason)
class XsdExplicitTimezoneFacet(XsdFacet):
"""
XSD 1.1 *explicitTimezone* facet.
.. <explicitTimezone
fixed = boolean : false
id = ID
value = NCName
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</explicitTimezone>
"""
value: str
base_type: BaseXsdType
_ADMITTED_TAGS = XSD_EXPLICIT_TIMEZONE,
def _parse_value(self, elem: ElementType) -> None:
self.value = elem.attrib['value']
if self.value == 'prohibited':
self._validator = self._prohibited_timezone_validator # type: ignore[assignment]
elif self.value == 'required':
self._validator = self._required_timezone_validator # type: ignore[assignment]
elif self.value != 'optional':
self.value = 'optional' # Error already detected by meta-schema validation
facet: Any = self.base_type.get_facet(XSD_EXPLICIT_TIMEZONE)
if facet is not None and facet.value != self.value and facet.value != 'optional':
self.parse_error("invalid restriction from {!r}".format(facet.value))
def _required_timezone_validator(self, value: Any) -> None:
if value.tzinfo is None:
raise XMLSchemaValidationError(
self, value, "time zone required for value {!r}".format(self.value)
)
def _prohibited_timezone_validator(self, value: Any) -> None:
if value.tzinfo is not None:
raise XMLSchemaValidationError(
self, value, "time zone prohibited for value {!r}".format(self.value)
)
class XsdEnumerationFacets(MutableSequence[ElementType], XsdFacet):
"""
Sequence of XSD *enumeration* facets. Values are validates if match any of enumeration values.
.. <enumeration
id = ID
value = anySimpleType
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</enumeration>
"""
base_type: BaseXsdType
_ADMITTED_TAGS = {XSD_ENUMERATION}
def __init__(self, elem: ElementType,
schema: SchemaType,
parent: 'XsdAtomicRestriction',
base_type: BaseXsdType) -> None:
XsdFacet.__init__(self, elem, schema, parent, base_type)
def _parse(self) -> None:
self._elements = [self.elem]
self.enumeration = [self._parse_value(self.elem)]
def _parse_value(self, elem: ElementType) -> Optional[AtomicValueType]:
try:
value = self.base_type.decode(elem.attrib['value'], namespaces=self.schema.namespaces)
except KeyError:
pass # pragma: no cover (already detected by meta-schema validation)
except XMLSchemaValidationError as err:
self.parse_error(err, elem)
else:
if self.base_type.name == XSD_NOTATION_TYPE:
assert isinstance(value, str)
try:
notation_qname = self.schema.resolve_qname(value)
except (KeyError, ValueError, RuntimeError) as err:
self.parse_error(err, elem)
else:
if notation_qname not in self.maps.notations:
msg = "value {!r} must match a notation declaration"
self.parse_error(msg.format(value), elem)
return cast(AtomicValueType, value)
return None
@overload
@abstractmethod
def __getitem__(self, i: int) -> ElementType: ...
@overload
@abstractmethod
def __getitem__(self, s: slice) -> MutableSequence[ElementType]: ...
def __getitem__(self, i: Union[int, slice]) \
-> Union[ElementType, MutableSequence[ElementType]]:
return self._elements[i]
def __setitem__(self, i: Union[int, slice], o: Any) -> None:
self._elements[i] = o
if isinstance(i, int):
self.enumeration[i] = self._parse_value(o)
else:
self.enumeration[i] = [self._parse_value(e) for e in o]
def __delitem__(self, i: Union[int, slice]) -> None:
del self._elements[i]
del self.enumeration[i]
def __len__(self) -> int:
return len(self._elements)
def insert(self, i: int, elem: ElementType) -> None:
self._elements.insert(i, elem)
self.enumeration.insert(i, self._parse_value(elem))
def __repr__(self) -> str:
if len(self.enumeration) > 5:
return '%s(%s)' % (
self.__class__.__name__, '[%s, ...]' % ', '.join(map(repr, self.enumeration[:5]))
)
else:
return '%s(%r)' % (self.__class__.__name__, self.enumeration)
def __call__(self, value: Any) -> None:
if value in self.enumeration:
return
try:
if math.isnan(value):
if any(math.isnan(x) for x in self.enumeration): # type: ignore[arg-type]
return
elif math.isinf(value):
if any(math.isinf(x) and str(value) == str(x) # type: ignore[arg-type]
for x in self.enumeration): # pragma: no cover
return
except TypeError:
pass
reason = "value must be one of {!r}".format(self.enumeration)
raise XMLSchemaValidationError(self, value, reason)
def get_annotation(self, i: int) -> Optional[XsdAnnotation]:
"""
Get the XSD annotation of the i-th enumeration facet.
:param i: an integer index.
:returns: an XsdAnnotation object or `None`.
"""
for child in self._elements[i]:
if child.tag == XSD_ANNOTATION:
return XsdAnnotation(child, self.schema, self)
return None
class XsdPatternFacets(MutableSequence[ElementType], XsdFacet):
"""
Sequence of XSD *pattern* facets. Values are validates if match any of patterns.
.. <pattern
id = ID
value = string
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</pattern>
"""
_ADMITTED_TAGS = {XSD_PATTERN}
patterns: List[Pattern[str]]
def __init__(self, elem: ElementType,
schema: SchemaType,
parent: 'XsdAtomicRestriction',
base_type: Optional[BaseXsdType]) -> None:
XsdFacet.__init__(self, elem, schema, parent, base_type)
def _parse(self) -> None:
self._elements = [self.elem]
self.patterns = [self._parse_value(self.elem)]
def _parse_value(self, elem: ElementType) -> Pattern[str]:
try:
python_pattern = translate_pattern(
pattern=elem.attrib['value'],
xsd_version=self.xsd_version,
back_references=False,
lazy_quantifiers=False,
anchors=False
)
return re.compile(python_pattern)
except KeyError:
return re.compile(r'^.*$')
except (RegexError, re.error, XMLSchemaDecodeError) as err:
self.parse_error(str(err), elem)
return re.compile(r'^.*$')
@overload
@abstractmethod
def __getitem__(self, i: int) -> ElementType: ...
@overload
@abstractmethod
def __getitem__(self, s: slice) -> MutableSequence[ElementType]: ...
def __getitem__(self, i: Union[int, slice]) \
-> Union[ElementType, MutableSequence[ElementType]]:
return self._elements[i]
def __setitem__(self, i: Union[int, slice], o: Any) -> None:
self._elements[i] = o
if isinstance(i, int):
self.patterns[i] = self._parse_value(o)
else:
self.patterns[i] = [self._parse_value(e) for e in o]
def __delitem__(self, i: Union[int, slice]) -> None:
del self._elements[i]
del self.patterns[i]
def __len__(self) -> int:
return len(self._elements)
def insert(self, i: int, elem: ElementType) -> None:
self._elements.insert(i, elem)
self.patterns.insert(i, self._parse_value(elem))
def __repr__(self) -> str:
s = repr(self.regexps)
if len(s) < 70:
return '%s(%s)' % (self.__class__.__name__, s)
else:
return '%s(%s...\'])' % (self.__class__.__name__, s[:70])
def __call__(self, text: str) -> None:
try:
if all(pattern.match(text) is None for pattern in self.patterns):
reason = "value doesn't match any pattern of {!r}".format(self.regexps)
raise XMLSchemaValidationError(self, text, reason)
except TypeError as err:
raise XMLSchemaValidationError(self, text, str(err)) from None
@property
def regexps(self) -> List[str]:
return [e.attrib.get('value', '') for e in self._elements]
def get_annotation(self, i: int) -> Optional[XsdAnnotation]:
"""
Get the XSD annotation of the i-th pattern facet.
:param i: an integer index.
:returns: an XsdAnnotation object or `None`.
"""
for child in self._elements[i]:
if child.tag == XSD_ANNOTATION:
return XsdAnnotation(child, self.schema, self)
return None
class XsdAssertionXPathParser(XPath2Parser):
"""Parser for XSD 1.1 assertion facets."""
XsdAssertionXPathParser.unregister('last')
XsdAssertionXPathParser.unregister('position')
# noinspection PyUnusedLocal
@XsdAssertionXPathParser.method(XsdAssertionXPathParser.function('last', nargs=0))
def evaluate_last(self, context=None): # type: ignore[no-untyped-def]
raise self.missing_context("context item size is undefined")
# noinspection PyUnusedLocal
@XsdAssertionXPathParser.method(XsdAssertionXPathParser.function('position', nargs=0))
def evaluate_position(self, context=None): # type: ignore[no-untyped-def]
raise self.missing_context("context item position is undefined")
class XsdAssertionFacet(XsdFacet):
"""
XSD 1.1 *assertion* facet for simpleType definitions.
.. <assertion
id = ID
test = an XPath expression
xpathDefaultNamespace = (anyURI | (##defaultNamespace | ##targetNamespace | ##local))
{any attributes with non-schema namespace . . .}>
Content: (annotation?)
</assertion>
"""
_ADMITTED_TAGS = {XSD_ASSERTION}
_root = etree_element('root')
def __repr__(self) -> str:
return '%s(test=%r)' % (self.__class__.__name__, self.path)
def _parse(self) -> None:
try:
self.path = self.elem.attrib['test']
except KeyError:
self.parse_error("missing attribute 'test'")
self.path = 'true()'
try:
value = self.base_type.primitive_type.prefixed_name # type: ignore[union-attr]
except AttributeError:
value = self.any_simple_type.prefixed_name
if 'xpathDefaultNamespace' in self.elem.attrib:
self.xpath_default_namespace = self._parse_xpath_default_namespace(self.elem)
else:
self.xpath_default_namespace = self.schema.xpath_default_namespace
self.parser = XsdAssertionXPathParser(
namespaces=self.namespaces,
strict=False,
variable_types={'value': value},
default_namespace=self.xpath_default_namespace
)
try:
self.token = self.parser.parse(self.path)
except ElementPathError as err:
self.parse_error(err)
self.token = self.parser.parse('true()')
def __call__(self, value: AtomicValueType) -> None:
context = XPathContext(self._root, variables={'value': value})
try:
if not self.token.evaluate(context):
reason = "value is not true with test path {!r}".format(self.path)
raise XMLSchemaValidationError(self, value, reason)
except ElementPathError as err:
raise XMLSchemaValidationError(self, value, reason=str(err)) from None
XSD_10_FACETS_BUILDERS = {
XSD_WHITE_SPACE: XsdWhiteSpaceFacet,
XSD_LENGTH: XsdLengthFacet,
XSD_MIN_LENGTH: XsdMinLengthFacet,
XSD_MAX_LENGTH: XsdMaxLengthFacet,
XSD_MIN_INCLUSIVE: XsdMinInclusiveFacet,
XSD_MIN_EXCLUSIVE: XsdMinExclusiveFacet,
XSD_MAX_INCLUSIVE: XsdMaxInclusiveFacet,
XSD_MAX_EXCLUSIVE: XsdMaxExclusiveFacet,
XSD_TOTAL_DIGITS: XsdTotalDigitsFacet,
XSD_FRACTION_DIGITS: XsdFractionDigitsFacet,
XSD_ENUMERATION: XsdEnumerationFacets,
XSD_PATTERN: XsdPatternFacets
}
XSD_11_FACETS_BUILDERS = XSD_10_FACETS_BUILDERS.copy()
XSD_11_FACETS_BUILDERS.update({
XSD_ASSERTION: XsdAssertionFacet,
XSD_EXPLICIT_TIMEZONE: XsdExplicitTimezoneFacet
})
XSD_10_FACETS = set(XSD_10_FACETS_BUILDERS)
XSD_11_FACETS = set(XSD_11_FACETS_BUILDERS)
XSD_10_LIST_FACETS = {XSD_LENGTH, XSD_MIN_LENGTH, XSD_MAX_LENGTH, XSD_PATTERN,
XSD_ENUMERATION, XSD_WHITE_SPACE}
XSD_11_LIST_FACETS = XSD_10_LIST_FACETS | {XSD_ASSERTION}
XSD_10_UNION_FACETS = {XSD_PATTERN, XSD_ENUMERATION}
XSD_11_UNION_FACETS = MULTIPLE_FACETS = {XSD_PATTERN, XSD_ENUMERATION, XSD_ASSERTION}
XsdFacetType = Union[XsdLengthFacet, XsdMinLengthFacet, XsdMaxLengthFacet,
XsdMinInclusiveFacet, XsdMinExclusiveFacet, XsdMaxInclusiveFacet,
XsdMaxExclusiveFacet, XsdTotalDigitsFacet, XsdFractionDigitsFacet,
XsdEnumerationFacets, XsdPatternFacets]
|