001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.filter;
018
019import java.io.Serializable;
020
021import org.apache.commons.vfs2.FileFilter;
022import org.apache.commons.vfs2.FileSelectInfo;
023import org.apache.commons.vfs2.FileSystemException;
024
025/**
026 * This filter accepts {@code File}s that are symbolic links.
027 * <p>
028 * Example, showing how to print out a list of the current directory's
029 * <em>symbolic link</em> files:
030 * </p>
031 *
032 * <pre>
033 * FileSystemManager fsManager = VFS.getManager();
034 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
035 * FileObject[] files = dir.findFiles(new FileFilterSelector(SymbolicLinkFileFilter.SYMBOLIC));
036 * for (int i = 0; i &lt; files.length; i++) {
037 *     System.out.println(files[i]);
038 * }
039 * </pre>
040 *
041 * <p>
042 * Example, showing how to print out a list of the current directory's
043 * <em>actual</em> (i.e. symbolic link) files:
044 * </p>
045 *
046 * <pre>
047 * FileSystemManager fsManager = VFS.getManager();
048 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
049 * FileObject[] files = dir.findFiles(new FileFilterSelector(SymbolicLinkFileFilter.ACTUAL));
050 * for (int i = 0; i &lt; files.length; i++) {
051 *     System.out.println(files[i]);
052 * }
053 * </pre>
054 *
055 * @since 2.4
056 */
057public class SymbolicLinkFileFilter implements FileFilter, Serializable {
058
059    /** Singleton instance of <em>hidden</em> filter. */
060    public static final FileFilter SYMBOLIC = new SymbolicLinkFileFilter();
061
062    /** Singleton instance of <em>visible</em> filter. */
063    public static final FileFilter ACTUAL = new NotFileFilter(SYMBOLIC);
064
065    private static final long serialVersionUID = 1L;
066
067    /**
068     * Restrictive constructor.
069     */
070    protected SymbolicLinkFileFilter() {
071    }
072
073    /**
074     * Checks to see if the file is a symbolic link. Non-existing files won't be accepted.
075     *
076     * @param fileSelectInfo the file to check
077     * @return {@code true} if the file is <em>symbolic link</em>, otherwise {@code false}.
078     * @throws FileSystemException Thrown for file system errors.
079     */
080    @Override
081    public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException {
082        if (!fileSelectInfo.getFile().exists()) {
083            return false;
084        }
085        return fileSelectInfo.getFile().isSymbolicLink();
086    }
087
088}