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  package org.apache.rat.walker;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.Reader;
24  import java.util.Objects;
25  
26  import org.apache.commons.io.IOUtils;
27  import org.apache.rat.api.RatException;
28  import org.apache.rat.commandline.Arg;
29  import org.apache.rat.config.exclusion.ExclusionUtils;
30  import org.apache.rat.document.DocumentName;
31  import org.apache.rat.document.DocumentNameMatcher;
32  import org.apache.rat.document.FileDocument;
33  import org.apache.rat.report.RatReport;
34  import org.apache.rat.report.Reportable;
35  import org.apache.rat.utils.DefaultLog;
36  
37  /**
38   * Implementation of Reportable that traverses over a resource collection
39   * internally.
40   */
41  public class FileListWalker implements Reportable {
42      /** The source document name. */
43      private final FileDocument source;
44      /** The root document name. */
45      private final DocumentName rootDoc;
46      /** The base directory for the source document. */
47      private final DocumentName baseDoc;
48  
49      /**
50       * Constructor.
51       * @param source the file document that is the source from which this walker will read.
52       */
53      public FileListWalker(final FileDocument source) {
54          this.source = source;
55          File baseDir = source.getFile().getParentFile().getAbsoluteFile();
56          this.baseDoc = DocumentName.builder(baseDir).build();
57          File p = baseDir;
58          while (p.getParentFile() != null) {
59              p = p.getParentFile();
60          }
61          this.rootDoc = DocumentName.builder(p).build();
62      }
63  
64      private FileDocument createDocument(final String unixFileName) {
65          Objects.requireNonNull(unixFileName, "unixFileName may not be null");
66          DocumentName sourceName = source.getName();
67          String finalName = ExclusionUtils.convertSeparator(unixFileName, "/", sourceName.getDirectorySeparator());
68          DocumentName documentBase = unixFileName.startsWith("/") ? rootDoc : baseDoc;
69          DocumentName documentName = documentBase.resolve(finalName);
70          File documentFile = documentName.asFile();
71          return new FileDocument(documentBase, documentFile, DocumentNameMatcher.MATCHES_ALL);
72      }
73  
74      @Override
75      public void run(final RatReport report) throws RatException {
76          DefaultLog.getInstance().debug(String.format("Reading file name: %s due to option %s", source, Arg.SOURCE.option()));
77          DocumentName sourceName = name();
78          try (Reader reader = source.reader()) {
79              for (String docName : IOUtils.readLines(reader)) {
80                  try {
81                      DefaultLog.getInstance().debug("Reading file name: " + docName);
82                      FileDocument document = createDocument(docName);
83                      if (document.isDirectory()) {
84                          new DirectoryWalker(document).run(report);
85                      } else {
86                          report.report(document);
87                      }
88                  } catch (RatException e) {
89                      throw new RatException(String.format("Error reading file `%s` read from `%s`", docName, sourceName), e);
90                  }
91              }
92          }  catch (IOException e) {
93              throw new RatException("can not read " + sourceName.getName(), e);
94          }
95      }
96  
97      @Override
98      public DocumentName name() {
99          return source.getName();
100     }
101 }