001: /*
002: * FindBugs - Find bugs in Java programs
003: * Copyright (C) 2003-2005 University of Maryland
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package edu.umd.cs.findbugs;
021:
022: import java.io.BufferedInputStream;
023: import java.io.FileInputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.net.URL;
027:
028: import javax.xml.transform.Transformer;
029: import javax.xml.transform.TransformerFactory;
030: import javax.xml.transform.stream.StreamResult;
031: import javax.xml.transform.stream.StreamSource;
032:
033: import org.dom4j.Document;
034: import org.dom4j.io.DocumentSource;
035:
036: public class HTMLBugReporter extends BugCollectionBugReporter {
037: private String stylesheet;
038: private Exception fatalException;
039:
040: public HTMLBugReporter(Project project, String stylesheet) {
041: super (project);
042: this .stylesheet = stylesheet;
043: }
044:
045: @Override
046: public void finish() {
047: try {
048: BugCollection bugCollection = getBugCollection();
049: bugCollection.setWithMessages(true);
050: // Decorate the XML with messages to display
051: Document document = bugCollection.toDocument(getProject());
052: // new AddMessages(bugCollection, document).execute();
053:
054: // Get the stylesheet as a StreamSource.
055: // First, try to load the stylesheet from the filesystem.
056: // If that fails, try loading it as a resource.
057: InputStream xslInputStream = getStylesheetStream(stylesheet);
058: StreamSource xsl = new StreamSource(xslInputStream);
059: xsl.setSystemId(stylesheet);
060:
061: // Create a transformer using the stylesheet
062: TransformerFactory factory = TransformerFactory
063: .newInstance();
064: Transformer transformer = factory.newTransformer(xsl);
065:
066: // Source document is the XML generated from the BugCollection
067: DocumentSource source = new DocumentSource(document);
068:
069: // Write result to output stream
070: StreamResult result = new StreamResult(outputStream);
071:
072: // Do the transformation
073: transformer.transform(source, result);
074: } catch (Exception e) {
075: logError("Could not generate HTML output", e);
076: fatalException = e;
077: if (FindBugs.DEBUG)
078: e.printStackTrace();
079: }
080: }
081:
082: public Exception getFatalException() {
083: return fatalException;
084: }
085:
086: private static InputStream getStylesheetStream(String stylesheet)
087: throws IOException {
088: if (FindBugs.DEBUG)
089: System.out.println("Attempting to load stylesheet "
090: + stylesheet);
091: try {
092: URL u = new URL(stylesheet);
093: return u.openStream();
094: } catch (Exception e) {
095: assert true; // ignore it
096: }
097: try {
098: return new BufferedInputStream(new FileInputStream(
099: stylesheet));
100: } catch (Exception fnfe) {
101: assert true; // ignore it
102: }
103: InputStream xslInputStream = HTMLBugReporter.class
104: .getResourceAsStream("/" + stylesheet);
105: if (xslInputStream == null) {
106: throw new IOException(
107: "Could not load HTML generation stylesheet "
108: + stylesheet);
109: }
110: return xslInputStream;
111: }
112: }
113:
114: // vim:ts=4
|