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.Capability;
22  import org.apache.commons.vfs2.FileFilter;
23  import org.apache.commons.vfs2.FileObject;
24  import org.apache.commons.vfs2.FileSelectInfo;
25  import org.apache.commons.vfs2.FileSystem;
26  import org.apache.commons.vfs2.FileSystemException;
27  
28  /**
29   * This filter accepts {@code File}s that can be written to.
30   * <p>
31   * Example, showing how to print out a list of the current directory's
32   * <i>writable</i> files:
33   * </p>
34   *
35   * <pre>
36   * FileSystemManager fsManager = VFS.getManager();
37   * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
38   * FileObject[] files = dir.findFiles(new FileFilterSelector(CanWriteFileFilter.CAN_WRITE));
39   * for (int i = 0; i &lt; files.length; i++) {
40   *     System.out.println(files[i]);
41   * }
42   * </pre>
43   *
44   * <p>
45   * Example, showing how to print out a list of the current directory's
46   * <i>un-writable</i> files:
47   * </p>
48   *
49   * <pre>
50   * FileSystemManager fsManager = VFS.getManager();
51   * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
52   * FileObject[] files = dir.findFiles(new FileFilterSelector(CanWriteFileFilter.CANNOT_WRITE));
53   * for (int i = 0; i &lt; files.length; i++) {
54   *     System.out.println(files[i]);
55   * }
56   * </pre>
57   *
58   * <p>
59   * <b>N.B.</b> For read-only files, use
60   * {@code CanReadFileFilter.READ_ONLY}.
61   * </p>
62   *
63   * @author This code was originally ported from Apache Commons IO File Filter
64   * @see "http://commons.apache.org/proper/commons-io/"
65   * @since 2.4
66   */
67  public class CanWriteFileFilter implements FileFilter, Serializable {
68  
69      private static final long serialVersionUID = 1L;
70  
71      /** Singleton instance of <i>writable</i> filter. */
72      public static final FileFilter CAN_WRITE = new CanWriteFileFilter();
73  
74      /** Singleton instance of not <i>writable</i> filter. */
75      public static final FileFilter CANNOT_WRITE = new NotFileFilter(CAN_WRITE);
76  
77      /**
78       * Restrictive constructor.
79       */
80      protected CanWriteFileFilter() {
81      }
82  
83      /**
84       * Checks to see if the file can be written to.
85       *
86       * @param fileSelectInfo the File to check
87       *
88       * @return {@code true} if the file can be written to, otherwise {@code false}.
89       * @throws FileSystemException Thrown for file system errors.
90       */
91      @Override
92      public boolean accept(final FileSelectInfo fileSelectInfo) throws FileSystemException {
93          try (final FileObject file = fileSelectInfo.getFile()) {
94              final FileSystem fileSystem = file.getFileSystem();
95              if (file.exists()) {
96                  if (!fileSystem.hasCapability(Capability.WRITE_CONTENT)) {
97                      return false;
98                  }
99                  return file.isWriteable();
100             }
101             if (!fileSystem.hasCapability(Capability.CREATE)) {
102                 return false;
103             }
104             return file.getParent().isWriteable();
105         }
106     }
107 
108 }