View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.filter;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.vfs2.FileFilter;
22  import org.apache.commons.vfs2.FileSelectInfo;
23  import org.apache.commons.vfs2.FileSystemException;
24  
25  /**
26   * This filter accepts <code>File</code>s that are hidden.
27   * <p>
28   * Example, showing how to print out a list of the current directory's
29   * <i>hidden</i> files:
30   * </p>
31   *
32   * <pre>
33   * FileSystemManager fsManager = VFS.getManager();
34   * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
35   * FileObject[] files = dir.findFiles(new FileFilterSelector(HiddenFileFilter.HIDDEN));
36   * for (int i = 0; i &lt; files.length; i++) {
37   *     System.out.println(files[i]);
38   * }
39   * </pre>
40   *
41   * <p>
42   * Example, showing how to print out a list of the current directory's
43   * <i>visible</i> (i.e. not hidden) files:
44   * </p>
45   *
46   * <pre>
47   * FileSystemManager fsManager = VFS.getManager();
48   * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
49   * FileObject[] files = dir.findFiles(new FileFilterSelector(HiddenFileFilter.VISIBLE));
50   * for (int i = 0; i &lt; files.length; i++) {
51   *     System.out.println(files[i]);
52   * }
53   * </pre>
54   *
55   * @author This code was originally ported from Apache Commons IO File Filter
56   * @see "http://commons.apache.org/proper/commons-io/"
57   * @since 2.4
58   */
59  public class HiddenFileFilter implements FileFilter, Serializable {
60  
61      private static final long serialVersionUID = 1L;
62  
63      /** Singleton instance of <i>hidden</i> filter. */
64      public static final FileFilter HIDDEN = new HiddenFileFilter();
65  
66      /** Singleton instance of <i>visible</i> filter. */
67      public static final FileFilter VISIBLE = new NotFileFilter(HIDDEN);
68  
69      /**
70       * Restrictive constructor.
71       */
72      protected HiddenFileFilter() {
73      }
74  
75      /**
76       * Checks to see if the file is hidden. Non existing files won't be accepted.
77       *
78       * @param fileInfo the File to check
79       *
80       * @return {@code true} if the file is <i>hidden</i>, otherwise {@code false}.
81       * @throws FileSystemException Thrown for file system errors.
82       */
83      @Override
84      public boolean accept(final FileSelectInfo fileInfo) throws FileSystemException {
85          if (!fileInfo.getFile().exists()) {
86              return false;
87          }
88          return fileInfo.getFile().isHidden();
89      }
90  
91  }