feat(go-vendr): Parsing of vendr.conf files now has more robust handling of whitespace. (Completes
package com.synopsys.integration.detectable.detectables.go.vendr.parse;
import java.util.List;
+import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import com.synopsys.integration.bdio.graph.DependencyGraph;
import com.synopsys.integration.bdio.graph.MutableDependencyGraph;
@@ -20,27 +23,42 @@ import com.synopsys.integration.bdio.model.externalid.ExternalId;
import com.synopsys.integration.bdio.model.externalid.ExternalIdFactory;
public class VndrParser {
+ private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
private final ExternalIdFactory externalIdFactory;
- public VndrParser(final ExternalIdFactory externalIdFactory) {
+ public VndrParser(ExternalIdFactory externalIdFactory) {
this.externalIdFactory = externalIdFactory;
}
- public DependencyGraph parseVendorConf(final List<String> vendorConfContents) {
- final MutableDependencyGraph graph = new MutableMapDependencyGraph();
+ public DependencyGraph parseVendorConf(List<String> vendorConfContents) {
+ MutableDependencyGraph graph = new MutableMapDependencyGraph();
- // TODO test against moby
- vendorConfContents.forEach(line -> {
- if (StringUtils.isNotBlank(line) && !line.startsWith("#")) {
- final String[] parts = line.split(" ");
+ vendorConfContents.stream()
+ .filter(this::shouldIncludeLine)
+ .map(this::parseLineToDependency)
+ .filter(Optional::isPresent)
+ .map(Optional::get)
+ .forEach(graph::addChildToRoot);
- final ExternalId dependencyExternalId = externalIdFactory.createNameVersionExternalId(Forge.GOLANG, parts[0], parts[1]);
- final Dependency dependency = new Dependency(parts[0], parts[1], dependencyExternalId);
- graph.addChildToRoot(dependency);
+ return graph;
}
- });
- return graph;
+ private Optional<Dependency> parseLineToDependency(String line) {
+ // Using StringUtils:split can handle multiple whitespace characters. See IDETECT-2722.
+ // Using null as the separator will make StringUtils check for any whitespace. IE tabs/multiple-spaces
+ String[] parts = StringUtils.split(line, null);
+ if (parts.length < 2) {
+ logger.error("Failed to parse vendor.conf line. Excluding from BOM. Line: {}", line);
+ return Optional.empty();
+ }
+ ExternalId dependencyExternalId = externalIdFactory.createNameVersionExternalId(Forge.GOLANG, parts[0], parts[1]);
+ Dependency dependency = new Dependency(parts[0], parts[1], dependencyExternalId);
+ return Optional.of(dependency);
+ }
+
+ private boolean shouldIncludeLine(String line) {
+ return StringUtils.isNotBlank(line) && !line.startsWith("#");
}
}
