KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > diff > Report


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.diff;
34
35 import java.util.*;
36
37 import org.apache.log4j.*;
38
39 import com.jeantessier.classreader.*;
40
41 public class Report extends Printer {
42     public static final String JavaDoc DEFAULT_ENCODING = "utf-8";
43     public static final String JavaDoc DEFAULT_DTD_PREFIX = "http://depfind.sourceforge.net/dtd";
44
45     private String JavaDoc name;
46     private String JavaDoc oldVersion;
47     private String JavaDoc newVersion;
48
49     private Collection removedPackages = new TreeSet();
50     private Collection undocumentedPackages = new TreeSet();
51
52     private Collection removedInterfaces = new TreeSet();
53     private Collection removedClasses = new TreeSet();
54
55     private Collection deprecatedInterfaces = new TreeSet();
56     private Collection deprecatedClasses = new TreeSet();
57     
58     private Collection undocumentedInterfaces = new TreeSet();
59     private Collection undocumentedClasses = new TreeSet();
60
61     private Collection modifiedInterfaces = new TreeSet();
62     private Collection modifiedClasses = new TreeSet();
63
64     private Collection documentedInterfaces = new TreeSet();
65     private Collection documentedClasses = new TreeSet();
66     
67     private Collection undeprecatedInterfaces = new TreeSet();
68     private Collection undeprecatedClasses = new TreeSet();
69     
70     private Collection newPackages = new TreeSet();
71     private Collection documentedPackages = new TreeSet();
72
73     private Collection newInterfaces = new TreeSet();
74     private Collection newClasses = new TreeSet();
75
76     public Report() {
77         this(DEFAULT_ENCODING, DEFAULT_DTD_PREFIX);
78     }
79     
80     public Report(String JavaDoc encoding, String JavaDoc dtdPrefix) {
81         appendHeader(encoding, dtdPrefix);
82     }
83
84     private void appendHeader(String JavaDoc encoding, String JavaDoc dtdPrefix) {
85         append("<?xml version=\"1.0\" encoding=\"").append(encoding).append("\" ?>").eol();
86         eol();
87         append("<!DOCTYPE differences SYSTEM \"").append(dtdPrefix).append("/differences.dtd\">").eol();
88         eol();
89     }
90
91     public void visitJarDifferences(JarDifferences differences) {
92         name = differences.getName();
93         oldVersion = differences.getOldVersion();
94         newVersion = differences.getNewVersion();
95
96         Iterator i = differences.getPackageDifferences().iterator();
97         while (i.hasNext()) {
98             ((Differences) i.next()).accept(this);
99         }
100     }
101
102     public void visitPackageDifferences(PackageDifferences differences) {
103         if (differences.isRemoved()) {
104             removedPackages.add(differences);
105         }
106     
107         Iterator i = differences.getClassDifferences().iterator();
108         while (i.hasNext()) {
109             ((Differences) i.next()).accept(this);
110         }
111
112         if (differences.isNew()) {
113             newPackages.add(differences);
114         }
115
116         if (isDocumented()) {
117             documentedPackages.add(differences);
118         }
119
120         if (isUndocumented()) {
121             undocumentedPackages.add(differences);
122         }
123     }
124
125     public void visitClassDifferences(ClassDifferences differences) {
126         if (differences.isRemoved()) {
127             removedClasses.add(differences);
128         }
129     
130         if (differences.isModified()) {
131             ClassReport visitor = new ClassReport();
132             visitor.setIndentText(getIndentText());
133             differences.accept(visitor);
134             modifiedClasses.add(visitor);
135         }
136     
137         if (differences.isNew()) {
138             newClasses.add(differences);
139         }
140
141         if (isDeprecated()) {
142             deprecatedClasses.add(differences);
143         }
144
145         if (isUndeprecated()) {
146             undeprecatedClasses.add(differences);
147         }
148
149         if (isDocumented()) {
150             documentedClasses.add(differences);
151         }
152
153         if (isUndocumented()) {
154             undocumentedClasses.add(differences);
155         }
156     }
157
158     public void visitInterfaceDifferences(InterfaceDifferences differences) {
159         if (differences.isRemoved()) {
160             removedInterfaces.add(differences);
161         }
162     
163         if (differences.isModified()) {
164             ClassReport visitor = new ClassReport();
165             visitor.setIndentText(getIndentText());
166             differences.accept(visitor);
167             modifiedInterfaces.add(visitor);
168         }
169     
170         if (differences.isNew()) {
171             newInterfaces.add(differences);
172         }
173
174         if (isDeprecated()) {
175             deprecatedInterfaces.add(differences);
176         }
177
178         if (isUndeprecated()) {
179             undeprecatedInterfaces.add(differences);
180         }
181
182         if (isDocumented()) {
183             documentedInterfaces.add(differences);
184         }
185
186         if (isUndocumented()) {
187             undocumentedInterfaces.add(differences);
188         }
189     }
190
191     public String JavaDoc toString() {
192         indent().append("<differences>").eol();
193         raiseIndent();
194
195         indent().append("<name>").append(name).append("</name>").eol();
196         indent().append("<old>").append(oldVersion).append("</old>").eol();
197         indent().append("<new>").append(newVersion).append("</new>").eol();
198     
199         if (removedPackages.size() !=0) {
200             indent().append("<removed-packages>").eol();
201             raiseIndent();
202
203             Iterator i = removedPackages.iterator();
204             while (i.hasNext()) {
205                 indent().append("<name>").append(i.next()).append("</name>").eol();
206             }
207
208             lowerIndent();
209             indent().append("</removed-packages>").eol();
210         }
211     
212         if (undocumentedPackages.size() !=0) {
213             indent().append("<undocumented-packages>").eol();
214             raiseIndent();
215
216             Iterator i = undocumentedPackages.iterator();
217             while (i.hasNext()) {
218                 indent().append("<name>").append(i.next()).append("</name>").eol();
219             }
220
221             lowerIndent();
222             indent().append("</undocumented-packages>").eol();
223         }
224
225         if (removedInterfaces.size() !=0) {
226             indent().append("<removed-interfaces>").eol();
227             raiseIndent();
228
229             Iterator i = removedInterfaces.iterator();
230             while (i.hasNext()) {
231                 ClassDifferences cd = (ClassDifferences) i.next();
232                 indent().append("<name").append(breakdownDeclaration(cd.getOldClass())).append(">").append(cd).append("</name>").eol();
233             }
234
235             lowerIndent();
236             indent().append("</removed-interfaces>").eol();
237         }
238
239         if (removedClasses.size() !=0) {
240             indent().append("<removed-classes>").eol();
241             raiseIndent();
242
243             Iterator i = removedClasses.iterator();
244             while (i.hasNext()) {
245                 ClassDifferences cd = (ClassDifferences) i.next();
246                 indent().append("<name").append(breakdownDeclaration(cd.getOldClass())).append(">").append(cd).append("</name>").eol();
247             }
248
249             lowerIndent();
250             indent().append("</removed-classes>").eol();
251         }
252
253         if (deprecatedInterfaces.size() !=0) {
254             indent().append("<deprecated-interfaces>").eol();
255             raiseIndent();
256
257             Iterator i = deprecatedInterfaces.iterator();
258             while (i.hasNext()) {
259                 ClassDifferences cd = (ClassDifferences) i.next();
260                 indent().append("<name").append(breakdownDeclaration(cd.getNewClass())).append(">").append(cd).append("</name>").eol();
261             }
262
263             lowerIndent();
264             indent().append("</deprecated-interfaces>").eol();
265         }
266
267         if (deprecatedClasses.size() !=0) {
268             indent().append("<deprecated-classes>").eol();
269             raiseIndent();
270
271             Iterator i = deprecatedClasses.iterator();
272             while (i.hasNext()) {
273                 ClassDifferences cd = (ClassDifferences) i.next();
274                 indent().append("<name").append(breakdownDeclaration(cd.getNewClass())).append(">").append(cd).append("</name>").eol();
275             }
276
277             lowerIndent();
278             indent().append("</deprecated-classes>").eol();
279         }
280
281         if (undocumentedInterfaces.size() !=0) {
282             indent().append("<undocumented-interfaces>").eol();
283             raiseIndent();
284
285             Iterator i = undocumentedInterfaces.iterator();
286             while (i.hasNext()) {
287                 ClassDifferences cd = (ClassDifferences) i.next();
288                 indent().append("<name").append(breakdownDeclaration(cd.getNewClass())).append(">").append(cd).append("</name>").eol();
289             }
290
291             lowerIndent();
292             indent().append("</undocumented-interfaces>").eol();
293         }
294
295         if (undocumentedClasses.size() !=0) {
296             indent().append("<undocumented-classes>").eol();
297             raiseIndent();
298
299             Iterator i = undocumentedClasses.iterator();
300             while (i.hasNext()) {
301                 ClassDifferences cd = (ClassDifferences) i.next();
302                 indent().append("<name").append(breakdownDeclaration(cd.getNewClass())).append(">").append(cd).append("</name>").eol();
303             }
304
305             lowerIndent();
306             indent().append("</undocumented-classes>").eol();
307         }
308
309         if (modifiedInterfaces.size() !=0) {
310             indent().append("<modified-interfaces>").eol();
311             raiseIndent();
312
313             Iterator i = modifiedInterfaces.iterator();
314             while (i.hasNext()) {
315                 append(i.next());
316             }
317
318             lowerIndent();
319             indent().append("</modified-interfaces>").eol();
320         }
321
322         if (modifiedClasses.size() !=0) {
323             indent().append("<modified-classes>").eol();
324             raiseIndent();
325
326             Iterator i = modifiedClasses.iterator();
327             while (i.hasNext()) {
328                 append(i.next());
329             }
330
331             lowerIndent();
332             indent().append("</modified-classes>").eol();
333         }
334
335         if (documentedInterfaces.size() !=0) {
336             indent().append("<documented-interfaces>").eol();
337             raiseIndent();
338
339             Iterator i = documentedInterfaces.iterator();
340             while (i.hasNext()) {
341                 ClassDifferences cd = (ClassDifferences) i.next();
342                 indent().append("<name").append(breakdownDeclaration(cd.getNewClass())).append(">").append(cd).append("</name>").eol();
343             }
344
345             lowerIndent();
346             indent().append("</documented-interfaces>").eol();
347         }
348
349         if (documentedClasses.size() !=0) {
350             indent().append("<documented-classes>").eol();
351             raiseIndent();
352
353             Iterator i = documentedClasses.iterator();
354             while (i.hasNext()) {
355                 ClassDifferences cd = (ClassDifferences) i.next();
356                 indent().append("<name").append(breakdownDeclaration(cd.getNewClass())).append(">").append(cd).append("</name>").eol();
357             }
358
359             lowerIndent();
360             indent().append("</documented-classes>").eol();
361         }
362
363         if (undeprecatedInterfaces.size() !=0) {
364             indent().append("<undeprecated-interfaces>").eol();
365             raiseIndent();
366
367             Iterator i = undeprecatedInterfaces.iterator();
368             while (i.hasNext()) {
369                 ClassDifferences cd = (ClassDifferences) i.next();
370                 indent().append("<name").append(breakdownDeclaration(cd.getNewClass())).append(">").append(cd).append("</name>").eol();
371             }
372
373             lowerIndent();
374             indent().append("</undeprecated-interfaces>").eol();
375         }
376
377         if (undeprecatedClasses.size() !=0) {
378             indent().append("<undeprecated-classes>").eol();
379             raiseIndent();
380
381             Iterator i = undeprecatedClasses.iterator();
382             while (i.hasNext()) {
383                 ClassDifferences cd = (ClassDifferences) i.next();
384                 indent().append("<name").append(breakdownDeclaration(cd.getNewClass())).append(">").append(cd).append("</name>").eol();
385             }
386
387             lowerIndent();
388             indent().append("</undeprecated-classes>").eol();
389         }
390
391         if (newPackages.size() !=0) {
392             indent().append("<new-packages>").eol();
393             raiseIndent();
394
395             Iterator i = newPackages.iterator();
396             while (i.hasNext()) {
397                 indent().append("<name>").append(i.next()).append("</name>").eol();
398             }
399
400             lowerIndent();
401             indent().append("</new-packages>").eol();
402         }
403     
404         if (documentedPackages.size() !=0) {
405             indent().append("<documented-packages>").eol();
406             raiseIndent();
407
408             Iterator i = documentedPackages.iterator();
409             while (i.hasNext()) {
410                 indent().append("<name>").append(i.next()).append("</name>").eol();
411             }
412
413             lowerIndent();
414             indent().append("</documented-packages>").eol();
415         }
416
417         if (newInterfaces.size() !=0) {
418             indent().append("<new-interfaces>").eol();
419             raiseIndent();
420
421             Iterator i = newInterfaces.iterator();
422             while (i.hasNext()) {
423                 ClassDifferences cd = (ClassDifferences) i.next();
424                 indent().append("<name").append(breakdownDeclaration(cd.getNewClass())).append(">").append(cd).append("</name>").eol();
425             }
426
427             lowerIndent();
428             indent().append("</new-interfaces>").eol();
429         }
430
431         if (newClasses.size() !=0) {
432             indent().append("<new-classes>").eol();
433             raiseIndent();
434
435             Iterator i = newClasses.iterator();
436             while (i.hasNext()) {
437                 ClassDifferences cd = (ClassDifferences) i.next();
438                 indent().append("<name").append(breakdownDeclaration(cd.getNewClass())).append(">").append(cd).append("</name>").eol();
439             }
440
441             lowerIndent();
442             indent().append("</new-classes>").eol();
443         }
444
445         lowerIndent();
446         indent().append("</differences>").eol();
447
448         return super.toString();
449     }
450
451     private static final String JavaDoc breakdownDeclaration(Classfile element) {
452         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
453
454         if (element != null) {
455             if (element.isPublic()) result.append(" visibility=\"public\"");
456             if (element.isPackage()) result.append(" visibility=\"package\"");
457             if (element.isFinal()) result.append(" final=\"yes\"");
458             if (element.isSuper()) result.append(" super=\"yes\"");
459             if (element.isSynthetic()) result.append(" synthetic=\"yes\"");
460             if (element.isDeprecated()) result.append(" deprecated=\"yes\"");
461
462             result.append(" name=\"").append(element.getClassName()).append("\"");
463
464             if (element.isInterface()) {
465                 result.append(" interface=\"yes\"");
466         
467                 result.append(" extends=\"");
468                 Iterator i = element.getAllInterfaces().iterator();
469                 while (i.hasNext()) {
470                     result.append(i.next());
471                     if (i.hasNext()) {
472                         result.append(", ");
473                     }
474                 }
475                 result.append("\"");
476             } else {
477                 if (element.isAbstract()) result.append(" abstract=\"yes\"");
478         
479                 result.append(" extends=\"").append(element.getSuperclassName()).append("\"");
480         
481                 result.append(" implements=\"");
482                 Iterator i = element.getAllInterfaces().iterator();
483                 while (i.hasNext()) {
484                     result.append(i.next());
485                     if (i.hasNext()) {
486                         result.append(", ");
487                     }
488                 }
489                 result.append("\"");
490             }
491         }
492
493         return result.toString();
494     }
495 }
496
Popular Tags