This is an automated email from the ASF dual-hosted git repository.

asf-gitbox-commits pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ant-antlibs-cyclonedx.git


The following commit(s) were added to refs/heads/main by this push:
     new 9ea6222  make specVersion configurable
9ea6222 is described below

commit 9ea6222e87dadb7e26ce2750d69d50ffd07ce6d4
Author: Stefan Bodewig <[email protected]>
AuthorDate: Sat May 2 14:04:02 2026 +0200

    make specVersion configurable
---
 .../org/apache/ant/cyclonedx/ComponentBomTask.java | 16 +++---
 src/main/org/apache/ant/cyclonedx/SpecVersion.java | 58 ++++++++++++++++++++++
 src/main/org/apache/ant/cyclonedx/ToolData.java    | 18 ++++---
 3 files changed, 80 insertions(+), 12 deletions(-)

diff --git a/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java 
b/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
index 2919579..14172ad 100644
--- a/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
+++ b/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
@@ -18,7 +18,6 @@ import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Task;
 
 import org.cyclonedx.Format;
-import org.cyclonedx.Version;
 import org.cyclonedx.exception.GeneratorException;
 import org.cyclonedx.generators.BomGeneratorFactory;
 import org.cyclonedx.generators.json.BomJsonGenerator;
@@ -37,6 +36,7 @@ public class ComponentBomTask extends Task {
 
     private File outputDirectory;
     private String bomName = "bom";
+    private SpecVersion specVersion = SpecVersion.VERSION_16;
     private OutputFormat format = OutputFormat.json;
     private Component component;
     private List<Component> additionalComponents = new ArrayList<>();
@@ -56,6 +56,10 @@ public class ComponentBomTask extends Task {
         this.format = format;
     }
 
+    public void setSpecVersion(SpecVersion specVersion) {
+        this.specVersion = specVersion;
+    }
+
     public Component createComponent() {
         if (component != null) {
             throw new BuildException("only one nested component element is 
permitted");
@@ -114,7 +118,7 @@ public class ComponentBomTask extends Task {
 
         Metadata meta = new Metadata();
         meta.setTimestamp(new Date());
-        meta.setToolChoice(ToolData.getToolInformation());
+        
meta.setToolChoice(ToolData.getToolInformation(specVersion.getVersion()));
 
         Lifecycles l = new Lifecycles();
         LifecycleChoice lc = new LifecycleChoice();
@@ -125,7 +129,7 @@ public class ComponentBomTask extends Task {
         if (component == null) {
             throw new BuildException("nested component element is required");
         }
-        
meta.setComponent(component.toMainCycloneDxComponent(Version.VERSION_16));
+        
meta.setComponent(component.toMainCycloneDxComponent(specVersion.getVersion()));
         if (useComponentSupplier) {
             OrganizationalEntity componentSupplier = 
meta.getComponent().getSupplier();
             if (componentSupplier == null) {
@@ -145,7 +149,7 @@ public class ComponentBomTask extends Task {
         if (!additionalComponents.isEmpty()) {
             List<org.cyclonedx.model.Component> cs = new ArrayList<>();
             for (Component c : additionalComponents) {
-                cs.add(c.toAdditionalCycloneDxComponent(Version.VERSION_16));
+                
cs.add(c.toAdditionalCycloneDxComponent(specVersion.getVersion()));
             }
             bom.setComponents(cs);
         }
@@ -208,7 +212,7 @@ public class ComponentBomTask extends Task {
     }
 
     private void writeJsonBom(Bom bom, File bomFile) throws IOException, 
GeneratorException {
-        BomJsonGenerator generator = 
BomGeneratorFactory.createJson(Version.VERSION_16, bom);
+        BomJsonGenerator generator = 
BomGeneratorFactory.createJson(specVersion.getVersion(), bom);
         try (FileOutputStream fos = new FileOutputStream(bomFile);
              OutputStreamWriter writer = new OutputStreamWriter(fos, 
StandardCharsets.UTF_8)) {
             writer.write(generator.toJsonString(true));
@@ -216,7 +220,7 @@ public class ComponentBomTask extends Task {
     }
 
     private void writeXmlBom(Bom bom, File bomFile) throws IOException, 
GeneratorException {
-        BomXmlGenerator generator = 
BomGeneratorFactory.createXml(Version.VERSION_16, bom);
+        BomXmlGenerator generator = 
BomGeneratorFactory.createXml(specVersion.getVersion(), bom);
         try (FileOutputStream fos = new FileOutputStream(bomFile);
              OutputStreamWriter writer = new OutputStreamWriter(fos, 
StandardCharsets.UTF_8)) {
             writer.write(generator.toXmlString());
diff --git a/src/main/org/apache/ant/cyclonedx/SpecVersion.java 
b/src/main/org/apache/ant/cyclonedx/SpecVersion.java
new file mode 100644
index 0000000..1c5879b
--- /dev/null
+++ b/src/main/org/apache/ant/cyclonedx/SpecVersion.java
@@ -0,0 +1,58 @@
+package org.apache.ant.cyclonedx;
+
+import java.util.Objects;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.types.EnumeratedAttribute;
+
+import org.cyclonedx.Version;
+
+public class SpecVersion extends EnumeratedAttribute {
+
+    public static final SpecVersion VERSION_16;
+
+    static {
+        VERSION_16 = new SpecVersion();
+        VERSION_16.setValue("1.6");
+    }
+
+    @Override
+    public String[] getValues() {
+        return new String[] { "1.0", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6" 
};
+    }
+
+    public Version getVersion() {
+        switch (getValue()) {
+        case "1.0":
+            return Version.VERSION_10;
+        case "1.1":
+            return Version.VERSION_11;
+        case "1.2":
+            return Version.VERSION_12;
+        case "1.3":
+            return Version.VERSION_13;
+        case "1.4":
+            return Version.VERSION_14;
+        case "1.5":
+            return Version.VERSION_15;
+        case "1.6":
+            return Version.VERSION_16;
+        default:
+            throw new BuildException("version '" + getValue() + "' is not 
supported");
+        }
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        if (this == other) {
+            return true;
+        }
+        return other != null && other.getClass().equals(getClass())
+            && Objects.equals(((SpecVersion) other).getValue(), getValue());
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(getValue());
+    }
+}
diff --git a/src/main/org/apache/ant/cyclonedx/ToolData.java 
b/src/main/org/apache/ant/cyclonedx/ToolData.java
index 28bcf24..02ea916 100644
--- a/src/main/org/apache/ant/cyclonedx/ToolData.java
+++ b/src/main/org/apache/ant/cyclonedx/ToolData.java
@@ -6,6 +6,8 @@ import java.io.InputStream;
 import java.net.URL;
 import java.security.CodeSource;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.Properties;
 
 import org.apache.tools.ant.types.resources.FileResource;
@@ -19,17 +21,21 @@ import org.cyclonedx.model.metadata.ToolInformation;
  * Provides tool information for BOM's metadata section.
  */
 public class ToolData {
-    private static ToolInformation cachedToolInformation;
+    private static Map<Version, ToolInformation> toolInformationCache = new 
HashMap<>();
 
     /**
      * Tool Information needed for BOM's metadata section.
      */
-    public static ToolInformation getToolInformation() throws IOException {
-        return cachedToolInformation != null ? cachedToolInformation
-            : (cachedToolInformation = cacheToolInformation());
+    public static ToolInformation getToolInformation(Version specVersion) 
throws IOException {
+        ToolInformation cachedToolInformation = 
toolInformationCache.get(specVersion);
+        if (cachedToolInformation == null) {
+            cachedToolInformation = cacheToolInformation(specVersion);
+            toolInformationCache.put(specVersion, cachedToolInformation);
+        }
+        return cachedToolInformation;
     }
 
-    private static ToolInformation cacheToolInformation() throws IOException {
+    private static ToolInformation cacheToolInformation(Version specVersion) 
throws IOException {
         ToolInformation tool = new ToolInformation();
         Component antlibComponent = new Component();
 
@@ -64,7 +70,7 @@ public class ToolData {
         }
 
         org.cyclonedx.model.Component cdxComponent =
-            antlibComponent.toMainCycloneDxComponent(Version.VERSION_16);
+            antlibComponent.toMainCycloneDxComponent(specVersion);
         cdxComponent.setBomRef(null);
         tool.setComponents(Collections.singletonList(cdxComponent));
         return tool;

Reply via email to