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  package org.apache.rat.walker;
20  
21  import org.apache.commons.io.filefilter.AbstractFileFilter;
22  import org.apache.commons.io.filefilter.IOFileFilter;
23  
24  import java.io.File;
25  import java.io.Serializable;
26  
27  /**
28   * This filter accepts {@code File}s that are hidden, e.g. file name starts with .
29   * <p>
30   * Example, showing how to print out a list of the current directory's <i>hidden</i> files:
31   * </p>
32   * <h2>Using Classic IO</h2>
33   * <pre>
34   * File dir = new File(".");
35   * String[] files = dir.list(NameBasedHiddenFileFilter.HIDDEN);
36   * for (String file : files) {
37   *     System.out.println(file);
38   * }
39   * </pre>
40   *
41   * <h2>Using NIO</h2>
42   * <pre>
43   * final Path dir = Paths.get("");
44   * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(NameBasedHiddenFileFilter.HIDDEN);
45   * //
46   * // Walk one dir
47   * Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
48   * System.out.println(visitor.getPathCounters());
49   * System.out.println(visitor.getFileList());
50   * //
51   * visitor.getPathCounters().reset();
52   * //
53   * // Walk dir tree
54   * Files.walkFileTree(dir, visitor);
55   * System.out.println(visitor.getPathCounters());
56   * System.out.println(visitor.getDirList());
57   * System.out.println(visitor.getFileList());
58   * </pre>
59   */
60  public class NameBasedHiddenFileFilter extends AbstractFileFilter implements Serializable {
61  
62      private static final long serialVersionUID = -5951069871734926741L;
63  	/**
64       * Singleton instance of <i>hidden</i> filter.
65       */
66      public static final IOFileFilter HIDDEN = new NameBasedHiddenFileFilter();
67  
68      /**
69       * Restrictive constructor.
70       */
71      protected NameBasedHiddenFileFilter() {
72      }
73  
74      /**
75       * Checks to see if the file is hidden, e.g. file name starts with .
76       *
77       * @param file the File to check
78       * @return {@code true} if the file is <i>hidden</i> (file name starting with .), {@code false} else
79       */
80      @Override
81      public boolean accept(File file) {
82          return file.getName().startsWith(".");
83      }
84  
85  }