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 org.apache.rat.report.IReportable;
23  
24  import java.io.File;
25  import java.io.FilenameFilter;
26  import java.util.regex.Pattern;
27  
28  /**
29   * Abstract walker.
30   */
31  public abstract class Walker implements IReportable {
32  
33      protected final File file;
34      protected final String name;
35  
36      protected final FilenameFilter filter;
37  
38      protected static FilenameFilter regexFilter(final Pattern pattern) {
39          return (dir, name) -> {
40              final boolean result;
41              if (pattern == null) {
42                  result = true;
43              } else {
44                  result = !pattern.matcher(name).matches();
45              }
46              return result;
47          };
48      }
49   
50      protected final boolean isNotIgnored(final File file) {
51          boolean result = false;
52          if (filter != null) {
53              final String name = file.getName();
54              final File dir = file.getParentFile();
55              result = !filter.accept(dir, name);
56          }
57          return !result;
58      }
59  
60      public Walker(File file, final FilenameFilter filter) {
61          this(file.getPath(), file, filter);
62      }
63  
64      protected Walker(final String name, final File file, final FilenameFilter filter) {
65          this.name = name;
66          this.file = file;
67          this.filter = filter;
68      }
69  
70  }