com.github.ferstl.depgraph.graph.puml.PumlGraphFormatter Maven / Gradle / Ivy
/*
* 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) {
final 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 rectangle {\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) {
final 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(">>");
}
puml.append("\n");
}
}
private void writeEdges(StringBuilder puml, Collection edges) {
for (Edge edge : edges) {
final 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", "_"), "_");
}
}