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
20 package org.apache.rat.walker;
21
22 import org.apache.rat.api.Document;
23 import org.apache.rat.api.RatException;
24 import org.apache.rat.document.impl.FileDocument;
25 import org.apache.rat.report.IReportable;
26 import org.apache.rat.report.RatReport;
27
28 import java.io.File;
29 import java.io.FilenameFilter;
30 import java.util.Arrays;
31 import java.util.regex.Pattern;
32
33 /**
34 * Walks directories.
35 */
36 public class DirectoryWalker extends Walker implements IReportable {
37
38 protected static final FileNameComparator COMPARATOR = new FileNameComparator();
39
40 public DirectoryWalker(File file) {
41 this(file, (FilenameFilter) null);
42 }
43
44 /**
45 * Constructs a walker.
46 *
47 * @param file not null
48 * @param filter filters input files (optional),
49 * or null when no filtering should be performed
50 */
51 public DirectoryWalker(File file, final FilenameFilter filter) {
52 super(file.getPath(), file, filter);
53 }
54
55 public DirectoryWalker(File file, final Pattern ignoreNameRegex) {
56 super(file.getPath(), file, regexFilter(ignoreNameRegex));
57 }
58
59 public boolean isRestricted() {
60 return false;
61 }
62
63 /**
64 * Process a directory, restricted directories will be ignored.
65 *
66 * @param report The report to process the directory with
67 * @param file the directory to process
68 * @throws RatException
69 */
70 private void processDirectory(RatReport report, final File file) throws RatException {
71 if (!isRestricted(file)) {
72 process(report, file);
73 }
74 }
75
76 /**
77 * Run a report over all files and directories in this DirectoryWalker,
78 * ignoring any files/directories set to be ignored.
79 *
80 * @param report the defined RatReport to run on this Directory walker.
81 */
82 public void run(final RatReport report) throws RatException {
83 process(report, file);
84 }
85
86 /**
87 * Process a directory, ignoring any files/directories set to be ignored.
88 *
89 * @param report the report to use in processing
90 * @param file the run the report against
91 * @throws RatException
92 */
93 private void process(final RatReport report, final File file) throws RatException {
94 final File[] files = file.listFiles();
95 if (files != null) {
96 Arrays.sort(files, COMPARATOR);
97 // breadth first traversal
98 processNonDirectories(report, files);
99 processDirectories(report, files);
100 }
101 }
102
103 /**
104 * Process all directories in a set of file objects, ignoring any directories set to be ignored.
105 *
106 * @param report the report to use in processing
107 * @param files the files to process (only directories will be processed)
108 * @throws RatException
109 */
110 private void processDirectories(final RatReport report, final File[] files) throws RatException {
111 for (final File file : files) {
112 if (!ignored(file) && file.isDirectory()) {
113 processDirectory(report, file);
114 }
115 }
116 }
117
118 /**
119 * Process all files in a set of file objects, ignoring any files set to be ignored.
120 *
121 * @param report the report to use in processing
122 * @param files the files to process (only files will be processed)
123 * @throws RatException
124 */
125 private void processNonDirectories(final RatReport report, final File[] files) throws RatException {
126 for (final File file : files) {
127 if (!ignored(file) && !file.isDirectory()) {
128 report(report, file);
129 }
130 }
131
132 }
133
134 /**
135 * Report on the given file.
136 *
137 * @param report the report to process the file with
138 * @param file the file to be reported on
139 * @throws RatException
140 */
141 private void report(final RatReport report, File file) throws RatException {
142
143 Document document = new FileDocument(file);
144 report.report(document);
145
146 }
147 }