1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
40
41 public class FileListWalker implements Reportable {
42
43 private final FileDocument source;
44
45 private final DocumentName rootDoc;
46
47 private final DocumentName baseDoc;
48
49
50
51
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 }