All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.maven.plugins.dependency.tree.VerboseGraphGraphmlSerializer Maven / Gradle / Ivy

There is a newer version: 3.8.1
Show newest version
package org.apache.maven.plugins.dependency.tree;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import org.eclipse.aether.graph.DependencyNode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

/**
 * Parses dependency graph and outputs in GraphML format for end user to review.
 */
public class VerboseGraphGraphmlSerializer extends AbstractVerboseGraphSerializer
{
    @Override
    public String serialize( DependencyNode root )
    {
        Set coordinateStrings = new HashSet<>();
        Map coordinateVersionMap = new HashMap<>();
        Map nodeErrors = getNodeConflictMessagesBfs( root, coordinateStrings,
                coordinateVersionMap );

        Set visitedNodes = new HashSet<>();
        Queue queue = new LinkedList<>();
        queue.add( root );
        StringBuilder result = new StringBuilder( " "
                + ""
                + "\n"
                + "  "
                + "\n"
                + "  "
                + "\n"
                + "" + "\n" );

        StringBuilder nodes = new StringBuilder();
        StringBuilder edges = new StringBuilder();
        while ( !queue.isEmpty() )
        {
            DependencyNode node = queue.poll();
            nodes.append( getGraphmlNodeLine( node, nodeErrors ) );
            if ( nodeErrors.get( node ) == null && !node.getArtifact().getProperties().containsKey( "Cycle" ) )
            {
                for ( DependencyNode child : node.getChildren() )
                {
                    if ( !visitedNodes.contains( child ) )
                    {
                        visitedNodes.add( child );
                        queue.add( child );
                    }
                    edges.append( getGraphmlEdgeLine( node, child, nodeErrors ) );
                }
            }
        }
        result.append( nodes ).append( edges );
        result.append( "" );
        return result.toString();
    }

    private String getGraphmlEdgeLine( DependencyNode parent, DependencyNode child,
                                       Map nodeErrors )
    {
        StringBuilder builder = new StringBuilder();
        builder.append( "" );


        boolean messageAdded = false;

        if ( child.getArtifact().getProperties().containsKey( PRE_MANAGED_SCOPE ) )
        {
            messageAdded = true;
            builder.append( child.getArtifact().getProperties().get( MANAGED_SCOPE ) );
            builder.append( ", scope managed from " ).append(
                    child.getArtifact().getProperties().get( PRE_MANAGED_SCOPE ) );
        }
        else
        {
            builder.append( child.getDependency().getScope() );
        }
        if ( child.getArtifact().getProperties().containsKey( "Cycle" ) )
        {
            if ( messageAdded )
            {
                builder.append( "," );
            }
            builder.append( " omitted due to cycle" );
        }
        else if ( nodeErrors.get( child ) != null )
        {
            if ( messageAdded )
            {
                builder.append( "," );
            }
            builder.append( " " ).append( nodeErrors.get( child ) );
        }
        builder.append( "" ).append( "\n" );
        return builder.toString();
    }

    private String getGraphmlNodeLine( DependencyNode node, Map nodeErrors )
    {
        StringBuilder builder = new StringBuilder();
        builder.append( "" + "" );

        String coordString = "";
        boolean messageAdded = false;

        if ( node.getArtifact().getProperties().containsKey( PRE_MANAGED_VERSION ) )
        {
            coordString +=
                    " - version managed from " + node.getArtifact().getProperties().get( PRE_MANAGED_VERSION );
            messageAdded = true;
        }
        if ( node.getArtifact().getProperties().containsKey( PRE_MANAGED_SCOPE ) )
        {
            if ( messageAdded )
            {
                coordString += "; ";
            }
            else
            {
                coordString += " - ";
                messageAdded = true;
            }
            coordString +=
                    "scope managed from " + node.getArtifact().getProperties().get( PRE_MANAGED_SCOPE );
        }
        builder.append( getDependencyCoordinate( node ) ).append( coordString );
        if ( node.getData().containsKey( "ContainsModule" ) )
        {
            builder.append( " WARNING: this tree contains a submodule. Once it reaches the submodule will print "
                    + "in nonVerbose fashion, to see the actual submodule "
                    + "verbose output refer to the rest of the output" );
        }
        if ( node.getArtifact().getProperties().containsKey( "Cycle" ) )
        {
            if ( !messageAdded )
            {
                builder.append( " - " );
            }
            builder.append( "omitted due to cycle" );
        }
        else if ( nodeErrors.get( node ) != null )
        {
            if ( !messageAdded )
            {
                builder.append( " - " );
            }
            builder.append( nodeErrors.get( node ) );
        }
        builder.append( "" ).append( "\n" );
        return builder.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy