View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */
19  
20  package org.apache.rat.walker;
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileNotFoundException;
25  import java.io.FilenameFilter;
26  import java.io.IOException;
27  
28  import org.apache.commons.compress.archivers.ArchiveEntry;
29  import org.apache.commons.compress.archivers.ArchiveInputStream;
30  import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
31  import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
32  import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
33  import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
34  import org.apache.rat.api.Document;
35  import org.apache.rat.api.RatException;
36  import org.apache.rat.document.impl.ArchiveEntryDocument;
37  import org.apache.rat.report.IReportable;
38  import org.apache.rat.report.RatReport;
39  
40  /**
41   * Walks various kinds of archives files
42   */
43  public class ArchiveWalker extends Walker implements IReportable {
44  	
45      /**
46       * Constructs a walker.
47       * @param file not null
48       * @param filter filters input files (optional), 
49       * or null when no filtering should be performed
50       * @throws IOException 
51       * @throws FileNotFoundException 
52       */
53      public ArchiveWalker(File file, final FilenameFilter filter) throws FileNotFoundException {
54          super(file, filter);
55      }
56      
57      /**
58       * Run a report over all files and directories in this GZIPWalker,
59       * ignoring any files/directories set to be ignored.
60       * 
61       * @param report the defined RatReport to run on this GZIP walker.
62       * 
63       */
64      public void run(final RatReport report) throws RatException {
65      	
66  		try {
67  			ArchiveInputStream input;
68  		
69              /* I am really sad that classes aren't first-class objects in
70                 Java :'( */
71  			try {
72  				input = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(file)));
73  			} catch (IOException e) {
74  				try {
75  					input = new TarArchiveInputStream(new BZip2CompressorInputStream(new FileInputStream(file)));
76  				} catch (IOException e2) {
77  					input = new ZipArchiveInputStream(new FileInputStream(file));
78  				}
79  			}
80  
81  			ArchiveEntry entry = input.getNextEntry();
82      	    while (entry != null) {
83      	    	File f = new File(entry.getName());
84      	    	byte[] contents = new byte[(int) entry.getSize()];
85      	    	int offset = 0;
86      	    	int length = contents.length;
87  
88      	    	while (offset < entry.getSize()) {
89          	    	int actualRead = input.read(contents, offset, length);
90          	    	length -= actualRead;
91          	    	offset += actualRead;
92      	    	}
93      	    	
94      	    	if (!entry.isDirectory() && !ignored(f)) {
95      	    		report(report, contents, f);
96      	    	}
97  
98          		entry = input.getNextEntry();
99      	    }
100     	    
101     	    input.close();
102 		} catch (IOException e) {
103 			throw new RatException(e);
104 		}
105     }
106 
107     /**
108      * Report on the given file.
109      * 
110      * @param report the report to process the file with
111      * @param file the file to be reported on
112      * @throws RatException
113      */
114     private void report(final RatReport report, byte[] contents, File file) throws RatException {
115 
116         Document document = new ArchiveEntryDocument(file, contents);
117         report.report(document);
118 
119     }
120 }