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.regex.Pattern;
25
26 import org.apache.rat.report.IReportable;
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 String name = file.getName();
54 boolean result = name.startsWith(".");
55 return result;
56 }
57
58 protected final boolean ignored(final File file) {
59 boolean result = false;
60 final String name = file.getName();
61 final File dir = file.getParentFile();
62 if (filter != null) {
63 result = !filter.accept(dir, name);
64 }
65 return result;
66 }
67
68 public Walker(File file, final FilenameFilter filter) {
69 this(file.getPath(), file, filter);
70 }
71
72 protected Walker(final String name, final File file, final FilenameFilter filter) {
73 this.name = name;
74 this.file = file;
75 this.filter = filter;
76 }
77
78 }