You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@creadur.apache.org by rd...@apache.org on 2009/06/16 22:26:40 UTC

svn commit: r785376 - in /incubator/rat/main/trunk/apache-rat-core: pom.xml src/main/java/org/apache/rat/Report.java

Author: rdonkin
Date: Tue Jun 16 20:26:40 2009
New Revision: 785376

URL: http://svn.apache.org/viewvc?rev=785376&view=rev
Log:
RAT-56 Support Commons IO file filter wildcards https://issues.apache.org/jira/browse/RAT-56

Modified:
    incubator/rat/main/trunk/apache-rat-core/pom.xml
    incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/Report.java

Modified: incubator/rat/main/trunk/apache-rat-core/pom.xml
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/apache-rat-core/pom.xml?rev=785376&r1=785375&r2=785376&view=diff
==============================================================================
--- incubator/rat/main/trunk/apache-rat-core/pom.xml (original)
+++ incubator/rat/main/trunk/apache-rat-core/pom.xml Tue Jun 16 20:26:40 2009
@@ -61,6 +61,11 @@
       <version>2.1</version>
     </dependency>
     <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+      <version>1.4</version>
+    </dependency>
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>3.8.1</version>

Modified: incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/Report.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/Report.java?rev=785376&r1=785375&r2=785376&view=diff
==============================================================================
--- incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/Report.java (original)
+++ incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/Report.java Tue Jun 16 20:26:40 2009
@@ -30,17 +30,23 @@
 import java.io.PipedReader;
 import java.io.PipedWriter;
 import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 
 import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.TransformerConfigurationException;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Option;
+import org.apache.commons.cli.OptionBuilder;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.cli.PosixParser;
+import org.apache.commons.io.filefilter.AndFileFilter;
+import org.apache.commons.io.filefilter.NotFileFilter;
+import org.apache.commons.io.filefilter.WildcardFileFilter;
 import org.apache.rat.analysis.IHeaderMatcher;
 import org.apache.rat.annotation.AbstractLicenceAppender;
 import org.apache.rat.annotation.ApacheV2LicenceAppender;
