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.rat.api.Document;
28  import org.apache.rat.api.RatException;
29  import org.apache.rat.document.impl.FileDocument;
30  import org.apache.rat.report.IReportable;
31  import org.apache.rat.report.RatReport;
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       * @param file not null
47       * @param filter filters input files (optional), 
48       * or null when no filtering should be performed
49       */
50      public DirectoryWalker(File file, final FilenameFilter filter) {
51          super(file.getPath(), file, filter);
52      }
53      
54      public DirectoryWalker(File file, final Pattern ignoreNameRegex) {
55          super(file.getPath(), file, regexFilter(ignoreNameRegex));
56      }
57  	
58      public boolean isRestricted() {
59          return false;
60      }
61     
62  	  /**
63  	   * Process a directory, restricted directories will be ignored.
64  	   * 
65  	   * @param report The report to process the directory with
66  	   * @param file the directory to process
67  	   * @throws RatException
68  	   */
69      private void processDirectory(RatReport  report, final File file) throws RatException {
70          if (!isRestricted(file)) {
71              process(report, file);
72          }
73      }
74      
75      /**
76       * Run a report over all files and directories in this DirectoryWalker,
77       * ignoring any files/directories set to be ignored.
78       * 
79       * @param report the defined RatReport to run on this Directory walker.
80       * 
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          Arrays.sort(files, COMPARATOR);
96          if (files != null) {
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 (int i = 0; i < files.length; i++) {
112             final File file = files[i];
113             if (!ignored(file)) {
114                 if (file.isDirectory()) {
115                     processDirectory(report, file);
116                 }
117             }
118         }
119     }
120     
121     /**
122      * Process all files in a set of file objects, ignoring any files set to be ignored.
123      * 
124      * @param report the report to use in processing
125      * @param files the files to process (only files will be processed)
126      * @throws RatException
127      */
128     private void processNonDirectories(final RatReport report, final File[] files) throws RatException {
129         for (int i = 0; i < files.length; i++) {
130             final File file = files[i];
131             if (!ignored(file)) {
132                 if (!file.isDirectory()) {
133                     report(report, file);
134                 }
135             }
136         }
137     }
138 
139     /**
140      * Report on the given file.
141      * 
142      * @param report the report to process the file with
143      * @param file the file to be reported on
144      * @throws RatException
145      */
146     private void report(final RatReport report, File file) throws RatException {
147 
148         Document document = new FileDocument(file);
149         report.report(document);
150 
151     }
152 }