KickJava   Java API By Example, From Geeks To Geeks.

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


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 XMLUISummary implements Summary {
19     private PrintWriter writer;
20
21     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
22         new XMLUISummary().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("<?xml version=\"1.0\"?>");
86         writer.println("<JarAnalyzer>");
87     }
88
89     private void printFooter() {
90         writer.println("</JarAnalyzer>");
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.metrics(jarBundles[i].calculateMetrics());writer.println();
102             this.jarPackages(jarBundles[i]);writer.println();
103             this.outgoingDependencies(jarBundles[i]);
104             this.incomingDependencies(jarBundles[i]);
105             this.cycles(jarBundles[i]);
106             this.unresolveableDependencies(jarBundles[i]);
107             writer.println(tab(3)+"</Summary>");writer.println();
108             writer.println(tab(2)+"</Jar>");writer.println();
109         }
110         writer.println(tab()+"</Jars>");
111     }
112
113     private void statistics(Jar jarBundle) {
114         writer.println(tab(4)+"<Statistics>");
115         writer.println(tab(5)+"<ClassCount>" + jarBundle.getClassCount() + "</ClassCount>");
116         writer.println(tab(5)+"<AbstractClassCount>" + jarBundle.getAbstractClassCount() + "</AbstractClassCount>");
117         writer.println(tab(5)+"<PackageCount>" + jarBundle.getPackageCount() + "</PackageCount>");
118         //writer.println(tab(5)+"<Level>" + jarBundle.getLevel() + "</Level>");
119
writer.println(tab(4)+"</Statistics>");
120     }
121
122     private void metrics(JarMetrics jarMetrics) {
123         writer.println(tab(4)+"<Metrics>");
124         writer.println(tab(5)+"<Abstractness>" + jarMetrics.calculateAbstractness().toString() + "</Abstractness>");
125         writer.println(tab(5)+"<Efferent>" + jarMetrics.calculateEfferentCoupling() + "</Efferent>");
126         writer.println(tab(5)+"<Afferent>" + jarMetrics.calculateAfferentCoupling() + "</Afferent>");
127         writer.println(tab(5)+"<Instability>" + jarMetrics.calculateInstability().toString() + "</Instability>");
128         writer.println(tab(5)+"<Distance>" + jarMetrics.calculateDistance().toString() + "</Distance>");
129         writer.println(tab(4)+"</Metrics>");
130     }
131
132     private void cycles(Jar jarBundle) {
133         writer.println(tab(4) + "<Cycles>");
134         if (jarBundle.hasCycles()) {
135             Iterator cyclicJars = jarBundle.getCyclicJars().iterator();
136             while (cyclicJars.hasNext()) {
137                 Jar cyclicBundle = (Jar) cyclicJars.next();
138                 String JavaDoc jarName = cyclicBundle.getJarFileName();
139                 writer.println(tab(5) + "<Cycle>" + jarName + "</Cycle>");
140             }
141         }
142         writer.println(tab(4) + "</Cycles>");writer.println();
143     }
144
145     private void outgoingDependencies(Jar jarBundle) {
146         writer.println(tab(4)+"<OutgoingDependencies>");
147         Iterator jarDependencies = jarBundle.getOutgoingDependencies().iterator();
148         while (jarDependencies.hasNext()) {
149             Jar dependentBundle = (Jar) jarDependencies.next();
150             //String jar2 = dependentBundle.getJarFileName().substring(dependentBundle.getJarFileName().lastIndexOf("\\") + 1,
151
// dependentBundle.getJarFileName().length());
152
String JavaDoc jar2= dependentBundle.getJarFileName();
153             writer.println(tab(5)+"<Jar>"+jar2+"</Jar>");
154         }
155         writer.println(tab(4)+"</OutgoingDependencies>");writer.println();
156     }
157
158     private void incomingDependencies(Jar jarBundle) {
159         writer.println(tab(4)+"<IncomingDependencies>");
160         Iterator jarDependencies = jarBundle.getIncomingDependencies().iterator();
161         while (jarDependencies.hasNext()) {
162             Jar dependentBundle = (Jar) jarDependencies.next();
163             //String jar2 = dependentBundle.getJarFileName().substring(dependentBundle.getJarFileName().lastIndexOf("\\") + 1,
164
// dependentBundle.getJarFileName().length());
165
String JavaDoc jar2= dependentBundle.getJarFileName();
166             writer.println(tab(5)+"<Jar>"+jar2+"</Jar>");
167         }
168         writer.println(tab(4)+"</IncomingDependencies>");writer.println();
169     }
170
171
172     private void externalDependencies(Jar jarBundle) {
173         Iterator allPackages = jarBundle.getAllExternallyReferencedPackages().iterator();
174         while (allPackages.hasNext()) {
175             String JavaDoc javaPackage = (String JavaDoc) allPackages.next();
176             writer.println(tab(5)+"<Package>"+javaPackage+"</Package>");
177         }
178     }
179
180     private void unresolveableDependencies(Jar jarBundle) {
181         writer.println(tab(4)+"<UnresolvedDependencies>");
182         Iterator unresolvedPackages = jarBundle.getAllUnidentifiableExternallyReferencedPackages().iterator();
183         while (unresolvedPackages.hasNext()) {
184             String JavaDoc packageName = (String JavaDoc) unresolvedPackages.next();
185             writer.println(tab(5)+"<Package>"+packageName+"</Package>");
186         }
187         writer.println(tab(4)+"</UnresolvedDependencies>");
188     }
189
190     private void jarPackages(Jar jarBundle) {
191         writer.println(tab(4)+"<Packages>");
192         Iterator allPackages = jarBundle.getAllContainedPackages().iterator();
193         while (allPackages.hasNext()) {
194             JarPackage javaPackage = (JarPackage) allPackages.next();
195             writer.println(tab(5)+"<Package>" + javaPackage.getLongName() + "</Package>");
196         }
197         writer.println(tab(4)+"</Packages>");
198     }
199
200     private String JavaDoc tab() {
201         return " ";
202     }
203
204     private String JavaDoc tab(int i) {
205         String JavaDoc tab = tab();
206         for (int j = 0; j < i - 1; j++) {
207             tab = tab + tab();
208         }
209         return tab;
210     }
211
212 }
Popular Tags