KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > kirkk > analyzer > textui > DOTSummary


1 package com.kirkk.analyzer.textui;
2
3
4 import java.io.*;
5 import java.util.*;
6 import com.kirkk.analyzer.framework.*;
7 import com.kirkk.analyzer.*;
8
9 /*
10  * Modified version of XMLUISummary
11  * http://www.kirkk.com/wiki/wiki.php/Main/JarAnalyzer
12  *
13  * This class takes the input directory and output file
14  * from the command line or from System.in. It is also
15  * usable directly from Java and can be called from
16  * an ant task
17  */

18 public class DOTSummary implements Summary {
19     private PrintWriter writer;
20
21     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
22         new DOTSummary().instanceMain(args);
23     }
24
25     public void instanceMain(String JavaDoc args[]) throws Exception JavaDoc {
26         File scrDir = (args.length > 0) ? new File(args[0]) : null;
27         File destFile = (args.length > 1) ? new File(args[1]) : null;
28         if(scrDir == null)
29         {
30             System.out.print("Please enter input directory name: ");
31             scrDir = getFile();
32         }
33         if(destFile == null)
34         {
35             System.out.print("Please enter output file name: ");
36             destFile = getFile();
37         }
38         createSummary(scrDir, destFile);
39     }
40
41     public void createSummary(File srcDir, File destFile) throws Exception JavaDoc
42     {
43         Analyzer analyzer = new Analyzer();
44         Jar jarBundle[] = analyzer.analyze(srcDir);
45         outputAll(jarBundle, destFile);
46     }
47
48     public void createSummary(File srcDir, File destFile, String JavaDoc packageFilter, String JavaDoc jarFilter) throws Exception JavaDoc {
49         Analyzer analyzer = new Analyzer();
50         Configuration.initialize(packageFilter, jarFilter);
51         Jar jarBundle[] = analyzer.analyze(srcDir);
52         outputAll(jarBundle, destFile);
53     }
54
55     private File getFile() throws IOException {
56         try {
57             InputStreamReader streamReader = new InputStreamReader(System.in);
58             BufferedReader reader = new BufferedReader(streamReader);
59             String JavaDoc fileName = reader.readLine();
60             File file = new File(fileName);
61             return file;
62         } catch (IOException e) {
63             throw e;
64         }
65     }
66
67     private void outputAll(Jar[] jarBundle, File file) {
68         try {
69             FileWriter fileWriter = new FileWriter(file);
70             writer = new PrintWriter(fileWriter);
71         } catch (IOException e) {
72             System.out.println("IOException - Redirecting to System.out");
73             System.out.println(e);
74             OutputStreamWriter outputWriter = new OutputStreamWriter(System.out);
75             writer = new PrintWriter(outputWriter);
76         }
77         this.printHeader();
78         this.output(jarBundle);
79         this.printFooter();
80         writer.flush();
81         writer.close();
82     }
83
84     private void printHeader() {
85         writer.println("digraph G {");
86         //writer.println("node[shape=box,width=1.5,height=1.25,fontsize=30];");
87
}
88
89     private void printFooter() {
90         writer.println("}");
91     }
92
93     private void output(Jar[] jarBundles) {
94         //writer.println(tab()+"<Jars>");writer.println();
95
for (int i = 0; i < jarBundles.length; i++) {
96             String JavaDoc jar = jarBundles[i].getJarFileName().substring(jarBundles[i].getJarFileName().lastIndexOf("\\") + 1,
97                                     jarBundles[i].getJarFileName().length());
98             //writer.println(tab(2)+"<Jar name=\"" + jar + "\">");
99
//writer.println(tab(3)+"<Summary>");
100
//this.statistics(jarBundles[i]);writer.println();
101
//this.jarPackages(jarBundles[i]);writer.println();
102
this.externalJars(jarBundles[i]);
103             //this.unresolveableDependencies(jarBundles[i]);
104
//writer.println(tab(3)+"</Summary>");writer.println();
105
//writer.println(tab(2)+"</Jar>");writer.println();
106
}
107         //writer.println(tab()+"</Jars>");
108
}
109
110     /*private void statistics(Jar jarBundle) {
111         writer.println(tab(4)+"<Statistics>");
112         writer.println(tab(5)+"<ClassCount>" + jarBundle.getPublicClassCount() + "</ClassCount>");
113         writer.println(tab(5)+"<PackageCount>" + jarBundle.getPackageCount() + "</PackageCount>");
114         writer.println(tab(5)+"<Level>" + jarBundle.getLevel() + "</Level>");
115         writer.println(tab(4)+"</Statistics>");
116     }*/

117     private void externalJars(Jar jarBundle) {
118         //writer.println(tab(4)+"<ResolvedDependencies>");
119
Iterator jarDependencies = jarBundle.getOutgoingDependencies().iterator();
120         while (jarDependencies.hasNext()) {
121             String JavaDoc jarName = jarBundle.getJarFileName().replaceAll("[-.]","_");
122             writer.print(tab() + jarName.substring(0, jarName.length() - 4));
123             Jar dependentBundle = (Jar) jarDependencies.next();
124             //String jar2 = dependentBundle.getJarFileName().substring(dependentBundle.getJarFileName().lastIndexOf("\\") + 1,
125
// dependentBundle.getJarFileName().length());
126
String JavaDoc jar2= dependentBundle.getJarFileName().replaceAll("[-.]","_");
127             writer.println(" -> " + jar2.substring(0, jar2.length() - 4) + ";");
128         }
129         //writer.println(tab(4)+"</ResolvedDependencies>");writer.println();
130
}
131
132
133     /*private void externalDependencies(Jar jarBundle) {
134         Iterator allPackages = jarBundle.getAllExternallyReferencedPackages().iterator();
135         while (allPackages.hasNext()) {
136             String javaPackage = (String) allPackages.next();
137             writer.println(tab(5)+"<Package>"+javaPackage+"</Package>");
138         }
139     }*/

140
141     /*private void unresolveableDependencies(Jar jarBundle) {
142         writer.println(tab(4)+"<UnresolvedDependencies>");
143         Iterator unresolvedPackages = jarBundle.getAllUnidentifiableExternallyReferencedPackages().iterator();
144         while (unresolvedPackages.hasNext()) {
145             String packageName = (String) unresolvedPackages.next();
146             writer.println(tab(5)+"<Package>"+packageName+"</Package>");
147         }
148         writer.println(tab(4)+"</UnresolvedDependencies>");
149     }*/

150
151     /*private void jarPackages(Jar jarBundle) {
152         writer.println(tab(4)+"<Packages>");
153         Iterator allPackages = jarBundle.getAllContainedPackages().iterator();
154         while (allPackages.hasNext()) {
155             JavaPackage javaPackage = (JavaPackage) allPackages.next();
156             writer.println(tab(5)+"<Package>" + javaPackage.getLongName() + "</Package>");
157         }
158         writer.println(tab(4)+"</Packages>");
159     }*/

160
161     private String JavaDoc tab() {
162         return " ";
163     }
164
165     private String JavaDoc tab(int i) {
166         String JavaDoc tab = tab();
167         for (int j = 0; j < i - 1; j++) {
168             tab = tab + tab();
169         }
170         return tab;
171     }
172
173 }
Popular Tags