Guice grapher实用程序中有一个bug导致大多数或所有图形呈现损坏。是否有解决方法或修复此问题?
答案 0 :(得分:16)
我稍微修改了@ wuppi的答案,也隐藏了类路径和长随机名称注释,使图形更加紧凑和可读。他的answer编辑后的代码如下:
我发现这个实用程序方法非常有用,它从来没有为我打错图。
关于style=invis
错误:Guice grapher插件生成一个点文件,其中一些clases的样式是不可见的。下面发布的方法中的replaceAll()
可以解决这个问题。其余代码与Guice示例几乎相同。
我已经整合了Scot对Guice 4.x的修复,其中包括Tim的答案:
public class Grapher {
public static void main(String[] args) throws Exception {
Grapher.graph4("filename.dot", Guice.createInjector(new MyModule()));
}
public static void graph4(String filename, Injector inj) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter out = new PrintWriter(baos);
Injector injector = Guice.createInjector(new GraphvizModule());
GraphvizGrapher renderer = injector.getInstance(GraphvizGrapher.class);
renderer.setOut(out);
renderer.setRankdir("TB");
renderer.graph(inj);
out = new PrintWriter(new File(filename), "UTF-8");
String s = baos.toString("UTF-8");
s = fixGrapherBug(s);
s = hideClassPaths(s);
out.write(s);
out.close();
}
public static String hideClassPaths(String s) {
s = s.replaceAll("\\w[a-z\\d_\\.]+\\.([A-Z][A-Za-z\\d_\\$]*)", "$1");
s = s.replaceAll("value=[\\w-]+", "random");
return s;
}
public static String fixGrapherBug(String s) {
s = s.replaceAll("style=invis", "style=solid");
s = s.replaceAll("margin=(\\S+), ", " margin=\"$1\", ");
return s;
}
}
当然,您可以自由生成任何其他文件名:)
答案 1 :(得分:5)
Guice 4.x将Jeff和Tim的解决方案结合起来的例子:
public class Grapher {
public static void main(String[] args) throws Exception {
Grapher.graph4("filename.dot", Guice.createInjector(new MyModule()));
}
public static void graph4(String filename, Injector inj) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter out = new PrintWriter(baos);
Injector injector = Guice.createInjector(new GraphvizModule());
GraphvizGrapher renderer = injector.getInstance(GraphvizGrapher.class);
renderer.setOut(out);
renderer.setRankdir("TB");
renderer.graph(inj);
out = new PrintWriter(new File(filename), "UTF-8");
String s = baos.toString("UTF-8");
s = fixGrapherBug(s);
s = hideClassPaths(s);
out.write(s);
out.close();
}
public static String hideClassPaths(String s) {
s = s.replaceAll("\\w[a-z\\d_\\.]+\\.([A-Z][A-Za-z\\d_]*)", "");
s = s.replaceAll("value=[\\w-]+", "random");
return s;
}
public static String fixGrapherBug(String s) {
s = s.replaceAll("style=invis", "style=solid");
s = s.replaceAll("margin=(\\S+), ", " margin=\"$1\", ");
return s;
}
}
答案 2 :(得分:2)
使用最新版本的GraphViz时,我发现以下替换也有帮助(否则GraphViz拒绝打开文件):
s.replaceAll(" margin=(\\S+), ", " margin=\"$1\", ")
答案 3 :(得分:-1)
上面的hideClassPaths()方法中的第一个replaceAll过于热心 - 它删除了类名和包。它应该是
s = s.replaceAll("\\w[a-z\\d_\\.]+\\.([A-Z][A-Za-z\\d_\\$]*)", "$1");
请注意添加美元符号,因此这也适用于内部类名。