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
26 changes: 11 additions & 15 deletions src/main/java/org/cyclonedx/parsers/JsonParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,14 @@
@SuppressWarnings("unused")
public class JsonParser extends CycloneDxSchema implements Parser {

private final ObjectMapper mapper;

public JsonParser() {
mapper = new ObjectMapper();
}
private static final ObjectMapper MAPPER = new ObjectMapper();

/**
* {@inheritDoc}
*/
public Bom parse(final File file) throws ParseException {
try {
return mapper.readValue(file, Bom.class);
return MAPPER.readValue(file, Bom.class);
} catch (IOException e) {
throw new ParseException("Unable to parse BOM from File", e);
}
Expand All @@ -65,7 +61,7 @@ public Bom parse(final File file) throws ParseException {
*/
public Bom parse(final byte[] bomBytes) throws ParseException {
try {
return mapper.readValue(bomBytes, Bom.class);
return MAPPER.readValue(bomBytes, Bom.class);
} catch (RuntimeException | IOException e) {
throw new ParseException("Unable to parse BOM from byte array", e);
}
Expand All @@ -76,7 +72,7 @@ public Bom parse(final byte[] bomBytes) throws ParseException {
*/
public Bom parse(final InputStream inputStream) throws ParseException {
try {
return mapper.readValue(inputStream, Bom.class);
return MAPPER.readValue(inputStream, Bom.class);
} catch (IOException e) {
throw new ParseException("Unable to parse BOM from InputStream", e);
}
Expand All @@ -87,7 +83,7 @@ public Bom parse(final InputStream inputStream) throws ParseException {
*/
public Bom parse(final Reader reader) throws ParseException {
try {
return mapper.readValue(reader, Bom.class);
return MAPPER.readValue(reader, Bom.class);
} catch (IOException e) {
throw new ParseException("Unable to parse BOM from Reader", e);
}
Expand All @@ -104,7 +100,7 @@ public List<ParseException> validate(final File file) throws IOException {
* {@inheritDoc}
*/
public List<ParseException> validate(final File file, final Version schemaVersion) throws IOException {
return validate(mapper.readTree(file), schemaVersion);
return validate(MAPPER.readTree(file), schemaVersion);
}

/**
Expand All @@ -118,7 +114,7 @@ public List<ParseException> validate(final byte[] bomBytes) throws IOException {
* {@inheritDoc}
*/
public List<ParseException> validate(final byte[] bomBytes, final Version schemaVersion) throws IOException {
return validate(mapper.readTree(bomBytes), schemaVersion);
return validate(MAPPER.readTree(bomBytes), schemaVersion);
}

/**
Expand All @@ -139,7 +135,7 @@ public List<ParseException> validate(final Reader reader, final Version schemaVe
if (firstChar != -1 && firstChar != '\uFEFF') {
pushbackReader.unread(firstChar);
}
return validate(mapper.readTree(pushbackReader), schemaVersion);
return validate(MAPPER.readTree(pushbackReader), schemaVersion);
}

/**
Expand All @@ -153,7 +149,7 @@ public List<ParseException> validate(final InputStream inputStream) throws IOExc
* {@inheritDoc}
*/
public List<ParseException> validate(final InputStream inputStream, final Version schemaVersion) throws IOException {
return validate(mapper.readTree(inputStream), schemaVersion);
return validate(MAPPER.readTree(inputStream), schemaVersion);
}

/**
Expand All @@ -165,7 +161,7 @@ public List<ParseException> validate(final InputStream inputStream, final Versio
* @return a list of exceptions encountered during validation
*/
public List<ParseException> validate(final String bomString, final Version schemaVersion) throws IOException {
return validate(mapper.readTree(skipUtf8Bom(bomString)), schemaVersion);
return validate(MAPPER.readTree(skipUtf8Bom(bomString)), schemaVersion);
}

/**
Expand All @@ -186,7 +182,7 @@ public List<ParseException> validate(final JsonNode bomJson, final Version schem
);
}

List<Error> errors = getJsonSchema(schemaVersion, mapper).validate(mapper.readTree(bomJson.toString()));
List<Error> errors = getJsonSchema(schemaVersion, MAPPER).validate(MAPPER.readTree(bomJson.toString()));
for (Error error : errors) {
final boolean hasLocation =
error.getInstanceLocation() != null
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/org/cyclonedx/parsers/XmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@
@SuppressWarnings("unused")
public class XmlParser extends CycloneDxSchema implements Parser {

private final ObjectMapper mapper;

public XmlParser() {
mapper = new XmlMapper();
}
private static final ObjectMapper MAPPER = new XmlMapper();

private static final Map<String, String> SCHEMA_VERSION_BY_NAMESPACE = new HashMap<>();

Expand All @@ -87,7 +83,7 @@ public Bom parse(final File file) throws ParseException {
schemaVersion = identifySchemaVersion(fis);
}

return injectSchemaVersion(mapper.readValue(file, Bom.class), schemaVersion);
return injectSchemaVersion(MAPPER.readValue(file, Bom.class), schemaVersion);
} catch (IOException | XMLStreamException e) {
throw new ParseException(e);
}
Expand All @@ -100,7 +96,7 @@ public Bom parse(final byte[] bomBytes) throws ParseException {
try {
final String schemaVersion = identifySchemaVersion(new ByteArrayInputStream(bomBytes));

return injectSchemaVersion(mapper.readValue(bomBytes, Bom.class), schemaVersion);
return injectSchemaVersion(MAPPER.readValue(bomBytes, Bom.class), schemaVersion);
} catch (IOException | XMLStreamException e) {
throw new ParseException(e);
}
Expand All @@ -111,7 +107,7 @@ public Bom parse(final byte[] bomBytes) throws ParseException {
*/
public Bom parse(final InputStream inputStream) throws ParseException {
try {
return mapper.readValue(inputStream, Bom.class);
return MAPPER.readValue(inputStream, Bom.class);
} catch (IOException e) {
throw new ParseException(e);
}
Expand All @@ -122,7 +118,7 @@ public Bom parse(final InputStream inputStream) throws ParseException {
*/
public Bom parse(final Reader reader) throws ParseException {
try {
return mapper.readValue(reader, Bom.class);
return MAPPER.readValue(reader, Bom.class);
} catch (IOException e) {
throw new ParseException(e);
}
Expand Down
Loading