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.nio.file.Path; 23 import java.util.SortedSet; 24 25 import org.apache.commons.io.filefilter.IOFileFilter; 26 import org.apache.rat.ReportConfiguration; 27 import org.apache.rat.api.Document; 28 import org.apache.rat.api.RatException; 29 import org.apache.rat.report.RatReport; 30 31 /** 32 * Walks directories. 33 */ 34 public class DirectoryWalker extends Walker { 35 36 /** The directories to ignore */ 37 private final IOFileFilter directoriesToIgnore; 38 39 /** 40 * Constructs a directory walker. 41 * 42 * @param config the report configuration for this run. 43 * @param document the document to process. 44 */ 45 public DirectoryWalker(final ReportConfiguration config, final Document document) { 46 super(document, config.getFilesToIgnore()); 47 this.directoriesToIgnore = config.getDirectoriesToIgnore(); 48 } 49 50 /** 51 * Process a directory, restricted directories will be ignored. 52 * 53 * @param report The report to process the directory with 54 * @param document the document to process. 55 * @throws RatException on error. 56 */ 57 private void processDirectory(final RatReport report, final Document document) throws RatException { 58 if (isNotIgnoredDirectory(document.getPath())) { 59 process(report, document); 60 } 61 } 62 63 /** 64 * Run a report over all files and directories in this DirectoryWalker, 65 * ignoring any files/directories set to be ignored. 66 * 67 * @param report the defined RatReport to run on this Directory walker. 68 * @throws RatException on error 69 */ 70 public void run(final RatReport report) throws RatException { 71 process(report, getDocument()); 72 } 73 74 /** 75 * Process a directory, ignoring any files/directories set to be ignored. 76 * 77 * @param report the report to use in processing 78 * @param document the document to run the report against 79 * @throws RatException on error 80 */ 81 protected void process(final RatReport report, final Document document) throws RatException { 82 final SortedSet<Document> documents = document.listChildren(); 83 if (documents != null) { 84 // breadth first traversal 85 processNonDirectories(report, documents); 86 processDirectories(report, documents); 87 } 88 } 89 90 /** test for directory that is not ignored */ 91 private boolean isNotIgnoredDirectory(final Path path) { 92 return !directoriesToIgnore.accept(path.getParent().toFile(), path.toString()); 93 } 94 95 /** 96 * Process all directories in a set of file objects, ignoring any directories set to be ignored. 97 * 98 * @param report the report to use in processing 99 * @param documents the documents to process (only directories will be processed) 100 * @throws RatException on error 101 */ 102 private void processDirectories(final RatReport report, final SortedSet<Document> documents) throws RatException { 103 for (final Document doc : documents) { 104 if (doc.isDirectory() && isNotIgnoredDirectory(doc.getPath())) { 105 processDirectory(report, doc); 106 } 107 } 108 } 109 110 /** 111 * Process all files in a set of file objects, ignoring any files set to be ignored. 112 * 113 * @param report the report to use in processing 114 * @param documents the documents to process (only files will be processed) 115 * @throws RatException on error 116 */ 117 private void processNonDirectories(final RatReport report, final SortedSet<Document> documents) throws RatException { 118 for (final Document doc : documents) { 119 if (!doc.isDirectory() && isNotIgnored(doc.getPath())) { 120 report.report(doc); 121 } 122 } 123 } 124 }