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

package edu.umd.cs.findbugs;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

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 String stylesheet;

	public HTMLBugReporter(Project project, String stylesheet) {
		super(project);
		this.stylesheet = stylesheet;
	}

	public void finish() {
		try {
			BugCollection bugCollection = getBugCollection();
			bugCollection.setWithMessages(true);
			// Decorate the XML with messages to display
			Document document = bugCollection.toDocument(getProject());
			// new AddMessages(bugCollection, document).execute();

			// Get the stylesheet as a StreamSource.
			// First, try to load the stylesheet from the filesystem.
			// If that fails, try loading it as a resource.
			InputStream xslInputStream;
			if (FindBugs.DEBUG) System.out.println("Attempting to load stylesheet " + stylesheet);
			try {
				xslInputStream = new BufferedInputStream(new FileInputStream(stylesheet));
			} catch (FileNotFoundException fnfe) {
				xslInputStream = this.getClass().getClassLoader().getResourceAsStream(stylesheet);
				if (xslInputStream == null)
					throw new IOException("Could not load HTML generation stylesheet " + stylesheet);
			}
			StreamSource xsl = new StreamSource(xslInputStream);
			xsl.setSystemId(stylesheet);

			// Create a transformer using the stylesheet
			TransformerFactory factory = TransformerFactory.newInstance();
			Transformer transformer = factory.newTransformer(xsl);

			// Source document is the XML generated from the BugCollection
			DocumentSource source = new DocumentSource(document);

			// Write result to output stream
			StreamResult result = new StreamResult(outputStream);

			// Do the transformation
			transformer.transform(source, result);
		} catch (Exception e) {
			logError("Could not generate HTML output", e);
			if (FindBugs.DEBUG) e.printStackTrace();
		}
	}
}

// vim:ts=4

@@ -1025,16 +1025,37 @@
 tStream;
+%0Aimport java.net.URL;
 %0A%0Aimport
@@ -1387,16 +1387,54 @@
 lesheet;
+%0A    private Exception fatalException;
 %0A%0A%09publi
@@ -2027,423 +2027,31 @@
 ream
-;%0A%09%09%09if (FindBugs.DEBUG) System.out.println(%22Attempting to load stylesheet %22 + stylesheet);%0A%09%09%09try %7B%0A%09%09%09%09xslInputStream = new BufferedInputStream(new FileInputStream(stylesheet));%0A%09%09%09%7D catch (FileNotFoundException fnfe) %7B%0A%09%09%09%09xslInputStream = this.getClass().getClassLoader().getResourceAsStream(stylesheet);%0A%09%09%09%09if (xslInputStream == null)%0A%09%09%09%09%09throw new IOException(%22Could not load HTML generation stylesheet %22 + 
+ = getStylesheetStream(
 styl
@@ -2063,13 +2063,8 @@
 t);%0A
-%09%09%09%7D%0A
 %09%09%09S
@@ -2685,16 +2685,48 @@
 t%22, e);%0A
+            fatalException = e;%0A
 %09%09%09if (F
@@ -2765,18 +2765,903 @@
 ();%0A%09%09%7D%0A
-
 %09%7D
+%0A%0A    public Exception getFatalException() %7B%0A        return fatalException;%0A    %7D%0A    private static InputStream getStylesheetStream(String stylesheet) throws IOException %7B%0A        if (FindBugs.DEBUG) System.out.println(%22Attempting to load stylesheet %22 + stylesheet);%0A        try %7B%0A            URL u = new URL(stylesheet);%0A            return u.openStream();%0A        %7D catch (Exception e) %7B%0A            assert true; // ignore it%0A        %7D%0A        try %7B%0A        %09return  new BufferedInputStream(new FileInputStream(stylesheet));%0A        %7D catch (Exception fnfe) %7B%0A            assert true; // ignore it%0A        %7D%0A        InputStream xslInputStream = HTMLBugReporter.class.getResourceAsStream(%22/%22+stylesheet);%0A        if (xslInputStream == null) %7B%0A            throw new IOException(%22Could not load HTML generation stylesheet %22 +  stylesheet);%0A        %7D%0A        return xslInputStream;%0A    %7D
 %0A%7D%0A%0A// v

Allow stylesheets to be URL's Fix [ 1647400 ] convertXmlToText using -fancy.xsl only creates empty file