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 d866632 make OutputFormat more useful
d866632 is described below
commit d86663232f0d7e52f29f2bb216c0b9510bcaa9a4
Author: Stefan Bodewig <[email protected]>
AuthorDate: Thu May 14 14:58:30 2026 +0200
make OutputFormat more useful
---
.../org/apache/ant/cyclonedx/ComponentBomTask.java | 4 +-
.../org/apache/ant/cyclonedx/OutputFormat.java | 60 ++++++++++++++++++----
src/main/org/apache/ant/cyclonedx/SpecVersion.java | 5 ++
3 files changed, 58 insertions(+), 11 deletions(-)
diff --git a/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
b/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
index 6117e5d..a8d6cf9 100644
--- a/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
+++ b/src/main/org/apache/ant/cyclonedx/ComponentBomTask.java
@@ -40,7 +40,7 @@ public class ComponentBomTask extends Task {
private File outputDirectory;
private String bomName = "bom";
private SpecVersion specVersion = SpecVersion.DEFAULT;
- private OutputFormat format = OutputFormat.json;
+ private OutputFormat format = OutputFormat.JSON;
private Component component;
private List<Component> additionalComponents = new ArrayList<>();
private Organization manufacturer = null;
@@ -113,7 +113,7 @@ public class ComponentBomTask extends Task {
try {
Bom bom = createBom();
- for (Format f : format.getCycloneDxFormats()) {
+ for (Format f :
format.getCycloneDxFormats(specVersion.getVersion())) {
writeBom(bom, f,
new File(outputDirectory,
bomName + "." +
f.name().toLowerCase(Locale.ENGLISH)));
diff --git a/src/main/org/apache/ant/cyclonedx/OutputFormat.java
b/src/main/org/apache/ant/cyclonedx/OutputFormat.java
index 21c02c3..5a77793 100644
--- a/src/main/org/apache/ant/cyclonedx/OutputFormat.java
+++ b/src/main/org/apache/ant/cyclonedx/OutputFormat.java
@@ -1,21 +1,63 @@
package org.apache.ant.cyclonedx;
import java.util.Arrays;
+import java.util.Locale;
+import java.util.stream.Stream;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.types.EnumeratedAttribute;
+
+import org.cyclonedx.Version;
import org.cyclonedx.Format;
-public enum OutputFormat {
- json(Format.JSON),
- xml(Format.XML),
- all(Format.JSON, Format.XML);
+/**
+ * CycloneDX format to use for the SBOM.
+ *
+ * <p>Accepts the enum constants like {@code JSON} as well as the
+ * lowercase version {@code json} and the special value {@code
+ * all}. The values other than {@code all} are directly provided by
+ * CycloneDX Core's enum.</p>
+ *
+ * <p>{@code all} means the task will emit SBOMs in all formats
+ * supported by the selected {@link SpecVersion} - i.e. only XML for
+ * versions 1.1 and 1.2 and both JSON and XML afterwards.</p>
+ */
+public class OutputFormat extends EnumeratedAttribute {
+
+ public static final OutputFormat JSON;
- private Format[] formats;
+ static {
+ JSON = new OutputFormat();
+ JSON.setValue(Format.JSON.name());
+ }
- private OutputFormat(Format... formats) {
- this.formats = formats;
+ @Override
+ public String[] getValues() {
+ return Stream
+ .concat(Arrays.stream(Format.values())
+ .flatMap(f -> Stream.of(f.name(), f.getExtension())),
+ Stream.of("all", "ALL"))
+ .toArray(String[]::new);
}
- public Iterable<Format> getCycloneDxFormats() {
- return Arrays.asList(formats);
+ /**
+ * Translates this instance to {@link Format}s.
+ *
+ * @throws BuildException if the value can not be translated.
+ */
+ public Iterable<Format> getCycloneDxFormats(Version version) {
+ String value = getValue();
+ if (value.equalsIgnoreCase("all")) {
+ return version.getFormats();
+ }
+ Format format = Format.fromExtension(value);
+ if (format == null) {
+ try {
+ format = Format.valueOf(value);
+ } catch (IllegalArgumentException ex) {
+ throw new BuildException(getValue() + " is not a supported
format");
+ }
+ }
+ return Arrays.asList(format);
}
}
diff --git a/src/main/org/apache/ant/cyclonedx/SpecVersion.java
b/src/main/org/apache/ant/cyclonedx/SpecVersion.java
index 94c8160..a06d861 100644
--- a/src/main/org/apache/ant/cyclonedx/SpecVersion.java
+++ b/src/main/org/apache/ant/cyclonedx/SpecVersion.java
@@ -32,6 +32,11 @@ public class SpecVersion extends EnumeratedAttribute {
.toArray(String[]::new);
}
+ /**
+ * Translates this instance to a {@link Version}.
+ *
+ * @throws BuildException if the value can not be translated.
+ */
public Version getVersion() {
Version version = Version.fromVersionString(getValue());
if (version == null) {