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.BufferedInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.List;
29  
30  import org.apache.commons.compress.archivers.ArchiveEntry;
31  import org.apache.commons.compress.archivers.ArchiveException;
32  import org.apache.commons.compress.archivers.ArchiveInputStream;
33  import org.apache.commons.compress.archivers.ArchiveStreamFactory;
34  import org.apache.commons.io.IOUtils;
35  import org.apache.rat.api.Document;
36  import org.apache.rat.api.RatException;
37  import org.apache.rat.document.ArchiveEntryDocument;
38  import org.apache.rat.document.ArchiveEntryName;
39  import org.apache.rat.document.DocumentName;
40  import org.apache.rat.report.RatReport;
41  import org.apache.rat.utils.DefaultLog;
42  
43  import static java.lang.String.format;
44  
45  /**
46   * Walks various kinds of archives files.
47   */
48  public class ArchiveWalker extends Walker {
49  
50      /**
51       * Constructs a walker.
52       * @param document the document to process.
53       */
54      public ArchiveWalker(final Document document) {
55          super(document);
56      }
57  
58      /**
59       * Run a report over all files and directories in this GZIPWalker,
60       * ignoring any files/directories set to be ignored.
61       *
62       * @param report the defined RatReport to run on this GZIP walker.
63       *
64       */
65      public void run(final RatReport report) throws RatException {
66          for (Document document : getDocuments()) {
67              report.report(document);
68          }
69      }
70  
71      /**
72       * Creates an input stream from the directory being walked.
73       * @return A buffered input stream reading the archive data.
74       * @throws IOException on error
75       */
76      private InputStream createInputStream() throws IOException {
77          return new BufferedInputStream(getDocument().inputStream());
78      }
79      /**
80       * Retrieves the documents from the archive.
81       * @return A collection of documents that pass the file filter.
82       * @throws RatException on error.
83       */
84      public Collection<Document> getDocuments() throws RatException {
85          List<Document> result = new ArrayList<>();
86          try (ArchiveInputStream<? extends ArchiveEntry> input = new ArchiveStreamFactory().createArchiveInputStream(createInputStream())) {
87              ArchiveEntry entry;
88              while ((entry = input.getNextEntry()) != null) {
89                  if (!entry.isDirectory() && input.canReadEntryData(entry)) {
90                      DocumentName innerName = DocumentName.builder().setName(entry.getName())
91                              .setBaseName(".").build();
92                      if (this.getDocument().getNameMatcher().matches(innerName)) {
93                          ByteArrayOutputStream baos = new ByteArrayOutputStream();
94                          IOUtils.copy(input, baos);
95                          ArchiveEntryName entryName = new ArchiveEntryName(getDocument().getName(), entry.getName());
96                          result.add(new ArchiveEntryDocument(entryName, baos.toByteArray(), getDocument().getNameMatcher()));
97                      }
98                  }
99              }
100         } catch (ArchiveException e) {
101             DefaultLog.getInstance().warn(format("Unable to process %s: %s", getDocument().getName(), e.getMessage()));
102         } catch (IOException e) {
103             throw RatException.makeRatException(e);
104         }
105         return result;
106     }
107 }