You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@creadur.apache.org by bo...@apache.org on 2010/05/26 13:21:29 UTC

svn commit: r948396 - in /incubator/rat/main/trunk: ./ apache-rat-tasks/src/main/java/org/apache/rat/anttasks/ apache-rat-tasks/src/test/resources/antunit/

Author: bodewig
Date: Wed May 26 11:21:29 2010
New Revision: 948396

URL: http://svn.apache.org/viewvc?rev=948396&view=rev
Log:
Support XML output in Ant task.  RAT-73

Modified:
    incubator/rat/main/trunk/ant-task-examples.xml
    incubator/rat/main/trunk/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java
    incubator/rat/main/trunk/apache-rat-tasks/src/test/resources/antunit/report-bad-configurations.xml
    incubator/rat/main/trunk/apache-rat-tasks/src/test/resources/antunit/report-normal-operation.xml

Modified: incubator/rat/main/trunk/ant-task-examples.xml
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/ant-task-examples.xml?rev=948396&r1=948395&r2=948396&view=diff
==============================================================================
--- incubator/rat/main/trunk/ant-task-examples.xml (original)
+++ incubator/rat/main/trunk/ant-task-examples.xml Wed May 26 11:21:29 2010
@@ -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">

Modified: incubator/rat/main/trunk/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java?rev=948396&r1=948395&r2=948396&view=diff
==============================================================================
--- incubator/rat/main/trunk/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java (original)
+++ incubator/rat/main/trunk/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java Wed May 26 11:21:29 2010
@@ -21,6 +21,7 @@ package org.apache.rat.anttasks;
 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.BuildExcepti
 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 @@ import org.apache.rat.license.ILicenseFa
  * 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 {
 
@@ -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 class Report extends Task {
     public void add(IHeaderMatcher matcher) {
         licenseMatchers.add(matcher);
     }
-    
+
     public void add(ILicenseFamily license) {
         licenseNames.add(license);
     }
@@ -105,6 +127,23 @@ public class Report extends Task {
     }
 
     /**
+     * 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.
      */
     public void execute() {
@@ -150,6 +189,19 @@ public class Report extends Task {
             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 @@ public class Report extends Task {
         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 @@ public class Report extends Task {
         }
         return matchers;
     }
-    
+
     private ILicenseFamily[] getApprovedLicenseNames() {
         // TODO: add support for adding default licenses
         ILicenseFamily[] results = null;
@@ -197,4 +268,28 @@ public class Report extends Task {
         }
         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
+            };
+        }
+    }
 }

Modified: incubator/rat/main/trunk/apache-rat-tasks/src/test/resources/antunit/report-bad-configurations.xml
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/apache-rat-tasks/src/test/resources/antunit/report-bad-configurations.xml?rev=948396&r1=948395&r2=948396&view=diff
==============================================================================
--- incubator/rat/main/trunk/apache-rat-tasks/src/test/resources/antunit/report-bad-configurations.xml (original)
+++ incubator/rat/main/trunk/apache-rat-tasks/src/test/resources/antunit/report-bad-configurations.xml Wed May 26 11:21:29 2010
@@ -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>

Modified: incubator/rat/main/trunk/apache-rat-tasks/src/test/resources/antunit/report-normal-operation.xml
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/apache-rat-tasks/src/test/resources/antunit/report-normal-operation.xml?rev=948396&r1=948395&r2=948396&view=diff
==============================================================================
--- incubator/rat/main/trunk/apache-rat-tasks/src/test/resources/antunit/report-normal-operation.xml (original)
+++ incubator/rat/main/trunk/apache-rat-tasks/src/test/resources/antunit/report-normal-operation.xml Wed May 26 11:21:29 2010
@@ -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">
@@ -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}"/>
@@ -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>