-
Notifications
You must be signed in to change notification settings - Fork 42
/
DirectoryWalker.java
124 lines (112 loc) · 4.67 KB
/
DirectoryWalker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
*/
package org.apache.rat.walker;
import java.nio.file.Path;
import java.util.SortedSet;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.rat.ReportConfiguration;
import org.apache.rat.api.Document;
import org.apache.rat.api.RatException;
import org.apache.rat.report.RatReport;
/**
* Walks directories.
*/
public class DirectoryWalker extends Walker {
/** The directories to ignore */
private final IOFileFilter directoriesToIgnore;
/**
* Constructs a directory walker.
*
* @param config the report configuration for this run.
* @param document the document to process.
*/
public DirectoryWalker(final ReportConfiguration config, final Document document) {
super(document, config.getFilesToIgnore());
this.directoriesToIgnore = config.getDirectoriesToIgnore();
}
/**
* Process a directory, restricted directories will be ignored.
*
* @param report The report to process the directory with
* @param document the document to process.
* @throws RatException on error.
*/
private void processDirectory(final RatReport report, final Document document) throws RatException {
if (isNotIgnoredDirectory(document.getPath())) {
process(report, document);
}
}
/**
* Run a report over all files and directories in this DirectoryWalker,
* ignoring any files/directories set to be ignored.
*
* @param report the defined RatReport to run on this Directory walker.
* @throws RatException on error
*/
public void run(final RatReport report) throws RatException {
process(report, getDocument());
}
/**
* Process a directory, ignoring any files/directories set to be ignored.
*
* @param report the report to use in processing
* @param document the document to run the report against
* @throws RatException on error
*/
protected void process(final RatReport report, final Document document) throws RatException {
final SortedSet<Document> documents = document.listChildren();
if (documents != null) {
// breadth first traversal
processNonDirectories(report, documents);
processDirectories(report, documents);
}
}
/** test for directory that is not ignored */
private boolean isNotIgnoredDirectory(final Path path) {
return !directoriesToIgnore.accept(path.getParent().toFile(), path.toString());
}
/**
* Process all directories in a set of file objects, ignoring any directories set to be ignored.
*
* @param report the report to use in processing
* @param documents the documents to process (only directories will be processed)
* @throws RatException on error
*/
private void processDirectories(final RatReport report, final SortedSet<Document> documents) throws RatException {
for (final Document doc : documents) {
if (doc.isDirectory() && isNotIgnoredDirectory(doc.getPath())) {
processDirectory(report, doc);
}
}
}
/**
* Process all files in a set of file objects, ignoring any files set to be ignored.
*
* @param report the report to use in processing
* @param documents the documents to process (only files will be processed)
* @throws RatException on error
*/
private void processNonDirectories(final RatReport report, final SortedSet<Document> documents) throws RatException {
for (final Document doc : documents) {
if (!doc.isDirectory() && isNotIgnored(doc.getPath())) {
report.report(doc);
}
}
}
}