Skip to content

Commit

Permalink
add new Enum based on the spec
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-zepol committed Dec 18, 2024
1 parent 0a6732e commit d1dd865
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 17 deletions.
15 changes: 12 additions & 3 deletions src/main/java/org/cyclonedx/model/License.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.cyclonedx.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand All @@ -30,6 +31,7 @@
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import org.cyclonedx.Version;
import org.cyclonedx.model.license.Acknowledgement;
import org.cyclonedx.util.deserializer.PropertiesDeserializer;

@SuppressWarnings("unused")
Expand All @@ -51,7 +53,7 @@ public class License extends ExtensibleElement {
@JacksonXmlProperty(isAttribute = true, localName = "acknowledgement")
@JsonProperty("acknowledgement")
@VersionFilter(Version.VERSION_16)
private String acknowledgement;
private Acknowledgement acknowledgement;

@VersionFilter(Version.VERSION_15)
private Licensing licensing;
Expand Down Expand Up @@ -120,6 +122,13 @@ public void setProperties(final List<Property> properties) {
this.properties = properties;
}

public void addProperty(Property property) {
if(this.properties == null) {
this.properties = new ArrayList<>();
}
this.properties.add(property);
}

public AttachmentText getAttachmentText() {
return attachmentText;
}
Expand All @@ -128,11 +137,11 @@ public void setLicenseText(AttachmentText attachmentText) {
this.attachmentText = attachmentText;
}

public String getAcknowledgement() {
public Acknowledgement getAcknowledgement() {
return acknowledgement;
}

public void setAcknowledgement(final String acknowledgement) {
public void setAcknowledgement(final Acknowledgement acknowledgement) {
this.acknowledgement = acknowledgement;
}

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/cyclonedx/model/license/Acknowledgement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.cyclonedx.model.license;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.cyclonedx.model.ExternalReference.Type;

public enum Acknowledgement
{
@JsonProperty("declared")
DECLARED("declared"),
@JsonProperty("concluded")
CONCLUDED("concluded");

private final String name;

public String getValue() {
return this.name;
}

Acknowledgement(String name) {
this.name = name;
}

public static Acknowledgement fromString(String text) {
for (Acknowledgement t : Acknowledgement.values()) {
if (t.name.equals(text)) {
return t;
}
}
return null;
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/cyclonedx/model/license/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Expression
@JacksonXmlProperty(isAttribute = true, localName = "acknowledgement")
@JsonProperty("acknowledgement")
@VersionFilter(Version.VERSION_16)
private String acknowledgement;
private Acknowledgement acknowledgement;

@JacksonXmlText
@JsonProperty("expression")
Expand All @@ -49,11 +49,11 @@ public void setBomRef(final String bomRef) {
this.bomRef = bomRef;
}

public String getAcknowledgement() {
public Acknowledgement getAcknowledgement() {
return acknowledgement;
}

public void setAcknowledgement(final String acknowledgement) {
public void setAcknowledgement(final Acknowledgement acknowledgement) {
this.acknowledgement = acknowledgement;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import org.cyclonedx.model.license.Acknowledgement;
import org.cyclonedx.model.license.Expression;

public class ExpressionDeserializer
Expand Down Expand Up @@ -53,7 +54,7 @@ private Expression parseExpressionNode(JsonNode node) {
}

if (node.has("acknowledgement")) {
expression.setAcknowledgement(node.get("acknowledgement").asText());
expression.setAcknowledgement(Acknowledgement.fromString(node.get("acknowledgement").asText()));
}

JsonNode textNode = node.get("expression");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.cyclonedx.model.License;
import org.cyclonedx.model.LicenseChoice;
import org.cyclonedx.model.Property;
import org.cyclonedx.model.license.Acknowledgement;
import org.cyclonedx.model.license.Expression;

import static org.cyclonedx.util.serializer.SerializerUtils.shouldSerializeField;
Expand Down Expand Up @@ -129,7 +130,7 @@ else if (lc.getExpression() != null) {
private void serializeXmlAttributes(
final ToXmlGenerator toXmlGenerator,
final String bomRef,
final String acknowledgement,
final Acknowledgement acknowledgement,
final Object object) throws IOException
{
toXmlGenerator.writeStartObject();
Expand All @@ -140,10 +141,10 @@ private void serializeXmlAttributes(
toXmlGenerator.writeString(bomRef);
toXmlGenerator.setNextIsAttribute(false);
}
if (StringUtils.isNotBlank(acknowledgement) && shouldSerializeField(object, version, "acknowledgement")) {
if (acknowledgement != null && shouldSerializeField(object, version, "acknowledgement")) {
toXmlGenerator.setNextIsAttribute(true);
toXmlGenerator.writeFieldName("acknowledgement");
toXmlGenerator.writeString(acknowledgement);
toXmlGenerator.writeString(acknowledgement.getValue());
toXmlGenerator.setNextIsAttribute(false);
}
}
Expand Down Expand Up @@ -197,9 +198,8 @@ private void serializeExpressionToJson(final LicenseChoice licenseChoice, final
gen.writeStartArray();
gen.writeStartObject();
gen.writeStringField("expression", expression.getValue());
if (StringUtils.isNotBlank(expression.getAcknowledgement()) &&
shouldSerializeField(expression, version, "acknowledgement")) {
gen.writeStringField("acknowledgement", expression.getAcknowledgement());
if (expression.getAcknowledgement() != null && shouldSerializeField(expression, version, "acknowledgement")) {
gen.writeStringField("acknowledgement", expression.getAcknowledgement().getValue());
}
if (StringUtils.isNotBlank(expression.getBomRef()) && shouldSerializeField(expression, version, "bomRef")) {
gen.writeStringField("bom-ref", expression.getBomRef());
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/cyclonedx/parsers/AbstractParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import org.cyclonedx.model.formulation.trigger.Trigger;
import org.cyclonedx.model.formulation.workspace.Volume;
import org.cyclonedx.model.formulation.workspace.Workspace;
import org.cyclonedx.model.license.Acknowledgement;
import org.cyclonedx.model.vulnerability.Vulnerability;
import org.cyclonedx.model.vulnerability.Vulnerability.Analysis.Justification;
import org.cyclonedx.model.vulnerability.Vulnerability.Analysis.State;
Expand Down Expand Up @@ -1010,7 +1011,7 @@ void assertAck(Bom bom) {
License license = lc.getLicenses().get(0);
assertEquals("Apache-2.0", license.getId());
assertEquals("my-license", license.getBomRef());
assertEquals("declared", license.getAcknowledgement());
assertEquals(Acknowledgement.DECLARED, license.getAcknowledgement());
}

Bom getXmlBom(String filename) throws ParseException, IOException {
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/cyclonedx/parsers/JsonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.cyclonedx.model.definition.Level;
import org.cyclonedx.model.definition.Requirement;
import org.cyclonedx.model.definition.Standard;
import org.cyclonedx.model.license.Acknowledgement;
import org.cyclonedx.model.license.Expression;
import org.junit.jupiter.api.Test;
import java.io.File;
Expand Down Expand Up @@ -281,7 +282,7 @@ public void schema16_license_id_acknowledgement() throws Exception {
License license = lc.getLicenses().get(0);
assertEquals("Apache-2.0", license.getId());
assertEquals("my-license", license.getBomRef());
assertEquals("declared", license.getAcknowledgement());
assertEquals(Acknowledgement.DECLARED, license.getAcknowledgement());
}

@Test
Expand All @@ -295,7 +296,7 @@ public void schema16_license_expression_acknowledgement() throws Exception {
Expression expression = lc.getExpression();
assertEquals("EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0", expression.getValue());
assertEquals("my-license", expression.getBomRef());
assertEquals("declared", expression.getAcknowledgement());
assertEquals(Acknowledgement.DECLARED, expression.getAcknowledgement());
}

@Test
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/cyclonedx/parsers/XmlParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.cyclonedx.model.definition.Level;
import org.cyclonedx.model.definition.Requirement;
import org.cyclonedx.model.definition.Standard;
import org.cyclonedx.model.license.Acknowledgement;
import org.cyclonedx.model.license.Expression;
import org.junit.jupiter.api.Test;
import java.io.File;
Expand Down Expand Up @@ -440,7 +441,7 @@ public void schema16_license_expression_acknowledgement() throws Exception {
Expression expression = lc.getExpression();
assertEquals("EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0", expression.getValue());
assertEquals("my-license", expression.getBomRef());
assertEquals("declared", expression.getAcknowledgement());
assertEquals(Acknowledgement.DECLARED, expression.getAcknowledgement());
}

@Test
Expand Down

0 comments on commit d1dd865

Please sign in to comment.