Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/com/siemens/sbom/standardbom/StandardBomParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,35 @@ public String stringify(@Nonnull final StandardBom pBom)
json = unescapeComponentFields(json);
json = unescapeExtRefPurls(json);
json = removeEmptyServicesMetadata(json);
json = injectSchemaSpec(json);
return json.concat(System.lineSeparator());
}



@Nonnull
private String injectSchemaSpec(@Nonnull final String pJson)
{
String schemaAttribute = " \"$schema\": \"http://cyclonedx.org/schema/bom-1.6.schema.json\","
.concat(System.lineSeparator());

// Check if $schema attribute already exists
if (pJson.contains("\"$schema\"")) {
return pJson;
}

// Find the position right after the opening curly brace
final String insertMarker = '{' + System.lineSeparator();
int insertPosition = pJson.indexOf(insertMarker) + insertMarker.length();

// Insert the $schema attribute at the found position
StringBuilder sb = new StringBuilder(pJson);
sb.insert(insertPosition, schemaAttribute);
return sb.toString();
}



private void escapeExtRefPurls(@Nonnull final StandardBom pBom)
{
if (pBom.getCycloneDxBom().getExternalReferences() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json",
"bomFormat" : "CycloneDX",
"specVersion" : "1.4",
"version" : 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
"bomFormat" : "CycloneDX",
"specVersion" : "1.6",
"version" : 1,
Expand Down