|
Java example source code file (GraphvizGrapher.java)
The GraphvizGrapher.java Java example source code
/**
* Copyright (C) 2008 Google Inc.
*
* 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.google.inject.grapher.graphviz;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.google.inject.Key;
import com.google.inject.grapher.AbstractInjectorGrapher;
import com.google.inject.grapher.BindingEdge;
import com.google.inject.grapher.DependencyEdge;
import com.google.inject.grapher.ImplementationNode;
import com.google.inject.grapher.InstanceNode;
import com.google.inject.grapher.InterfaceNode;
import com.google.inject.grapher.NameFactory;
import com.google.inject.grapher.NodeId;
import com.google.inject.spi.InjectionPoint;
import java.io.PrintWriter;
import java.lang.reflect.Member;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* {@link com.google.inject.grapher.InjectorGrapher} implementation that writes out a Graphviz DOT
* file of the graph. Dependencies are bound in {@link GraphvizModule}.
* <p>
* Specify the {@link PrintWriter} to output to with {@link #setOut(PrintWriter)}.
*
* @author phopkins@gmail.com (Pete Hopkins)
* @since 4.0
*/
public class GraphvizGrapher extends AbstractInjectorGrapher {
private final Map<NodeId, GraphvizNode> nodes = Maps.newHashMap();
private final List<GraphvizEdge> edges = Lists.newArrayList();
private final NameFactory nameFactory;
private final PortIdFactory portIdFactory;
private PrintWriter out;
private String rankdir = "TB";
@Inject GraphvizGrapher(@Graphviz NameFactory nameFactory,
@Graphviz PortIdFactory portIdFactory) {
this.nameFactory = nameFactory;
this.portIdFactory = portIdFactory;
}
@Override protected void reset() {
nodes.clear();
edges.clear();
}
public void setOut(PrintWriter out) {
this.out = out;
}
public void setRankdir(String rankdir) {
this.rankdir = rankdir;
}
@Override protected void postProcess() {
start();
for (GraphvizNode node : nodes.values()) {
renderNode(node);
}
for (GraphvizEdge edge : edges) {
renderEdge(edge);
}
finish();
out.flush();
}
protected Map<String, String> getGraphAttributes() {
Map<String, String> attrs = Maps.newHashMap();
attrs.put("rankdir", rankdir);
return attrs;
}
protected void start() {
out.println("digraph injector {");
Map<String, String> attrs = getGraphAttributes();
out.println("graph " + getAttrString(attrs) + ";");
}
protected void finish() {
out.println("}");
}
protected void renderNode(GraphvizNode node) {
Map<String, String> attrs = getNodeAttributes(node);
out.println(node.getIdentifier() + " " + getAttrString(attrs));
}
protected Map<String, String> getNodeAttributes(GraphvizNode node) {
Map<String, String> attrs = Maps.newHashMap();
attrs.put("label", getNodeLabel(node));
// remove most of the margin because the table has internal padding
attrs.put("margin", "\"0.02,0\"");
attrs.put("shape", node.getShape().toString());
attrs.put("style", node.getStyle().toString());
return attrs;
}
/**
* Creates the "label" for a node. This is a string of HTML that defines a
* table with a heading at the top and (in the case of
* {@link ImplementationNode}s) rows for each of the member fields.
*/
protected String getNodeLabel(GraphvizNode node) {
String cellborder = node.getStyle() == NodeStyle.INVISIBLE ? "1" : "0";
StringBuilder html = new StringBuilder();
html.append("<");
html.append("<table cellspacing=\"0\" cellpadding=\"5\" cellborder=\"");
html.append(cellborder).append("\" border=\"0\">");
html.append("<tr>").append(" |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.