Search in sources :

Example 1 with ExcludedIncludedFilter

use of com.synopsys.integration.util.ExcludedIncludedFilter in project hub-detect by blackducksoftware.

the class SbtPackager method makeModuleAggregate.

private List<SbtDependencyModule> makeModuleAggregate(final List<File> reportFiles, final String include, final String exclude) throws SAXException, IOException, ParserConfigurationException {
    final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    final DocumentBuilder builder = factory.newDocumentBuilder();
    final SbtReportParser parser = new SbtReportParser();
    final SbtDependencyResolver resolver = new SbtDependencyResolver(externalIdFactory);
    final ExcludedIncludedFilter filter = new ExcludedIncludedFilter(exclude, include);
    final SbtModuleAggregator aggregator = new SbtModuleAggregator();
    final List<SbtDependencyModule> modules = new ArrayList<>();
    for (final File reportFile : reportFiles) {
        final Document xml = builder.parse(reportFile);
        logger.debug(String.format("Parsing SBT report file: %s", reportFile.getCanonicalPath()));
        final SbtReport report = parser.parseReportFromXml(xml);
        final SbtDependencyModule tree = resolver.resolveReport(report);
        modules.add(tree);
    }
    final List<SbtDependencyModule> includedModules = modules.stream().filter(module -> filter.shouldInclude(module.configuration)).collect(Collectors.toList());
    if (modules.size() <= 0) {
        logger.warn("No sbt configurations were found in report folder.");
        return null;
    } else if (includedModules.size() <= 0) {
        logger.warn(String.format("Although %s configs were found, none were included.", modules.size()));
        return null;
    }
    return aggregator.aggregateModules(includedModules);
}
Also used : Arrays(java.util.Arrays) DetectorType(com.blackducksoftware.integration.hub.detect.detector.DetectorType) ExcludedIncludedFilter(com.synopsys.integration.util.ExcludedIncludedFilter) Logger(org.slf4j.Logger) Forge(com.synopsys.integration.bdio.model.Forge) LoggerFactory(org.slf4j.LoggerFactory) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) File(java.io.File) ArrayList(java.util.ArrayList) List(java.util.List) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXException(org.xml.sax.SAXException) DetectFileFinder(com.blackducksoftware.integration.hub.detect.workflow.file.DetectFileFinder) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ArrayList(java.util.ArrayList) ExcludedIncludedFilter(com.synopsys.integration.util.ExcludedIncludedFilter) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File)

Example 2 with ExcludedIncludedFilter

use of com.synopsys.integration.util.ExcludedIncludedFilter in project hub-detect by blackducksoftware.

the class MavenCodeLocationPackager method extractCodeLocations.

// mavenTextOutput should be the full output of mvn dependency:tree (no scope applied); scope filtering is now done by this method
public List<MavenParseResult> extractCodeLocations(final String sourcePath, final String mavenOutputText, final String targetScope, final String excludedModules, final String includedModules) {
    final ExcludedIncludedFilter filter = new ExcludedIncludedFilter(excludedModules, includedModules);
    codeLocations = new ArrayList<>();
    currentMavenProject = null;
    dependencyParentStack = new Stack<>();
    parsingProjectSection = false;
    currentGraph = new MutableMapDependencyGraph();
    level = 0;
    for (final String currentLine : mavenOutputText.split(System.lineSeparator())) {
        String line = currentLine.trim();
        if (!isLineRelevant(line)) {
            continue;
        }
        line = trimLogLevel(line);
        if (StringUtils.isBlank(line)) {
            continue;
        }
        if (isProjectSection(line)) {
            parsingProjectSection = true;
            continue;
        }
        if (!parsingProjectSection) {
            continue;
        }
        if (isDependencyTreeUpdates(line)) {
            continue;
        }
        if (parsingProjectSection && currentMavenProject == null) {
            // this is the first line of a new code location, the following lines will be the tree of dependencies for this code location
            currentGraph = new MutableMapDependencyGraph();
            final MavenParseResult mavenProject = createMavenParseResult(sourcePath, line, currentGraph);
            if (null != mavenProject && filter.shouldInclude(mavenProject.projectName)) {
                logger.trace(String.format("Project: %s", mavenProject.projectName));
                this.currentMavenProject = mavenProject;
                codeLocations.add(mavenProject);
            } else {
                logger.trace("Project: unknown");
                currentMavenProject = null;
                dependencyParentStack.clear();
                parsingProjectSection = false;
                level = 0;
            }
            continue;
        }
        final boolean finished = line.contains("--------");
        if (finished) {
            currentMavenProject = null;
            dependencyParentStack.clear();
            parsingProjectSection = false;
            level = 0;
            continue;
        }
        final int previousLevel = level;
        final String cleanedLine = calculateCurrentLevelAndCleanLine(line);
        final ScopedDependency dependency = textToDependency(cleanedLine);
        if (null == dependency) {
            continue;
        }
        if (currentMavenProject != null) {
            if (level == 1) {
                // a direct dependency, clear the stack and add this as a potential parent for the next line
                if (dependency.isInScope(targetScope)) {
                    logger.trace(String.format("Level 1 component %s:%s:%s:%s is in scope; adding it to hierarchy root", dependency.externalId.group, dependency.externalId.name, dependency.externalId.version, dependency.scope));
                    currentGraph.addChildToRoot(dependency);
                    inOutOfScopeTree = false;
                } else {
                    logger.trace(String.format("Level 1 component %s:%s:%s:%s is a top-level out-of-scope component; entering non-scoped tree", dependency.externalId.group, dependency.externalId.name, dependency.externalId.version, dependency.scope));
                    inOutOfScopeTree = true;
                }
                dependencyParentStack.clear();
                dependencyParentStack.push(dependency);
            } else {
                // level should be greater than 1
                if (level == previousLevel) {
                    // a sibling of the previous dependency
                    dependencyParentStack.pop();
                    addDependencyIfInScope(currentGraph, orphans, targetScope, inOutOfScopeTree, dependencyParentStack.peek(), dependency);
                    dependencyParentStack.push(dependency);
                } else if (level > previousLevel) {
                    // a child of the previous dependency
                    addDependencyIfInScope(currentGraph, orphans, targetScope, inOutOfScopeTree, dependencyParentStack.peek(), dependency);
                    dependencyParentStack.push(dependency);
                } else {
                    // a child of a dependency further back than 1 line
                    for (int i = previousLevel; i >= level; i--) {
                        dependencyParentStack.pop();
                    }
                    addDependencyIfInScope(currentGraph, orphans, targetScope, inOutOfScopeTree, dependencyParentStack.peek(), dependency);
                    dependencyParentStack.push(dependency);
                }
            }
        }
    }
    addOrphansToGraph(currentGraph, orphans);
    return codeLocations;
}
Also used : ExcludedIncludedFilter(com.synopsys.integration.util.ExcludedIncludedFilter) MutableMapDependencyGraph(com.synopsys.integration.bdio.graph.MutableMapDependencyGraph)

Aggregations

ExcludedIncludedFilter (com.synopsys.integration.util.ExcludedIncludedFilter)2 DetectorType (com.blackducksoftware.integration.hub.detect.detector.DetectorType)1 DetectFileFinder (com.blackducksoftware.integration.hub.detect.workflow.file.DetectFileFinder)1 MutableMapDependencyGraph (com.synopsys.integration.bdio.graph.MutableMapDependencyGraph)1 Forge (com.synopsys.integration.bdio.model.Forge)1 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Document (org.w3c.dom.Document)1 SAXException (org.xml.sax.SAXException)1