Class: Sbom::Parser
- Inherits:
-
Object
- Object
- Sbom::Parser
- Defined in:
- lib/sbom/parser.rb
Constant Summary collapse
- EXTENSION_MAP =
{ ".spdx" => [:spdx, :tag], ".spdx.json" => [:spdx, :json], ".spdx.yaml" => [:spdx, :yaml], ".spdx.yml" => [:spdx, :yaml], ".spdx.xml" => [:spdx, :xml], ".spdx.rdf" => [:spdx, :rdf], ".cdx.json" => [:cyclonedx, :json], ".bom.json" => [:cyclonedx, :json], ".cdx.xml" => [:cyclonedx, :xml], ".bom.xml" => [:cyclonedx, :xml] }.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(sbom_type: :auto) ⇒ Parser
constructor
A new instance of Parser.
- #parse_file(filename) ⇒ Object
- #parse_string(content, sbom_type: nil, format: nil) ⇒ Object
Constructor Details
#initialize(sbom_type: :auto) ⇒ Parser
Returns a new instance of Parser.
18 19 20 |
# File 'lib/sbom/parser.rb', line 18 def initialize(sbom_type: :auto) @sbom_type = sbom_type end |
Class Method Details
.parse_file(filename, sbom_type: :auto) ⇒ Object
51 52 53 |
# File 'lib/sbom/parser.rb', line 51 def self.parse_file(filename, sbom_type: :auto) new(sbom_type: sbom_type).parse_file(filename) end |
.parse_string(content, sbom_type: :auto) ⇒ Object
55 56 57 |
# File 'lib/sbom/parser.rb', line 55 def self.parse_string(content, sbom_type: :auto) new(sbom_type: sbom_type).parse_string(content) end |
Instance Method Details
#parse_file(filename) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/sbom/parser.rb', line 22 def parse_file(filename) raise ParserError, "File not found: #{filename}" unless File.exist?(filename) raise ParserError, "Empty file: #{filename}" if File.size(filename).zero? content = File.read(filename) sbom_type, format = detect_type_from_filename(filename) parse_string(content, sbom_type: sbom_type, format: format) end |
#parse_string(content, sbom_type: nil, format: nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/sbom/parser.rb', line 32 def parse_string(content, sbom_type: nil, format: nil) sbom_type ||= @sbom_type if sbom_type == :auto sbom_type, format = detect_type_from_content(content) end case sbom_type when :spdx parser = Spdx::Parser.new parser.parse(content, format) when :cyclonedx parser = Cyclonedx::Parser.new parser.parse(content, format) else try_both_parsers(content) end end |