Module: Bibliothecary::MultiParsers::CycloneDX

Includes:
Analyser, Analyser::TryCache
Defined in:
lib/bibliothecary/multi_parsers/cyclonedx.rb

Defined Under Namespace

Classes: ManifestEntries

Constant Summary collapse

NoComponents =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Analyser::TryCache

#try_cache

Methods included from Analyser

create_analysis, create_error_analysis, included

Class Method Details

.mappingObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bibliothecary/multi_parsers/cyclonedx.rb', line 77

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

Instance Method Details

#parse_cyclonedx_json(file_contents, options: {}) ⇒ Object

Raises:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bibliothecary/multi_parsers/cyclonedx.rb', line 102

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: {}) ⇒ Object

Raises:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/bibliothecary/multi_parsers/cyclonedx.rb', line 121

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