@@ -55,12 +61,57 @@
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
 
 
 public class Report {
 
+    private static final char EXCLUDE_CLI = 'e';
+
     //@SuppressWarnings("unchecked")
     public static final void main(String args[]) throws Exception {
+        Options opts = buildOptions();
+        
+        PosixParser parser = new PosixParser();
+        CommandLine cl = null;
+        try {
+            cl = parser.parse(opts, args);
+        } catch (ParseException e) {
+            System.err.println("Please use the \"--help\" option to see a list of valid commands and options");
+            System.exit(1);
+        }
+
+        if (cl.hasOption('h')) {
+            printUsage(opts);
+        }
+
+        args = cl.getArgs();
+        if (args == null || args.length != 1) {
+            printUsage(opts);
+        } else {
+            Report report = new Report(args[0]);
+
+            if (cl.hasOption('a')) {
+                configureForAddLicense(cl, report);
+            }
+
+            if (cl.hasOption(EXCLUDE_CLI)) {
+                String[] excludes = cl.getOptionValues(EXCLUDE_CLI);
+                if (excludes != null) {
+                    final FilenameFilter filter = new NotFileFilter(new WildcardFileFilter(excludes));
+                    report.setInputFileFilter(filter);
+                }
+            }
+            
+            if (cl.hasOption('x')) {
+                report.report(System.out);
+            } else {
+                report.styleReport(System.out);
+            }	
+        } 
+    }
+
+    private static Options buildOptions() {
         Options opts = new Options();
 
         Option help = new Option("h", "help", false,
@@ -87,7 +138,7 @@
                 true,
         "The copyright message to use in the licence headers, usually in the form of \"Copyright 2008 Foo\"");
         opts.addOption(copyright);
-
+        
         Option xml = new Option(
                 "x",
                 "xml",
@@ -95,71 +146,63 @@
         "Output the report in XML format");
         opts.addOption(xml);
 
-        PosixParser parser = new PosixParser();
-        CommandLine cl = null;
-        try {
-            cl = parser.parse(opts, args);
-        } catch (ParseException e) {
-            System.err.println("Please use the \"--help\" option to see a list of valid commands and options");
-            System.exit(1);
-        }
-
-        if (cl.hasOption('h')) {
-            printUsage(opts);
-        }
-
+        final Option exclude = OptionBuilder
+                            .withArgName("expression")
+                            .withLongOpt("exclude")
+                            .hasArgs()
+                            .withDescription("Excludes files matching <expression>. " +
+                                    "Note that --dir is required when using this parameter. " +
+                                    "Allows multiple arguments.")
+                            .create(EXCLUDE_CLI);
+        opts.addOption(exclude);
+        
+        Option dir = new Option(
+                "d",
+                "dir",
+                false,
+        "Used to indicate source when using --exclude");
+        opts.addOption(dir);
+        
+        return opts;
+    }
 
-        args = cl.getArgs();
-        if (args == null || args.length != 1) {
-            printUsage(opts);
+    private static void configureForAddLicense(CommandLine cl, Report report) throws Exception, UnsupportedEncodingException, SAXException, IOException, ParserConfigurationException {
+        OutputStream reportOutput = new ByteArrayOutputStream();
+        PrintStream stream = new PrintStream(reportOutput, true);
+        report.report(stream);
+
+        AbstractLicenceAppender  appender;
+        String copyrightMsg = cl.getOptionValue("c");
+        if ( copyrightMsg != null) {
+            appender = new ApacheV2LicenceAppender(copyrightMsg);
         } else {
-            Report report = new Report(args[0]);
-
-            if (cl.hasOption('a')) {
-                OutputStream reportOutput = new ByteArrayOutputStream();
-                PrintStream stream = new PrintStream(reportOutput, true);
-                report.report(stream);
-
-                AbstractLicenceAppender  appender;
-                String copyrightMsg = cl.getOptionValue("c");
-                if ( copyrightMsg != null) {
-                    appender = new ApacheV2LicenceAppender(copyrightMsg);
-                } else {
-                    appender = new ApacheV2LicenceAppender();
-                }
-                if (cl.hasOption("f")) {
-                    appender.setForce(true);
-                }
-
-                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-                factory.setValidating(false);
-                ByteArrayInputStream xmlStream = new ByteArrayInputStream(reportOutput.toString().getBytes("UTF-8"));
-                Document doc = factory.newDocumentBuilder().parse(xmlStream);
-
-                NodeList resourceHeaders = doc.getElementsByTagName("header-type");
-                String value = null;
-                for (int i = 0; i < resourceHeaders.getLength(); i++) {
-                    Node headerType = resourceHeaders.item(i).getAttributes().getNamedItem("name");
-                    if(headerType != null) {
-                        value = headerType.getNodeValue();
-                    } else {
-                        value = null;
-                    }
-                    if (value != null &&value.equals("?????")) {
-                        Node resource = resourceHeaders.item(i).getParentNode();
-                        String filename = resource.getAttributes().getNamedItem("name").getNodeValue();
-                        File document = new File(filename);
-                        appender.append(document);
-                    }
-                }
-            }
+            appender = new ApacheV2LicenceAppender();
+        }
+        if (cl.hasOption("f")) {
+            appender.setForce(true);
+        }
 
-            if (cl.hasOption('x')) {
-                report.report(System.out);
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        factory.setValidating(false);
+        ByteArrayInputStream xmlStream = new ByteArrayInputStream(reportOutput.toString().getBytes("UTF-8"));
+        Document doc = factory.newDocumentBuilder().parse(xmlStream);
+
+        NodeList resourceHeaders = doc.getElementsByTagName("header-type");
+        String value = null;
+        for (int i = 0; i < resourceHeaders.getLength(); i++) {
+            Node headerType = resourceHeaders.item(i).getAttributes().getNamedItem("name");
+            if(headerType != null) {
+                value = headerType.getNodeValue();
             } else {
-                report.styleReport(System.out);
-            }		  
-        } 
+                value = null;
+            }
+            if (value != null &&value.equals("?????")) {
+                Node resource = resourceHeaders.item(i).getParentNode();
+                String filename = resource.getAttributes().getNamedItem("name").getNodeValue();
+                File document = new File(filename);
+                appender.append(document);
+            }
+        }
     }
 
     private static final void printUsage(Options opts) {