• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4#
5# Copyright (c) 2025 Northeastern University
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19from typing import Dict, Any, Type
20
21from ohos.sbom.converters.base import SBOMFormat, SBOMConverterFactory, ISBOMConverter
22from ohos.sbom.sbom.metadata.sbom_meta_data import SBOMMetaData
23
24
25class SBOMConverter:
26    """
27    Main entry point for converting SBOM metadata between different formats.
28
29    This class provides:
30    1. On-demand conversion of SBOM metadata to various formats
31    2. Registration mechanism for new format converters
32    3. Factory-based converter instantiation
33    """
34
35    def __init__(self, sbom_meta: SBOMMetaData):
36        """
37        Initialize converter with SBOM metadata.
38
39        Args:
40            sbom_meta: The SBOM metadata object to be converted
41        """
42        self.sbom_meta = sbom_meta
43
44    @staticmethod
45    def register_format(sbom_format: SBOMFormat, converter: Type['ISBOMConverter']) -> None:
46        """
47        Register a new converter for a specific SBOM format.
48
49        Args:
50            sbom_format: Format to register (from SBOMFormat enum)
51            converter: Converter class implementing ISBOMConverter interface
52        """
53        SBOMConverterFactory.register(sbom_format, converter)
54
55    def convert(self, sbom_format: SBOMFormat) -> Dict[str, Any]:
56        """
57        Convert the SBOM metadata to the specified format.
58
59        Args:
60            sbom_format: Target format (from SBOMFormat enum)
61
62        Returns:
63            Dictionary containing the converted SBOM data
64        """
65        return SBOMConverterFactory.create(sbom_format).convert(self.sbom_meta)
66