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

com.github.ferstl.depgraph.graph.puml.PumlGraphFormatter Maven / Gradle / Ivy

Go to download

This Maven plugin generates dependency graphs on single modules or in an aggregated form on multi-module projects. The graphs are represented by .dot files. In case that Graphviz is installed on the machine where this plugin is run, the .dot file can be directly converted into all supported image files.

There is a newer version: 4.0.3
Show newest version
/*
 * Copyright (c) 2014 - 2017 the original author or authors.
 *
 * Licensed 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.
 */
package com.github.ferstl.depgraph.graph.puml;

import java.util.Collection;
import org.apache.commons.lang3.StringUtils;
import com.github.ferstl.depgraph.dependency.puml.PumlEdgeInfo;
import com.github.ferstl.depgraph.dependency.puml.PumlNodeInfo;
import com.github.ferstl.depgraph.graph.Edge;
import com.github.ferstl.depgraph.graph.GraphFormatter;
import com.github.ferstl.depgraph.graph.Node;

/**
 * Graph formatter for http://plantuml.com/component-diagram diagram.
 */
public class PumlGraphFormatter implements GraphFormatter {

  @Override
  public String format(String graphName, Collection> nodes, Collection edges) {
    StringBuilder puml = new StringBuilder();

    startUml(puml);
    skinParam(puml);
    writeNodes(puml, nodes);
    writeEdges(puml, edges);
    endUml(puml);

    return puml.toString();
  }

  private void startUml(StringBuilder puml) {
    puml.append("@startuml\n");
  }

  private void skinParam(StringBuilder puml) {
    puml.append("skinparam defaultTextAlignment center\n")
        .append("skinparam rectangle {\n")
        .append("  BackgroundColor<> beige\n")
        .append("  BackgroundColor<> lightGreen\n")
        .append("  BackgroundColor<> lightBlue\n")
        .append("  BackgroundColor<> lightGray\n")
        .append("}\n");
  }

  private void writeNodes(StringBuilder puml, Collection> nodes) {
    for (Node node : nodes) {

      PumlNodeInfo nodeInfo = PumlNodeInfo.parse(node.getNodeName());

      puml.append(nodeInfo.getComponent())
          .append(" \"")
          .append(nodeInfo.getLabel())
          .append("\" as ")
          .append(escape(node.getNodeId()));


      if (!nodeInfo.getStereotype().equals("compile")) {
        puml.append("<<")
            .append(nodeInfo.getStereotype())
            .append(">>");
      }

      if (nodeInfo.isOptional()) {
        puml.append("<<")
            .append("optional")
            .append(">>");
      }


      puml.append("\n");
    }
  }

  private void writeEdges(StringBuilder puml, Collection edges) {
    for (Edge edge : edges) {
      PumlEdgeInfo edgeInfo = PumlEdgeInfo.parse(edge.getName());
      puml.append(escape(edge.getFromNodeId()))
          .append(" ")
          .append(edgeInfo.getBegin())
          .append(edgeInfo.getColor())
          .append(edgeInfo.getEnd())
          .append(" ")
          .append(escape(edge.getToNodeId()));

      if (edgeInfo.getLabel() != null && !edgeInfo.getLabel().equals("")) {
        puml.append(": ")
            .append(edgeInfo.getLabel());
      }

      puml.append("\n");
    }
  }

  private void endUml(StringBuilder puml) {
    puml.append("@enduml");
  }

  private String escape(String id) {
    return StringUtils.removeEnd(id.replaceAll("\\W", "_"), "_");
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy