You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@creadur.apache.org by gm...@apache.org on 2009/12/13 00:18:08 UTC

svn commit: r890004 - in /incubator/rat/main/trunk/apache-rat-core: ./ src/main/java/org/apache/rat/ src/main/java/org/apache/rat/document/impl/ src/main/java/org/apache/rat/walker/

Author: gmcdonald
Date: Sat Dec 12 23:18:07 2009
New Revision: 890004

URL: http://svn.apache.org/viewvc?rev=890004&view=rev
Log:
Add support for running RAT on compressed archives. Due to hwright, thanks!. Fix for RAT-17

Added:
    incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/document/impl/ArchiveEntryDocument.java   (with props)
    incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/GZIPWalker.java   (with props)
    incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/Walker.java   (with props)
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
    incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/document/impl/DocumentImplUtils.java
    incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/DirectoryWalker.java
    incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/FileNameComparator.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=890004&r1=890003&r2=890004&view=diff
==============================================================================
--- incubator/rat/main/trunk/apache-rat-core/pom.xml (original)
+++ incubator/rat/main/trunk/apache-rat-core/pom.xml Sat Dec 12 23:18:07 2009
@@ -66,6 +66,11 @@
       <version>1.4</version>
     </dependency>
     <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-compress</artifactId>
+      <version>1.0</version>
+    </dependency>
+    <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>3.8.1</version>
@@ -83,4 +88,4 @@
     <developerConnection>scm:svn:https://svn.apache.org/repos/asf/incubator/rat/main/trunk/apache-rat-core</developerConnection>
     <url>http://svn.apache.org/repos/asf/incubator/rat/main/trunk/apache-rat-core</url>
   </scm>
-</project>
\ No newline at end of file
+</project>

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=890004&r1=890003&r2=890004&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 Sat Dec 12 23:18:07 2009
@@ -44,7 +44,6 @@
 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;
@@ -58,6 +57,8 @@
 import org.apache.rat.report.xml.XmlReportFactory;
 import org.apache.rat.report.xml.writer.IXmlWriter;
 import org.apache.rat.report.xml.writer.impl.base.XmlWriter;
+import org.apache.rat.walker.DirectoryWalker;
+import org.apache.rat.walker.GZIPWalker;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
@@ -250,28 +251,34 @@
     }
 
     public ClaimStatistic report(PrintStream out) throws Exception {
-        final DirectoryWalker base = getDirectory(out);
+        final IReportable base = getDirectory(out);
         if (base != null) {
             return report(base, new OutputStreamWriter(out), Defaults.createDefaultMatcher(), null);
         }
         return null;
     }
 
