|
27 | 27 | from enum import Enum
|
28 | 28 | from functools import reduce
|
29 | 29 | from json import loads as json_loads
|
30 |
| -from typing import Any, Dict, FrozenSet, Generator, Iterable, List, Optional, Tuple, Type |
| 30 | +from typing import Any, Dict, FrozenSet, Generator, Iterable, List, Optional, Tuple, Type, Union |
| 31 | +from urllib.parse import quote as url_quote |
| 32 | +from uuid import UUID |
31 | 33 | from warnings import warn
|
32 | 34 | from xml.etree.ElementTree import Element as XmlElement # nosec B405
|
33 | 35 |
|
|
51 | 53 | SchemaVersion1Dot5,
|
52 | 54 | SchemaVersion1Dot6,
|
53 | 55 | )
|
| 56 | +from .bom_ref import BomRef |
| 57 | + |
| 58 | +_BOM_LINK_PREFIX = 'urn:cdx:' |
54 | 59 |
|
55 | 60 |
|
56 | 61 | @serializable.serializable_enum
|
@@ -767,6 +772,36 @@ def deserialize(cls, o: Any) -> 'XsUri':
|
767 | 772 | f'XsUri string supplied does not parse: {o!r}'
|
768 | 773 | ) from err
|
769 | 774 |
|
| 775 | + @classmethod |
| 776 | + def make_bom_link( |
| 777 | + cls, |
| 778 | + serial_number: Union[UUID, str], |
| 779 | + version: int = 1, |
| 780 | + bom_ref: Optional[Union[str, BomRef]] = None |
| 781 | + ) -> 'XsUri': |
| 782 | + """ |
| 783 | + Generate a BOM-Link URI. |
| 784 | +
|
| 785 | + Args: |
| 786 | + serial_number: The unique serial number of the BOM. |
| 787 | + version: The version of the BOM. The default version is 1. |
| 788 | + bom_ref: The unique identifier of the component, service, or vulnerability within the BOM. |
| 789 | +
|
| 790 | + Returns: |
| 791 | + XsUri: Instance of XsUri with the generated BOM-Link URI. |
| 792 | + """ |
| 793 | + bom_ref_part = f'#{url_quote(str(bom_ref))}' if bom_ref else '' |
| 794 | + return cls(f'{_BOM_LINK_PREFIX}{serial_number}/{version}{bom_ref_part}') |
| 795 | + |
| 796 | + def is_bom_link(self) -> bool: |
| 797 | + """ |
| 798 | + Check if the URI is a BOM-Link. |
| 799 | +
|
| 800 | + Returns: |
| 801 | + `bool` |
| 802 | + """ |
| 803 | + return self._uri.startswith(_BOM_LINK_PREFIX) |
| 804 | + |
770 | 805 |
|
771 | 806 | @serializable.serializable_class
|
772 | 807 | class ExternalReference:
|
|
0 commit comments