Skip to content

Commit

Permalink
issue #107: support relative path for stylesheet (XSL)
Browse files Browse the repository at this point in the history
  • Loading branch information
KengoTODA committed Mar 11, 2019
1 parent 5dd3462 commit 8e6b37c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.github.spotbugs.internal;

import java.io.File;
import java.util.Optional;

import org.gradle.api.InvalidUserDataException;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.logging.Logger;
import org.gradle.api.reporting.internal.CustomizableHtmlReportImpl;
import org.gradle.api.resources.ResourceHandler;
import org.gradle.api.resources.TextResource;
import org.gradle.api.resources.TextResourceFactory;
import org.gradle.api.tasks.Input;

public class SpotBugsHtmlReportImpl extends CustomizableHtmlReportImpl {
private static final long serialVersionUID = 6474874842199703745L;
private final ResourceHandler handler;
private final Configuration configuration;
private final Logger logger;

/**
* Null-able string representing relative file path of XSL packaged in spotbugs.jar.
*/
private String stylesheet;

public SpotBugsHtmlReportImpl(String name, Task task) {
super(name, task);
handler = task.getProject().getResources();
configuration = task.getProject().getConfigurations().getAt("spotbugs");
logger = task.getLogger();
}

@Input
public void setStylesheet(String fileName) {
this.stylesheet = fileName;
}

@Override
public TextResource getStylesheet() {
if (stylesheet == null) {
return super.getStylesheet();
}

TextResourceFactory factory = handler.getText();
Optional<File> spotbugs = configuration.files(this::find).stream().findFirst();
if (spotbugs.isPresent()) {
File jar = spotbugs.get();
logger.debug("Specified stylesheet ({}) found in spotbugs configuration: {}", stylesheet, jar.getAbsolutePath());
return factory.fromArchiveEntry(jar, stylesheet);
} else {
throw new InvalidUserDataException("Specified stylesheet (" + stylesheet + ") does not found in spotbugs configuration");
}
}

private boolean find(Dependency d) {
return "com.github.spotbugs".equals(d.getGroup()) && "spotbugs".equals(d.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.gradle.api.Task;
import org.gradle.api.reporting.SingleFileReport;
import org.gradle.api.reporting.internal.CustomizableHtmlReportImpl;
import org.gradle.api.reporting.internal.TaskGeneratedSingleFileReport;
import org.gradle.api.reporting.internal.TaskReportContainer;

Expand All @@ -11,11 +10,11 @@

public class SpotBugsReportsImpl extends TaskReportContainer<SingleFileReport> implements SpotBugsReportsInternal {

public SpotBugsReportsImpl(Task task) {
public SpotBugsReportsImpl(Task task) {
super(SingleFileReport.class, task);

add(SpotBugsXmlReportImpl.class, "xml", task);
add(CustomizableHtmlReportImpl.class, "html", task);
add(SpotBugsHtmlReportImpl.class, "html", task);
add(TaskGeneratedSingleFileReport.class, "text", task);
add(TaskGeneratedSingleFileReport.class, "emacs", task);
}
Expand Down
4 changes: 1 addition & 3 deletions src/test/resources/HtmlReportTest.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ plugins {
id 'java'
id 'com.github.spotbugs'
}
configurations { spotbugsStylesheets { transitive false } }
dependencies { spotbugsStylesheets 'com.github.spotbugs:spotbugs:3.1.10' }
version = '1.2.3'
repositories {
mavenCentral()
Expand All @@ -13,7 +11,7 @@ spotbugsMain {
xml.enabled false
html {
enabled true
stylesheet resources.text.fromArchiveEntry(configurations.spotbugsStylesheets, 'fancy-hist.xsl')
stylesheet 'fancy-hist.xsl'
}
}
}

0 comments on commit 8e6b37c

Please sign in to comment.