KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > HTMLBugReporter


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27
28 import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.stream.StreamResult JavaDoc;
31 import javax.xml.transform.stream.StreamSource JavaDoc;
32
33 import org.dom4j.Document;
34 import org.dom4j.io.DocumentSource;
35
36 public class HTMLBugReporter extends BugCollectionBugReporter {
37     private String JavaDoc stylesheet;
38
39     public HTMLBugReporter(Project project, String JavaDoc stylesheet) {
40         super(project);
41         this.stylesheet = stylesheet;
42     }
43
44     public void finish() {
45         try {
46             BugCollection bugCollection = getBugCollection();
47             bugCollection.setWithMessages(true);
48             // Decorate the XML with messages to display
49
Document document = bugCollection.toDocument(getProject());
50             // new AddMessages(bugCollection, document).execute();
51

52             // Get the stylesheet as a StreamSource.
53
// First, try to load the stylesheet from the filesystem.
54
// If that fails, try loading it as a resource.
55
InputStream JavaDoc xslInputStream;
56             if (FindBugs.DEBUG) System.out.println("Attempting to load stylesheet " + stylesheet);
57             try {
58                 xslInputStream = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(stylesheet));
59             } catch (FileNotFoundException JavaDoc fnfe) {
60                 xslInputStream = this.getClass().getClassLoader().getResourceAsStream(stylesheet);
61                 if (xslInputStream == null)
62                     throw new IOException JavaDoc("Could not load HTML generation stylesheet " + stylesheet);
63             }
64             StreamSource JavaDoc xsl = new StreamSource JavaDoc(xslInputStream);
65             xsl.setSystemId(stylesheet);
66
67             // Create a transformer using the stylesheet
68
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
69             Transformer JavaDoc transformer = factory.newTransformer(xsl);
70
71             // Source document is the XML generated from the BugCollection
72
DocumentSource source = new DocumentSource(document);
73
74             // Write result to output stream
75
StreamResult JavaDoc result = new StreamResult JavaDoc(outputStream);
76
77             // Do the transformation
78
transformer.transform(source, result);
79         } catch (Exception JavaDoc e) {
80             logError("Could not generate HTML output", e);
81             if (FindBugs.DEBUG) e.printStackTrace();
82         }
83     }
84 }
85
86 // vim:ts=4
87
Popular Tags