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 new FilenameFilter() {
40              public boolean accept(File dir, String name) {
41                  final boolean result;
42                  if (pattern == null) {
43                      result = true;
44                  } else {
45                      result = !pattern.matcher(name).matches();
46                  }
47                  return result;
48              }
49          };
50      }
51  
52      protected boolean isRestricted(File file) {
53          return file.getName().startsWith(".");
54      }
55   
56      protected final boolean isNotIgnored(final File file) {
57          boolean result = false;
58          if (filter != null) {
59              final String name = file.getName();
60              final File dir = file.getParentFile();
61              result = !filter.accept(dir, name);
62          }
63          return !result;
64      }
65  
66      public Walker(File file, final FilenameFilter filter) {
67          this(file.getPath(), file, filter);
68      }
69  
70      protected Walker(final String name, final File file, final FilenameFilter filter) {
71          this.name = name;
72          this.file = file;
73          this.filter = filter;
74      }
75  
76  }