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

net.kemitix.dependency.digraph.maven.plugin.DotFileFormatNested Maven / Gradle / Ivy

There is a newer version: 0.9.1
Show newest version
package net.kemitix.dependency.digraph.maven.plugin;

import net.kemitix.node.Node;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Generates a dot file dependency report as nested clusters.
 *
 * @author pcampbell
 */
class DotFileFormatNested extends AbstractDotFileFormat {

    DotFileFormatNested(
            final Node base,
            final NodePathGenerator nodePathGenerator) {
        super(base, nodePathGenerator);
    }

    @Override
    String renderNode(final Node node) {
        final StringBuilder render = new StringBuilder();
        final String clusterId = getPath(node, "_");
        final String nodeId = node.getData().getName();
        final String headerFormat
                = "subgraph \"cluster{0}\"'{'"
                + "label=\"{1}\";\"{1}\"[style=dotted]\n";
        render.append(MessageFormat.format(headerFormat, clusterId, nodeId));
        node.getChildren().stream()
                .sorted(new NodePackageDataComparator())
                .forEach((Node child) -> {
                    if (child.getChildren().size() > 0) {
                        render.append(renderNode(child));
                    } else {
                        render.append(renderLeafPackage(child));
                    }
                });
        render.append("}\n");
        return render.toString();
    }

    @Override
    String renderUsages(final Node node) {
        final StringBuilder usages = new StringBuilder();
        node.getChildren().stream()
                .sorted(new NodePackageDataComparator())
                .forEach((Node childNode) -> {
                    childNode.getData().getUses().stream()
                            .filter((Node n)
                                    -> n.isChildOf(getBase()))
                            .sorted(new NodePackageDataComparator())
                            .forEach((Node n) -> {
                                usages.append(renderUsage(childNode, n));
                            });
                    usages.append(renderUsages(childNode));
                });
        return usages.toString();
    }

    private String renderUsage(
            final Node tailNode,
            final Node headNode) {
        List attributes = new ArrayList<>();
        // if tail node has children, then add ltail attribute
        if (tailNode.getChildren().size() > 0) {
            attributes.add(String.format("ltail=\"cluster%s\",",
                    getPath(tailNode, "_")));
        }
        // if head node has children, then add lhead attribute
        if (headNode.getChildren().size() > 0) {
            attributes.add(String.format("lhead=\"cluster%s\",",
                    getPath(headNode, "_")));
        }
        final StringBuilder attributeTag = new StringBuilder();
        if (attributes.size() > 0) {
            attributeTag.append("[");
            attributes.forEach(attributeTag::append);
            attributeTag.append("]");
        }

        return String.format("\"%s\"->\"%s\"%s%n",
                getPath(tailNode, "."), getPath(headNode, "."), attributeTag);
    }

    private String renderLeafPackage(final Node node) {
        return String.format("\"%s\"[label=\"%s\"];",
                getPath(node, "."), node.getData().getName());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy