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