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 symbolic links.
27   * <p>
28   * Example, showing how to print out a list of the current directory's
29   * <i>symbolic link</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(SymbolicLinkFileFilter.SYMBOLIC));
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>actual</i> (i.e. symbolic link) 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(SymbolicLinkFileFilter.ACTUAL));
50   * for (int i = 0; i &lt; files.length; i++) {
51   *     System.out.println(files[i]);
52   * }
53   * </pre>
54   *
55   * @since 2.4
56   */
57  public class SymbolicLinkFileFilter implements FileFilter, Serializable {
58  
59      private static final long serialVersionUID = 1L;
60  
61      /** Singleton instance of <i>hidden</i> filter. */
62      public static final FileFilter SYMBOLIC = new SymbolicLinkFileFilter();
63  
64      /** Singleton instance of <i>visible</i> filter. */
65      public static final FileFilter ACTUAL = new NotFileFilter(SYMBOLIC);
66  
67      /**
68       * Restrictive constructor.
69       */
70      protected SymbolicLinkFileFilter() {
71      }
72  
73      /**
74       * Checks to see if the file is a symbolic link. Non existing files won't be accepted.
75       *
76       * @param fileInfo the file to check
77       *
78       * @return {@code true} if the file is <i>symbolic link</i>, otherwise {@code false}.
79       * @throws FileSystemException Thrown for file system errors.
80       */
81      @Override
82      public boolean accept(final FileSelectInfo fileInfo) throws FileSystemException {
83          if (!fileInfo.getFile().exists()) {
84              return false;
85          }
86          return fileInfo.getFile().isSymbolicLink();
87      }
88  
89  }