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