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.provider.local;
18  
19  import java.io.File;
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.Collections;
23  
24  import org.apache.commons.lang3.SystemUtils;
25  import org.apache.commons.vfs2.Capability;
26  import org.apache.commons.vfs2.FileName;
27  import org.apache.commons.vfs2.FileObject;
28  import org.apache.commons.vfs2.FileSystem;
29  import org.apache.commons.vfs2.FileSystemException;
30  import org.apache.commons.vfs2.FileSystemOptions;
31  import org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider;
32  import org.apache.commons.vfs2.provider.LocalFileProvider;
33  import org.apache.commons.vfs2.provider.UriParser;
34  
35  /**
36   * A file system provider, which uses direct file access.
37   */
38  public class DefaultLocalFileProvider extends AbstractOriginatingFileProvider implements LocalFileProvider {
39  
40      /** The provider's capabilities. */
41      public static final Collection<Capability> capabilities = Collections.unmodifiableCollection(
42              Arrays.asList(Capability.CREATE, Capability.DELETE, Capability.RENAME, Capability.GET_TYPE,
43                      Capability.GET_LAST_MODIFIED, Capability.SET_LAST_MODIFIED_FILE,
44                      Capability.SET_LAST_MODIFIED_FOLDER, Capability.LIST_CHILDREN, Capability.READ_CONTENT,
45                      Capability.URI, Capability.WRITE_CONTENT, Capability.APPEND_CONTENT, Capability.RANDOM_ACCESS_READ,
46                      Capability.RANDOM_ACCESS_SET_LENGTH, Capability.RANDOM_ACCESS_WRITE));
47  
48      /**
49       * Constructs a new provider.
50       */
51      public DefaultLocalFileProvider() {
52          if (SystemUtils.IS_OS_WINDOWS) {
53              setFileNameParser(new WindowsFileNameParser());
54          } else {
55              setFileNameParser(new GenericFileNameParser());
56          }
57      }
58  
59      /**
60       * Creates the file system.
61       *
62       * @return a new FileSystem, never null.
63       */
64      @Override
65      protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
66              throws FileSystemException {
67          // Create the file system
68          final LocalFileName rootName = (LocalFileName) name;
69          return new LocalFileSystem(rootName, rootName.getRootFile(), fileSystemOptions);
70      }
71  
72      /**
73       * Finds a local file.
74       *
75       * @param file The File to locate.
76       * @return the located FileObject.
77       * @throws FileSystemException if an error occurs.
78       */
79      @Override
80      public FileObject findLocalFile(final File file) throws FileSystemException {
81          return findLocalFile(UriParser.encode(file.getAbsolutePath()));
82      }
83  
84      /**
85       * Finds a local file, from its local name.
86       *
87       * @param name The name of the file to locate.
88       * @return the located FileObject.
89       * @throws FileSystemException if an error occurs.
90       */
91      @Override
92      public FileObject findLocalFile(final String name) throws FileSystemException {
93          final String scheme = "file:";
94          final StringBuilder builder = new StringBuilder(name.length() + scheme.length());
95          return findFile(parseUri(null, builder.append(scheme).append(name).toString()), null);
96      }
97  
98      @Override
99      public Collection<Capability> getCapabilities() {
100         return capabilities;
101     }
102 
103     /**
104      * Determines if a name is an absolute file name.
105      *
106      * @param name The file name.
107      * @return true if the name is absolute, false otherwise.
108      */
109     @Override
110     public boolean isAbsoluteLocalName(final String name) {
111         return ((LocalFileNameParser) getFileNameParser()).isAbsoluteName(name);
112     }
113 }