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
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
38
39
40 public class FileListWalker implements IReportable {
41
42 private final FileDocument source;
43
44 private final DocumentName rootDoc;
45
46 private final DocumentName baseDoc;
47
48
49
50
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 }