Permalink
Browse files

Refactor package diagram creator

  • Loading branch information...
1 parent c345417 commit 57dee96b149de72f9d892d99e3eb3253805c882f Basil Vandegriend committed Mar 6, 2012
Showing with 67 additions and 49 deletions.
  1. +67 −49 Examples/src/java/com/basilv/examples/packagediagram/PackageDiagramCreatorApp.java
@@ -5,59 +5,33 @@
import jdepend.framework.*;
-public class PackageDiagramCreatorApp
-{
+public class PackageDiagramCreatorApp {
- private static void generateImage(String fileName,
- String graphVizDotFormattedGraph) throws IOException,
- InterruptedException {
- File graphFile = createFileWithContents(fileName
- + ".txt", graphVizDotFormattedGraph);
-
- // This requires the GraphViz software to be installed -
- // see http://graphviz.org/
- String imageFileLocation = fileName + ".png";
- Runtime.getRuntime().exec(
- "dot -v -Tpng " + graphFile.getName() + " -o "
- + imageFileLocation);
-
- System.out.println("Image file available at "
- + new File(imageFileLocation).getAbsolutePath());
- }
-
- private static File createFileWithContents(
- String fileName, String graphVizDotFormattedGraph)
- throws IOException {
- File graphFile = new File(fileName);
- FileWriter writer = new FileWriter(graphFile, false);
- try {
- writer.append(graphVizDotFormattedGraph);
- } finally {
- writer.close();
- }
- return graphFile;
+ public static void main(String[] args) {
+ createPackageDependencyDiagram();
+ System.exit(0);
}
-
- private static String getGraphVizNodeForPackage(
- JavaPackage javaPackage) {
-
- String rootPackage = "com.basilv.examples.packagediagram";
- String packageName = javaPackage.getName();
- if (!packageName.startsWith(rootPackage)) {
- return null;
- }
-
- return packageName.replace(".", "_");
+
+ public static void createPackageDependencyDiagram() {
+ Collection<JavaPackage> packages = analyzePackages();
+ StringBuilder builder = generateGraph(packages);
+ generateImage("packages", builder.toString());
}
@SuppressWarnings("unchecked")
- public static void createPackageDependencyDiagram()
- throws Exception {
-
+ private static Collection<JavaPackage> analyzePackages() {
JDepend jdepend = new JDepend();
- jdepend.addDirectory("dist/classes");
+ try {
+ jdepend.addDirectory("dist/classes");
+ } catch (IOException e) {
+ throw new RuntimeException("Error adding directory for JDepend to analyze.", e);
+ }
Collection<JavaPackage> packages = jdepend.analyze();
+ return packages;
+ }
+ private static StringBuilder generateGraph(
+ Collection<JavaPackage> packages) {
StringBuilder builder = new StringBuilder();
builder.append("digraph packages {").append("\n");
builder.append("node [shape=box];").append("\n");
@@ -70,7 +44,9 @@ public static void createPackageDependencyDiagram()
}
builder.append(packageNodeName).append("\n");
+ @SuppressWarnings("unchecked")
Collection<JavaPackage> dependencies = javaPackage.getEfferents();
+
for (JavaPackage dependency : dependencies) {
String dependencyNodeName = getGraphVizNodeForPackage(dependency);
if (dependencyNodeName == null
@@ -88,12 +64,54 @@ public static void createPackageDependencyDiagram()
}
}
builder.append("}\n");
+ return builder;
+ }
- generateImage("packages", builder.toString());
+ private static String getGraphVizNodeForPackage(
+ JavaPackage javaPackage) {
+
+ String rootPackage = "com.basilv.examples.packagediagram";
+ String packageName = javaPackage.getName();
+ if (!packageName.startsWith(rootPackage)) {
+ return null;
+ }
+
+ return packageName.replace(".", "_");
}
- public static void main(String[] args) throws Exception {
- createPackageDependencyDiagram();
- System.exit(0);
+ private static void generateImage(String fileName,
+ String graphVizDotFormattedGraph) {
+ try {
+ File graphFile = createFileWithContents(fileName
+ + ".txt", graphVizDotFormattedGraph);
+
+ // This requires the GraphViz software to be installed -
+ // see http://graphviz.org/
+ String imageFileLocation = fileName + ".png";
+ Runtime.getRuntime().exec(
+ "dot -v -Tpng " + graphFile.getName() + " -o "
+ + imageFileLocation);
+
+ System.out.println("Image file available at "
+ + new File(imageFileLocation).getAbsolutePath());
+ } catch (IOException e) {
+ throw new RuntimeException("Error generating image " + fileName, e);
+ }
}
+
+ private static File createFileWithContents(
+ String fileName, String graphVizDotFormattedGraph)
+ throws IOException {
+ File graphFile = new File(fileName);
+ FileWriter writer = new FileWriter(graphFile, false);
+ try {
+ writer.append(graphVizDotFormattedGraph);
+ } finally {
+ writer.close();
+ }
+ return graphFile;
+ }
+
}
+
+

0 comments on commit 57dee96

Please sign in to comment.