From 04b799c9921f0af95169692fab1ee4fde6171416 Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 26 May 2010 11:21:29 +0000 Subject: [PATCH] Support XML output in Ant task. RAT-73 git-svn-id: https://svn.apache.org/repos/asf/incubator/rat/main/trunk@948396 13f79535-47bb-0310-9956-ffa450edef68 --- ant-task-examples.xml | 8 ++ .../java/org/apache/rat/anttasks/Report.java | 105 +++++++++++++++++- .../antunit/report-bad-configurations.xml | 15 +++ .../antunit/report-normal-operation.xml | 18 ++- 4 files changed, 140 insertions(+), 6 deletions(-) diff --git a/ant-task-examples.xml b/ant-task-examples.xml index 6528c2b08..9d22609da 100755 --- a/ant-task-examples.xml +++ b/ant-task-examples.xml @@ -83,6 +83,14 @@ + + + + + + diff --git a/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java b/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java index 20dc914ac..303572ad0 100755 --- a/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java +++ b/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; @@ -31,6 +32,8 @@ import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.apache.tools.ant.taskdefs.LogOutputStream; +import org.apache.tools.ant.types.EnumeratedAttribute; +import org.apache.tools.ant.types.Resource; import org.apache.tools.ant.types.ResourceCollection; import org.apache.tools.ant.types.resources.Union; import org.apache.tools.ant.util.FileUtils; @@ -46,6 +49,17 @@ * the nested resource collection(s). * *

ILicenseMatcher(s) can be specified as nested elements as well.

+ * + *

The attribute format defines the output format and + * can take the values + *

    + *
  • xml - RAT's native XML output.
  • + *
  • styled - transforms the XML output using the given + * stylesheet. The stylesheet attribute must be set as well if this + * attribute is used.
  • + *
  • plain - plain text using RAT's built-in stylesheet. This is + * the default.
  • + *
*/ public class Report extends Task { @@ -57,9 +71,9 @@ public class Report extends Task { * The licenses we want to match on. */ private ArrayList licenseMatchers = new ArrayList(); - + private ArrayList licenseNames = new ArrayList(); - + /** * Whether to add the default list of license matchers. */ @@ -68,6 +82,14 @@ public class Report extends Task { * Where to send the report. */ private File reportFile; + /** + * Which format to use. + */ + private Format format = Format.PLAIN; + /** + * Which stylesheet to use. + */ + private Resource stylesheet; /** * Adds resources that will be checked. @@ -85,7 +107,7 @@ public void add(ResourceCollection rc) { public void add(IHeaderMatcher matcher) { licenseMatchers.add(matcher); } - + public void add(ILicenseFamily license) { licenseNames.add(license); } @@ -104,6 +126,23 @@ public void setReportFile(File f) { reportFile = f; } + /** + * Which format to use. + */ + public void setFormat(Format f) { + if (f == null) { + throw new IllegalArgumentException("format must not be null"); + } + format = f; + } + + /** + * Which stylesheet to use (only meaningful with format='styled'). + */ + public void setStylesheet(Resource r) { + stylesheet = r; + } + /** * Generates the report. */ @@ -150,6 +189,19 @@ private void validate() { throw new BuildException("You must specify at least one license" + " matcher"); } + if (format.getValue().equals(Format.STYLED_KEY)) { + if (stylesheet == null) { + throw new BuildException("You must specify a stylesheet when" + + " using the 'styled' format"); + } + if (!stylesheet.isExists()) { + throw new BuildException("Cannot find specified stylesheet '" + + stylesheet + "'"); + } + } else if (stylesheet != null) { + log("Ignoring stylesheet '" + stylesheet + "' when using format '" + + format.getValue() + "'", Project.MSG_WARN); + } } /** @@ -162,7 +214,26 @@ private void createReport(PrintWriter out) throws IOException, TransformerExcept HeaderMatcherMultiplexer m = new HeaderMatcherMultiplexer(getLicenseMatchers()); ResourceCollectionContainer rcElement = new ResourceCollectionContainer(nestedResources); - org.apache.rat.Report.report(out, rcElement, Defaults.getDefaultStyleSheet(), m, getApprovedLicenseNames()); + if (format.getValue().equals(Format.XML_KEY)) { + org.apache.rat.Report.report(rcElement, out, m, + getApprovedLicenseNames()); + } else { + InputStream style = null; + try { + if (format.getValue().equals(Format.PLAIN_KEY)) { + style = Defaults.getPlainStyleSheet(); + } else if (format.getValue().equals(Format.STYLED_KEY)) { + style = stylesheet.getInputStream(); + } else { + throw new BuildException("unsupported format '" + + format.getValue() + "'"); + } + org.apache.rat.Report.report(out, rcElement, style, + m, getApprovedLicenseNames()); + } finally { + FileUtils.close(style); + } + } } /** @@ -188,7 +259,7 @@ private IHeaderMatcher[] getLicenseMatchers() { } return matchers; } - + private ILicenseFamily[] getApprovedLicenseNames() { // TODO: add support for adding default licenses ILicenseFamily[] results = null; @@ -197,4 +268,28 @@ private ILicenseFamily[] getApprovedLicenseNames() { } return results; } + + /** + * Type for the format attribute. + */ + public static class Format extends EnumeratedAttribute { + static final String XML_KEY = "xml"; + static final String STYLED_KEY = "styled"; + static final String PLAIN_KEY = "plain"; + + static final Format PLAIN = new Format(PLAIN_KEY); + + public Format() { super(); } + + private Format(String s) { + this(); + setValue(s); + } + + public String[] getValues() { + return new String[] { + XML_KEY, STYLED_KEY, PLAIN_KEY + }; + } + } } diff --git a/apache-rat-tasks/src/test/resources/antunit/report-bad-configurations.xml b/apache-rat-tasks/src/test/resources/antunit/report-bad-configurations.xml index 4c702b85c..aa79e0fff 100755 --- a/apache-rat-tasks/src/test/resources/antunit/report-bad-configurations.xml +++ b/apache-rat-tasks/src/test/resources/antunit/report-bad-configurations.xml @@ -58,4 +58,19 @@
+ + + + + + + + + + + + + + + diff --git a/apache-rat-tasks/src/test/resources/antunit/report-normal-operation.xml b/apache-rat-tasks/src/test/resources/antunit/report-normal-operation.xml index e261d9be3..60f7549bf 100755 --- a/apache-rat-tasks/src/test/resources/antunit/report-normal-operation.xml +++ b/apache-rat-tasks/src/test/resources/antunit/report-normal-operation.xml @@ -50,6 +50,8 @@ + + @@ -84,6 +86,19 @@ + + + + + + + + + + + @@ -130,8 +145,9 @@ + - +