package edu.umd.cs.findbugs;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.dom4j.Document;
import org.dom4j.io.DocumentSource;
public class HTMLBugReporter extends BugCollectionBugReporter {
private final String stylesheet;
private Exception fatalException;
public HTMLBugReporter(Project project, String stylesheet) {
super(project);
this.stylesheet = stylesheet;
}
@Override
public void finish() {
try {
BugCollection bugCollection = getBugCollection();
bugCollection.setWithMessages(true);
Document document = bugCollection.toDocument();
InputStream xslInputStream = getStylesheetStream(stylesheet);
StreamSource xsl = new StreamSource(xslInputStream);
xsl.setSystemId(stylesheet);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(xsl);
DocumentSource source = new DocumentSource(document);
StreamResult result = new StreamResult(outputStream);
transformer.transform(source, result);
} catch (Exception e) {
logError("Could not generate HTML output", e);
fatalException = e;
if (FindBugs.DEBUG) {
e.printStackTrace();
}
}
outputStream.close();
}
public Exception getFatalException() {
return fatalException;
}
private static InputStream getStylesheetStream(String stylesheet) throws IOException {
if (FindBugs.DEBUG) {
System.out.println("Attempting to load stylesheet " + stylesheet);
}
try {
URL u = new URL(stylesheet);
return u.openStream();
} catch (Exception e) {
assert true;
}
try {
return new BufferedInputStream(new FileInputStream(stylesheet));
} catch (Exception fnfe) {
assert true;
}
InputStream xslInputStream = HTMLBugReporter.class.getResourceAsStream("/" + stylesheet);
if (xslInputStream == null) {
throw new IOException("Could not load HTML generation stylesheet " + stylesheet);
}
return xslInputStream;
}
}