Skip to content

Commit

Permalink
Support XML output in Ant task. RAT-73
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/incubator/rat/main/trunk@948396 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
bodewig committed May 26, 2010
1 parent 3d3cd4a commit 04b799c
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 6 deletions.
8 changes: 8 additions & 0 deletions ant-task-examples.xml
Expand Up @@ -83,6 +83,14 @@
</rat:report>
</target>

<target name="run-on-rat-output-xml" depends="-taskdef"
description="Runs RAT on its own source tree and creates an XML report">
<rat:report xmlns:rat="antlib:org.apache.rat.anttasks"
format="xml" reportFile="rat-report.xml">
<fileset dir="."/>
</rat:report>
</target>

<target name="-taskdef">
<typedef resource="org/apache/rat/anttasks/antlib.xml"
uri="antlib:org.apache.rat.anttasks">
Expand Down
105 changes: 100 additions & 5 deletions apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -46,6 +49,17 @@
* the nested resource collection(s).
*
* <p>ILicenseMatcher(s) can be specified as nested elements as well.</p>
*
* <p>The attribute <code>format</code> defines the output format and
* can take the values
* <ul>
* <li>xml - RAT's native XML output.</li>
* <li>styled - transforms the XML output using the given
* stylesheet. The stylesheet attribute must be set as well if this
* attribute is used.</li>
* <li>plain - plain text using RAT's built-in stylesheet. This is
* the default.</li>
* </ul>
*/
public class Report extends Task {

Expand All @@ -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.
*/
Expand All @@ -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.
Expand All @@ -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);
}
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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);
}
}

/**
Expand All @@ -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);
}
}
}

/**
Expand All @@ -188,7 +259,7 @@ private IHeaderMatcher[] getLicenseMatchers() {
}
return matchers;
}

private ILicenseFamily[] getApprovedLicenseNames() {
// TODO: add support for adding default licenses
ILicenseFamily[] results = null;
Expand All @@ -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
};
}
}
}
Expand Up @@ -58,4 +58,19 @@
</au:expectfailure>
</target>

<target name="testNoStylesheet">
<au:expectfailure expectedMessage="must specify a stylesheet">
<rat:report format="styled">
<file file="${ant.file}"/>
</rat:report>
</au:expectfailure>
</target>

<target name="testUnusedStylesheet">
<rat:report stylesheet="${ant.file}">
<file file="${ant.file}"/>
</rat:report>
<au:assertLogContains text="Ignoring stylesheet"/>
</target>

</project>
Expand Up @@ -50,6 +50,8 @@
</path>
</pathconvert>
<property name="expectedOutput" value=" AL ${file.name}"/>
<property name="expectedOutputXML" value="&lt;resource name='${file.name}'"/>
<property name="expectedOutputXML2" value="&lt;header-type name='AL '"/>
</target>

<target name="allTests">
Expand Down Expand Up @@ -84,6 +86,19 @@
<assertReportContains text="${expectedOutput}"/>
</target>

<target name="testXMLReportSentToFile" depends="fileReportTest">
<rat:report reportFile="${report.file}.xml" format="xml">
<file file="${ant.file}"/>
</rat:report>
<au:assertLogDoesntContain text="${expectedOutputXML}"/>
<au:assertLogDoesntContain text="${expectedOutputXML2}"/>
<au:assertFileExists file="${report.file}.xml"/>
<assertReportContains text="${expectedOutputXML}"
file="${report.file}.xml"/>
<assertReportContains text="${expectedOutputXML2}"
file="${report.file}.xml"/>
</target>

<target name="xtestWithASLUnknown">
<rat:report addDefaultLicenseMatchers="false">
<file file="${ant.file}"/>
Expand Down Expand Up @@ -130,8 +145,9 @@
<au:assertFileDoesntExist file="${report.file}"/>
<macrodef name="assertReportContains">
<attribute name="text"/>
<attribute name="file" default="${report.file}"/>
<sequential>
<loadfile srcFile="${report.file}" property="report"/>
<loadfile srcFile="@{file}" property="report"/>
<au:assertTrue message="expected report to contain '@{text}' but was '${report}'">
<contains string="${report}" substring="@{text}"/>
</au:assertTrue>
Expand Down

0 comments on commit 04b799c

Please sign in to comment.