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      @Override
63      protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
64              throws FileSystemException {
65          // Create the file system
66          final LocalFileName../../../org/apache/commons/vfs2/provider/local/LocalFileName.html#LocalFileName">LocalFileName rootName = (LocalFileName) name;
67          return new LocalFileSystem(rootName, rootName.getRootFile(), fileSystemOptions);
68      }
69  
70      /**
71       * Finds a local file.
72       *
73       * @param file The File to locate.
74       * @return the located FileObject.
75       * @throws FileSystemException if an error occurs.
76       */
77      @Override
78      public FileObject findLocalFile(final File file) throws FileSystemException {
79          return findLocalFile(UriParser.encode(file.getAbsolutePath()));
80          // return findLocalFile(file.getAbsolutePath());
81      }
82  
83      /**
84       * Finds a local file, from its local name.
85       *
86       * @param name The name of the file to locate.
87       * @return the located FileObject.
88       * @throws FileSystemException if an error occurs.
89       */
90      @Override
91      public FileObject findLocalFile(final String name) throws FileSystemException {
92          final String scheme = "file:";
93          final StringBuilder uri = new StringBuilder(name.length() + scheme.length());
94          uri.append(scheme);
95          uri.append(name);
96          final FileName fileName = parseUri(null, uri.toString());
97          return findFile(fileName, null);
98      }
99  
100     @Override
101     public Collection<Capability> getCapabilities() {
102         return capabilities;
103     }
104 
105     /**
106      * Determines if a name is an absolute file name.
107      *
108      * @param name The file name.
109      * @return true if the name is absolute, false otherwise.
110      */
111     @Override
112     public boolean isAbsoluteLocalName(final String name) {
113         return ((LocalFileNameParser) getFileNameParser()).isAbsoluteName(name);
114     }
115 }