module Bibliothecary::MultiParsers::CycloneDX

Constants

NoComponents

Public Class Methods

mapping() click to toggle source
# File lib/bibliothecary/multi_parsers/cyclonedx.rb, line 65
def self.mapping
  {
    match_filename("cyclonedx.json") => {
      kind: "lockfile",
      parser: :parse_cyclonedx_json,
      ungroupable: true,
    },
    match_extension("cdx.json") => {
      kind: "lockfile",
      parser: :parse_cyclonedx_json,
      ungroupable: true,
    },
    match_filename("cyclonedx.xml") => {
      kind: "lockfile",
      parser: :parse_cyclonedx_xml,
      ungroupable: true,
    },
    match_extension(".cdx.xml") => {
      kind: "lockfile",
      parser: :parse_cyclonedx_xml,
      ungroupable: true,
    },
  }
end

Public Instance Methods

parse_cyclonedx_json(file_contents, options: {}) click to toggle source
# File lib/bibliothecary/multi_parsers/cyclonedx.rb, line 90
def parse_cyclonedx_json(file_contents, options: {})

  manifest = try_cache(options, options[:filename]) do
    JSON.parse(file_contents)
  end

  raise NoComponents unless manifest["components"]

  entries = ManifestEntries.new(parse_queue: manifest["components"])

  entries.parse! do |component, parse_queue|
    parse_queue.concat(component["components"]) if component["components"]

    component["purl"]
  end

  entries[platform_name.to_sym]
end
parse_cyclonedx_xml(file_contents, options: {}) click to toggle source
# File lib/bibliothecary/multi_parsers/cyclonedx.rb, line 109
def parse_cyclonedx_xml(file_contents, options: {})
  manifest = try_cache(options, options[:filename]) do
    Ox.parse(file_contents)
  end

  root = manifest
  if root.respond_to?(:bom)
    root = root.bom
  end

  raise NoComponents unless root.locate("components").first

  entries = ManifestEntries.new(parse_queue: root.locate("components/*"))

  entries.parse! do |component, parse_queue|
    # #locate returns an empty array if nothing is found, so we can
    # always safely concatenate it to the parse queue.
    parse_queue.concat(component.locate("components/*"))

    component.locate("purl").first&.text
  end

  entries[platform_name.to_sym]
end