/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.commons.vfs2.filter;

import java.io.Serializable;

import org.apache.commons.vfs2.Capability;
import org.apache.commons.vfs2.FileFilter;
import org.apache.commons.vfs2.FileSelectInfo;
import org.apache.commons.vfs2.FileSystem;
import org.apache.commons.vfs2.FileSystemException;

/**
 * This filter accepts <code>File</code>s that can be written to.
 * <p>
 * Example, showing how to print out a list of the current directory's
 * <i>writable</i> files:
 * </p>
 *
 * <pre>
 * FileSystemManager fsManager = VFS.getManager();
 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
 * FileObject[] files = dir.findFiles(new FileFilterSelector(CanWriteFileFilter.CAN_WRITE));
 * for (int i = 0; i &lt; files.length; i++) {
 *     System.out.println(files[i]);
 * }
 * </pre>
 *
 * <p>
 * Example, showing how to print out a list of the current directory's
 * <i>un-writable</i> files:
 * </p>
 *
 * <pre>
 * FileSystemManager fsManager = VFS.getManager();
 * FileObject dir = fsManager.toFileObject(new File(&quot;.&quot;));
 * FileObject[] files = dir.findFiles(new FileFilterSelector(CanWriteFileFilter.CANNOT_WRITE));
 * for (int i = 0; i &lt; files.length; i++) {
 *     System.out.println(files[i]);
 * }
 * </pre>
 *
 * <p>
 * <b>N.B.</b> For read-only files, use
 * <code>CanReadFileFilter.READ_ONLY</code>.
 * </p>
 *
 * @author This code was originally ported from Apache Commons IO File Filter
 * @see "http://commons.apache.org/proper/commons-io/"
 * @since 2.4
 */
public class CanWriteFileFilter implements FileFilter, Serializable {

    private static final long serialVersionUID = 1L;

    /** Singleton instance of <i>writable</i> filter. */
    public static final FileFilter CAN_WRITE = new CanWriteFileFilter();

    /** Singleton instance of not <i>writable</i> filter. */
    public static final FileFilter CANNOT_WRITE = new NotFileFilter(CAN_WRITE);

    /**
     * Restrictive constructor.
     */
    protected CanWriteFileFilter() {
    }

    /**
     * Checks to see if the file can be written to.
     *
     * @param fileInfo the File to check
     *
     * @return {@code true} if the file can be written to, otherwise {@code false}.
     * @throws FileSystemException Thrown for file system errors.
     */
    @Override
    public boolean accept(final FileSelectInfo fileInfo) throws FileSystemException {
        final FileSystem fileSystem = fileInfo.getFile().getFileSystem();
        if (fileInfo.getFile().exists()) {
            if (!fileSystem.hasCapability(Capability.WRITE_CONTENT)) {
                return false;
            }
            return fileInfo.getFile().isWriteable();
        }
        if (!fileSystem.hasCapability(Capability.CREATE)) {
            return false;
        }
        return fileInfo.getFile().getParent().isWriteable();
    }

}

@@ -975,16 +975,60 @@
 ilter;%0D%0A
+import org.apache.commons.vfs2.FileObject;%0D%0A
 import o
@@ -3310,16 +3310,21 @@
         
+try (
 final Fi
@@ -3329,25 +3329,19 @@
 File
-System fileSystem
+Object file
  = f
@@ -3353,24 +3353,75 @@
 fo.getFile()
+) %7B%0D%0A            final FileSystem fileSystem = file
 .getFileSyst
@@ -3439,16 +3439,20 @@
     
+    
 if (file
 Info
@@ -3447,30 +3447,16 @@
 if (file
-Info.getFile()
 .exists(
@@ -3457,24 +3457,28 @@
 xists()) %7B%0D%0A
+    
             
@@ -3545,32 +3545,36 @@
                 
+    
 return false;%0D%0A 
@@ -3576,32 +3576,36 @@
 e;%0D%0A            
+    
 %7D%0D%0A            r
@@ -3595,32 +3595,36 @@
  %7D%0D%0A            
+  
+  
 return fileInfo.
@@ -3618,30 +3618,16 @@
 urn file
-Info.getFile()
 .isWrite
@@ -3631,24 +3631,28 @@
 iteable();%0D%0A
+    
         %7D%0D%0A 
@@ -3650,32 +3650,36 @@
      %7D%0D%0A        
+    
 if (!fileSystem.
@@ -3719,32 +3719,36 @@
  %7B%0D%0A            
+    
 return false;%0D%0A 
@@ -3746,32 +3746,36 @@
 false;%0D%0A        
+    
 %7D%0D%0A        retur
@@ -3761,32 +3761,36 @@
      %7D%0D%0A        
+ 
+   
 return fileInfo.
@@ -3784,30 +3784,16 @@
 urn file
-Info.getFile()
 .getPare
@@ -3809,24 +3809,35 @@
 iteable();%0D%0A
+        %7D%0D%0A
     %7D%0D%0A%0D%0A%7D%0D%0A

Refactor multiple API calls.