Skip to content

Commit

Permalink
Extract BugCollectionExporter Interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jqyp committed Aug 20, 2020
1 parent a662704 commit d44ef95
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2020 SpotBugs plugin contributors
*
* This file is part of IntelliJ SpotBugs plugin.
*
* IntelliJ SpotBugs plugin is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* IntelliJ SpotBugs plugin 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with IntelliJ SpotBugs plugin.
* If not, see <http://www.gnu.org/licenses/>.
*/

package org.jetbrains.plugins.spotbugs.actions;

import com.intellij.openapi.diagnostic.Logger;
import edu.umd.cs.findbugs.BugCollection;
import org.jetbrains.annotations.NotNull;

import javax.xml.transform.TransformerException;
import java.io.File;
import java.io.IOException;

public interface BugCollectionExporter {
Logger LOGGER = Logger.getInstance(ExportBugCollection.class);

void export(@NotNull final BugCollection bugCollection, @NotNull final File file) throws IOException, TransformerException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@

import com.intellij.ide.BrowserUtil;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.progress.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.wm.ToolWindow;
import edu.umd.cs.findbugs.*;
import net.sf.saxon.TransformerFactoryImpl;
import org.dom4j.Document;
import org.dom4j.io.DocumentSource;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.spotbugs.common.EventDispatchThreadHelper;
import org.jetbrains.plugins.spotbugs.common.util.*;
Expand All @@ -39,19 +35,12 @@
import org.jetbrains.plugins.spotbugs.gui.toolwindow.view.ToolWindowPanel;
import org.jetbrains.plugins.spotbugs.resources.ResourcesLoader;

import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;

public final class ExportBugCollection extends AbstractAction {

private static final Logger LOGGER = Logger.getInstance(ExportBugCollection.class);
private static final String FINDBUGS_PLAIN_XSL = "plain.xsl";

@Override
void updateImpl(
@NotNull final AnActionEvent e,
Expand Down Expand Up @@ -168,11 +157,13 @@ private void exportImpl(
bugCollection.setWithMessages(true);
if (exportXml) {
final File xml = new File(exportDir, fileName + ".xml");
exportXml(bugCollection, xml.getPath());
XmlBugCollectionExporter xmlExporter = new XmlBugCollectionExporter();
xmlExporter.export(bugCollection, xml);
}
if (exportHtml) {
final File html = new File(exportDir, fileName + ".html");
exportHtml(bugCollection, html);
HtmlBugCollectionExporter htmlExporter = new HtmlBugCollectionExporter();
htmlExporter.export(bugCollection, html);
if (openInBrowser) {
openInBrowser(html);
}
Expand All @@ -182,40 +173,6 @@ private void exportImpl(
}
}

private void exportXml(@NotNull final BugCollection bugCollection, @NotNull final String fileName) throws IOException {
// Issue 77: workaround internal FindBugs NPE
// As of my point of view, the NPE is a FindBugs bug
bugCollection.writeXML(fileName);
}

private void exportHtml(@NotNull final BugCollection bugCollection, @NotNull final File file) throws IOException, TransformerException {
final Document document = bugCollection.toDocument();
final InputStream stylesheet = getStylesheetStream(FINDBUGS_PLAIN_XSL);
try {
final Source xsl = new StreamSource(stylesheet);
xsl.setSystemId(FINDBUGS_PLAIN_XSL);

// Create a transformer using the stylesheet
final TransformerFactoryImpl transformerFactory = new TransformerFactoryImpl();
final Transformer transformer = transformerFactory.newTransformer(xsl);

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

// Write result to output stream
final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
try {
final Result result = new StreamResult(writer);
// Do the transformation
transformer.transform(source, result);
} finally {
IoUtil.safeClose(writer);
}
} finally {
IoUtil.safeClose(stylesheet);
}
}

private static void openInBrowser(@NotNull final File file) {
EventDispatchThreadHelper.invokeLater(() -> BrowserUtil.browse(file.toURI()));
}
Expand All @@ -225,23 +182,4 @@ private static void showError(@NotNull final String message) {
() -> Messages.showErrorDialog(
message, StringUtil.capitalizeWords(ResourcesLoader.getString("export.title"), true)));
}

@NotNull
private static InputStream getStylesheetStream(@NotNull final String stylesheet) throws IOException {
try {
final URL url = new URL(stylesheet);
return url.openStream();
} catch (final Exception e) {
LOGGER.info("xls read failed.", e);
}
try {
return new BufferedInputStream(new FileInputStream(stylesheet));
} catch (final Exception ignored) {
}
final InputStream xslInputStream = HTMLBugReporter.class.getResourceAsStream('/' + stylesheet);
if (xslInputStream == null) {
throw new IOException("Could not load HTML generation stylesheet " + stylesheet);
}
return xslInputStream;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2020 SpotBugs plugin contributors
*
* This file is part of IntelliJ SpotBugs plugin.
*
* IntelliJ SpotBugs plugin is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* IntelliJ SpotBugs plugin 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with IntelliJ SpotBugs plugin.
* If not, see <http://www.gnu.org/licenses/>.
*/

package org.jetbrains.plugins.spotbugs.actions;

import edu.umd.cs.findbugs.BugCollection;
import edu.umd.cs.findbugs.HTMLBugReporter;
import net.sf.saxon.TransformerFactoryImpl;
import org.dom4j.Document;
import org.dom4j.io.DocumentSource;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.spotbugs.common.util.IoUtil;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class HtmlBugCollectionExporter implements BugCollectionExporter {

private static final String FINDBUGS_PLAIN_XSL = "plain.xsl";

public void export(@NotNull final BugCollection bugCollection, @NotNull final File file) throws IOException, TransformerException {
final Document document = bugCollection.toDocument();
final InputStream stylesheet = getStylesheetStream(FINDBUGS_PLAIN_XSL);
try {
final Source xsl = new StreamSource(stylesheet);
xsl.setSystemId(FINDBUGS_PLAIN_XSL);

// Create a transformer using the stylesheet
final TransformerFactoryImpl transformerFactory = new TransformerFactoryImpl();
final Transformer transformer = transformerFactory.newTransformer(xsl);

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

// Write result to output stream
final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8);
try {
final Result result = new StreamResult(writer);
// Do the transformation
transformer.transform(source, result);
} finally {
IoUtil.safeClose(writer);
}
} finally {
IoUtil.safeClose(stylesheet);
}
}

@NotNull
static InputStream getStylesheetStream(@NotNull final String stylesheet) throws IOException {
try {
final URL url = new URL(stylesheet);
return url.openStream();
} catch (final Exception e) {
LOGGER.info("xls read failed.", e);
}
try {
return new BufferedInputStream(new FileInputStream(stylesheet));
} catch (final Exception ignored) {
}
final InputStream xslInputStream = HTMLBugReporter.class.getResourceAsStream('/' + stylesheet);
if (xslInputStream == null) {
throw new IOException("Could not load HTML generation stylesheet " + stylesheet);
}
return xslInputStream;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2020 SpotBugs plugin contributors
*
* This file is part of IntelliJ SpotBugs plugin.
*
* IntelliJ SpotBugs plugin is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* IntelliJ SpotBugs plugin 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with IntelliJ SpotBugs plugin.
* If not, see <http://www.gnu.org/licenses/>.
*/

package org.jetbrains.plugins.spotbugs.actions;

import edu.umd.cs.findbugs.BugCollection;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;

public class XmlBugCollectionExporter implements BugCollectionExporter {

public void export(@NotNull final BugCollection bugCollection, @NotNull final File file) throws IOException {
// Issue 77: workaround internal FindBugs NPE
// As of my point of view, the NPE is a FindBugs bug
bugCollection.writeXML(file.getPath());
}
}

0 comments on commit d44ef95

Please sign in to comment.