-    private DirectoryWalker getDirectory(PrintStream out) {
-        DirectoryWalker result = null;
+    private IReportable getDirectory(PrintStream out) {
         File base = new File(baseDirectory);
         if (!base.exists()) {
             out.print("ERROR: ");
             out.print(baseDirectory);
             out.print(" does not exist.\n");
-        } else if (!base.isDirectory()) {
+            return null;
+        } 
+        
+        if (base.isDirectory()) {
+            return new DirectoryWalker(base, inputFileFilter);
+        }
+        
+        try {
+        	return new GZIPWalker(base, inputFileFilter);
+        } catch (IOException ex) {
             out.print("ERROR: ");
             out.print(baseDirectory);
-            out.print(" must be a directory.\n");
-        } else {
-            result = new DirectoryWalker(base, inputFileFilter);
+            out.print(" is not valid gzip data.\n");
+            return null;
         }
-        return result;
     }
 
     /**
@@ -282,7 +289,7 @@
      * @throws Exception
      */
     public void styleReport(PrintStream out) throws Exception {
-        final DirectoryWalker base = getDirectory(out);
+        final IReportable base = getDirectory(out);
         if (base != null) {
             InputStream style = Defaults.getDefaultStyleSheet();
             report(out, base, style, Defaults.createDefaultMatcher(), null);

Added: incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/document/impl/ArchiveEntryDocument.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/document/impl/ArchiveEntryDocument.java?rev=890004&view=auto
==============================================================================
--- incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/document/impl/ArchiveEntryDocument.java (added)
+++ incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/document/impl/ArchiveEntryDocument.java Sat Dec 12 23:18:07 2009
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ */
+
+package org.apache.rat.document.impl;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+
+import org.apache.rat.api.Document;
+import org.apache.rat.api.MetaData;
+import org.apache.rat.api.RatException;
+
+public class ArchiveEntryDocument implements Document {
+	
+	private byte[] contents;
+    private final String name;
+
+    private final MetaData metaData = new MetaData();
+
+	public ArchiveEntryDocument(File file, byte[] contents) throws RatException {
+		super();
+		name = DocumentImplUtils.toName(file);
+		this.contents = contents;
+	}
+	
+	public MetaData getMetaData() {
+		return metaData;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public InputStream inputStream() throws IOException {
+		return new ByteArrayInputStream(contents);
+	}
+
+	public boolean isComposite() {
+		return DocumentImplUtils.isZipStream(new ByteArrayInputStream(contents));
+	}
+
+	public Reader reader() throws IOException {
+		return new InputStreamReader(new ByteArrayInputStream(contents));
+	}
+
+
+    /**
+     * Representations suitable for logging.
+     * @return a <code>String</code> representation 
+     * of this object.
+     */
+    public String toString()
+    {
+        return "TarEntryDocument ( "
+            + "name = " + this.name + " "
+            + "metaData = " + this.metaData + " "
+            + " )";
+    }
+}

Propchange: incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/document/impl/ArchiveEntryDocument.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/document/impl/DocumentImplUtils.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/document/impl/DocumentImplUtils.java?rev=890004&r1=890003&r2=890004&view=diff
==============================================================================
--- incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/document/impl/DocumentImplUtils.java (original)
+++ incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/document/impl/DocumentImplUtils.java Sat Dec 12 23:18:07 2009
@@ -19,9 +19,11 @@
 package org.apache.rat.document.impl;
 
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.zip.ZipException;
-import java.util.zip.ZipFile;
+import java.util.zip.ZipInputStream;
 
 public class DocumentImplUtils {
 
@@ -30,18 +32,17 @@
         String normalisedPath = path.replace('\\', '/');
         return normalisedPath;
     }
-
-    public static final boolean isZip(File file) {
-        ZipFile zip = null;
-        try {
-            zip = new ZipFile(file);
-            zip.entries();
-            return true;
+    
+    public static final boolean isZipStream(InputStream stream) {
+    	ZipInputStream zip = new ZipInputStream(stream);
+    	try {
+			zip.getNextEntry();
+			return true;
         } catch (ZipException e) {
             return false;
-        } catch (IOException e) {
-            return false;
-        } finally {
+		} catch (IOException e) {
+			return false;
+		} finally {
             if (zip != null) {
                 try {
                     zip.close();
@@ -49,6 +50,14 @@
                     // Swallow
                 }
             }
+		}
+    }
+
+    public static final boolean isZip(File file) {
+        try {
+        	return isZipStream(new FileInputStream(file));
+        } catch (IOException e) {
+            return false;
         }
     }
 

Modified: incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/DirectoryWalker.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/DirectoryWalker.java?rev=890004&r1=890003&r2=890004&view=diff
==============================================================================
--- incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/DirectoryWalker.java (original)
+++ incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/DirectoryWalker.java Sat Dec 12 23:18:07 2009
@@ -17,7 +17,7 @@
  * under the License.                                           *
  */
 
-package org.apache.rat;
+package org.apache.rat.walker;
 
 import java.io.File;
 import java.io.FilenameFilter;
@@ -33,29 +33,10 @@
 /**
  * Walks directories.
  */
-public class DirectoryWalker implements IReportable {
+public class DirectoryWalker extends Walker implements IReportable {
 
-    private static FilenameFilter regexFilter(final Pattern pattern) {
-        return new FilenameFilter() {
-            public boolean accept(File dir, String name) {
-                final boolean result;
-                if (pattern == null) {
-                    result = true;
-                } else {
-                    result = !pattern.matcher(name).matches();
-                }
-                return result;
-            }
-        };
-    }
-    
-    private static final FileNameComparator COMPARATOR = new FileNameComparator();
-    
-    protected final File file;
-    protected final String name;
-
-    private final FilenameFilter filter;
-    
+    protected static final FileNameComparator COMPARATOR = new FileNameComparator();
+	
 	public DirectoryWalker(File file) {
 	    this(file, (FilenameFilter) null);
 	}
@@ -67,40 +48,17 @@
      * or null when no filtering should be performed
      */
     public DirectoryWalker(File file, final FilenameFilter filter) {
-        this(file.getPath(), file, filter);
+        super(file.getPath(), file, filter);
     }
     
     public DirectoryWalker(File file, final Pattern ignoreNameRegex) {
-        this(file.getPath(), file, regexFilter(ignoreNameRegex));
-    }
-
-    private DirectoryWalker(final String name, final File file, final FilenameFilter filter) {
-        this.name = name;
-        this.file = file;
-        this.filter = filter;
+        super(file.getPath(), file, regexFilter(ignoreNameRegex));
     }
 	
     public boolean isRestricted() {
         return false;
     }
-
-    protected final boolean ignored(final File file) {
-        boolean result = false;
-        final String name = file.getName();
-        final File dir = file.getParentFile();
-        if (filter != null) {
-            result = !filter.accept(dir, name);
-        }
-        return result;
-    }
-
-	
-	boolean isRestricted(File file) {
-		String name = file.getName();
-		boolean result = name.startsWith(".");
-		return result;
-	}
-    
+   
 	  /**
 	   * Process a directory, restricted directories will be ignored.
 	   * 

Modified: incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/FileNameComparator.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/FileNameComparator.java?rev=890004&r1=890003&r2=890004&view=diff
==============================================================================
--- incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/FileNameComparator.java (original)
+++ incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/FileNameComparator.java Sat Dec 12 23:18:07 2009
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations      *
  * under the License.                                           *
  */ 
-package org.apache.rat;
+package org.apache.rat.walker;
 
 import java.io.File;
 import java.util.Comparator;

Added: incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/GZIPWalker.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/GZIPWalker.java?rev=890004&view=auto
==============================================================================
--- incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/GZIPWalker.java (added)
+++ incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/GZIPWalker.java Sat Dec 12 23:18:07 2009
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ */
+
+package org.apache.rat.walker;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.util.zip.GZIPInputStream;
+
+import org.apache.commons.compress.archivers.ArchiveEntry;
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.rat.api.Document;
+import org.apache.rat.api.RatException;
+import org.apache.rat.document.impl.ArchiveEntryDocument;
+import org.apache.rat.report.IReportable;
+import org.apache.rat.report.RatReport;
+
+/**
+ * Walks .tar.gz files
+ */
+public class GZIPWalker extends Walker implements IReportable {
+	
+    /**
+     * Constructs a walker.
+     * @param file not null
+     * @param filter filters input files (optional), 
+     * or null when no filtering should be performed
+     * @throws IOException 
+     * @throws FileNotFoundException 
+     */
+    public GZIPWalker(File file, final FilenameFilter filter) throws FileNotFoundException {
+        super(file, filter);
+    }
+    
+    /**
+     * Run a report over all files and directories in this GZIPWalker,
+     * ignoring any files/directories set to be ignored.
+     * 
+     * @param report the defined RatReport to run on this GZIP walker.
+     * 
+     */
+    public void run(final RatReport report) throws RatException {
+    	
+		try {
+	        ArchiveInputStream input = new TarArchiveInputStream(new GZIPInputStream(new FileInputStream(file)));
+
+			ArchiveEntry entry = input.getNextEntry();
+    	    while (entry != null) {
+    	    	File f = new File(entry.getName());
+    	    	byte[] contents = new byte[(int) entry.getSize()];
+    	    	int offset = 0;
+    	    	int length = contents.length;
+
+    	    	while (offset < entry.getSize()) {
+        	    	int actualRead = input.read(contents, offset, length);
+        	    	length -= actualRead;
+        	    	offset += actualRead;
+    	    	}
+    	    	
+    	    	if (!entry.isDirectory() && !ignored(f)) {
+    	    		report(report, contents, f);
+    	    	}
+
+        		entry = input.getNextEntry();
+    	    }
+    	    
+    	    input.close();
+		} catch (IOException e) {
+			throw new RatException(e);
+		}
+    }
+
+    /**
+     * Report on the given file.
+     * 
+     * @param report the report to process the file with
+     * @param file the file to be reported on
+     * @throws RatException
+     */
+    private void report(final RatReport report, byte[] contents, File file) throws RatException {
+
+        Document document = new ArchiveEntryDocument(file, contents);
+        report.report(document);
+
+    }
+}

Propchange: incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/GZIPWalker.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/Walker.java
URL: http://svn.apache.org/viewvc/incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/Walker.java?rev=890004&view=auto
==============================================================================
--- incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/Walker.java (added)
+++ incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/Walker.java Sat Dec 12 23:18:07 2009
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ */
+
+package org.apache.rat.walker;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.util.regex.Pattern;
+
+import org.apache.rat.report.IReportable;
+
+/**
+ * Abstract walker.
+ */
+public abstract class Walker implements IReportable {
+	
+    protected final File file;
+    protected final String name;
+
+    protected final FilenameFilter filter;
+	
+    protected static FilenameFilter regexFilter(final Pattern pattern) {
+        return new FilenameFilter() {
+            public boolean accept(File dir, String name) {
+                final boolean result;
+                if (pattern == null) {
+                    result = true;
+                } else {
+                    result = !pattern.matcher(name).matches();
+                }
+                return result;
+            }
+        };
+    }
+	
+	protected boolean isRestricted(File file) {
+		String name = file.getName();
+		boolean result = name.startsWith(".");
+		return result;
+	}
+ 
+    protected final boolean ignored(final File file) {
+        boolean result = false;
+        final String name = file.getName();
+        final File dir = file.getParentFile();
+        if (filter != null) {
+            result = !filter.accept(dir, name);
+        }
+        return result;
+    }
+        
+    public Walker(File file, final FilenameFilter filter) {
+        this(file.getPath(), file, filter);
+    }
+	
+    protected Walker(final String name, final File file, final FilenameFilter filter) {
+        this.name = name;
+        this.file = file;
+        this.filter = filter;
+    }
+
+}

Propchange: incubator/rat/main/trunk/apache-rat-core/src/main/java/org/apache/rat/walker/Walker.java
------------------------------------------------------------------------------
    svn:eol-style = native