-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polish Code and reduce duplicate code
Signed-off-by: Alex Alzate <aalzate@sonatype.com>
- Loading branch information
Showing
17 changed files
with
178 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/org/cyclonedx/util/deserializer/AbstractDataTypeDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* This file is part of CycloneDX Core (Java). | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) OWASP Foundation. All Rights Reserved. | ||
*/ | ||
package org.cyclonedx.util.deserializer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import org.cyclonedx.model.formulation.common.AbstractType; | ||
import org.cyclonedx.model.formulation.common.EnvVariableChoice; | ||
import org.cyclonedx.model.formulation.common.ResourceReferenceChoice; | ||
|
||
public abstract class AbstractDataTypeDeserializer<T extends AbstractType> | ||
extends JsonDeserializer<T> { | ||
|
||
protected final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
protected void setEnvironmentVars(final JsonNode node, AbstractType data) throws JsonProcessingException { | ||
JsonNode nodes = node.get("environmentVars"); | ||
List<EnvVariableChoice> environmentVars = new ArrayList<>(); | ||
|
||
ArrayNode environmentVarsNode = (nodes.isArray() ? (ArrayNode) nodes : new ArrayNode(null).add(nodes)); | ||
|
||
for (JsonNode envVarNode : environmentVarsNode) { | ||
EnvVariableChoice envVar = objectMapper.treeToValue(envVarNode, EnvVariableChoice.class); | ||
environmentVars.add(envVar); | ||
} | ||
data.setEnvironmentVars(environmentVars); | ||
} | ||
|
||
protected void setReference(JsonNode node, String fieldName, AbstractType type) | ||
throws JsonProcessingException | ||
{ | ||
if (node.has(fieldName)) { | ||
JsonNode fieldNode = node.get(fieldName); | ||
ResourceReferenceChoice reference = objectMapper.treeToValue(fieldNode, ResourceReferenceChoice.class); | ||
|
||
if ("source".equals(fieldName)) { | ||
type.setSource(reference); | ||
} else if ("target".equals(fieldName)) { | ||
type.setTarget(reference); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/org/cyclonedx/util/serializer/AbstractDataTypeSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.cyclonedx.util.serializer; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
import org.cyclonedx.model.formulation.common.AbstractType; | ||
import org.cyclonedx.model.formulation.common.EnvVariableChoice; | ||
|
||
|
||
public abstract class AbstractDataTypeSerializer<T extends AbstractType> extends StdSerializer<T> | ||
{ | ||
|
||
public AbstractDataTypeSerializer(Class<T> t) { | ||
super(t); | ||
} | ||
|
||
protected void parseEnvironmentVars(final JsonGenerator jsonGenerator, final List<EnvVariableChoice> vars) | ||
throws IOException | ||
{ | ||
jsonGenerator.writeArrayFieldStart("environmentVars"); | ||
for (EnvVariableChoice envVarChoice : vars) { | ||
if (envVarChoice.getEnvironmentVar() != null) { | ||
jsonGenerator.writeStartObject(); | ||
jsonGenerator.writeObjectField("environmentVar", envVarChoice.getEnvironmentVar()); | ||
jsonGenerator.writeEndObject(); | ||
} | ||
else if (envVarChoice.getValue() != null) { | ||
jsonGenerator.writeStartObject(); | ||
jsonGenerator.writeObjectField("value", envVarChoice.getValue()); | ||
jsonGenerator.writeEndObject(); | ||
} | ||
} | ||
jsonGenerator.writeEndArray(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.