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.vfs2.Capability;
25  import org.apache.commons.vfs2.FileName;
26  import org.apache.commons.vfs2.FileObject;
27  import org.apache.commons.vfs2.FileSystem;
28  import org.apache.commons.vfs2.FileSystemException;
29  import org.apache.commons.vfs2.FileSystemOptions;
30  import org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider;
31  import org.apache.commons.vfs2.provider.LocalFileProvider;
32  import org.apache.commons.vfs2.provider.UriParser;
33  import org.apache.commons.vfs2.util.Os;
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(new Capability[] { Capability.CREATE, Capability.DELETE, Capability.RENAME,
43                      Capability.GET_TYPE, 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          super();
53  
54          if (Os.isFamily(Os.OS_FAMILY_WINDOWS)) {
55              setFileNameParser(new WindowsFileNameParser());
56          } else {
57              setFileNameParser(new GenericFileNameParser());
58          }
59      }
60  
61      /**
62       * Determines if a name is an absolute file name.
63       *
64       * @param name The file name.
65       * @return true if the name is absolute, false otherwise.
66       */
67      @Override
68      public boolean isAbsoluteLocalName(final String name) {
69          return ((LocalFileNameParser) getFileNameParser()).isAbsoluteName(name);
70      }
71  
72      /**
73       * Finds a local file, from its local name.
74       *
75       * @param name The name of the file to locate.
76       * @return the located FileObject.
77       * @throws FileSystemException if an error occurs.
78       */
79      @Override
80      public FileObject findLocalFile(final String name) throws FileSystemException {
81          final String scheme = "file:";
82          final StringBuilder uri = new StringBuilder(name.length() + scheme.length());
83          uri.append(scheme);
84          uri.append(name);
85          final FileName fileName = parseUri(null, uri.toString());
86          return findFile(fileName, null);
87      }
88  
89      /**
90       * Finds a local file.
91       *
92       * @param file The File to locate.
93       * @return the located FileObject.
94       * @throws FileSystemException if an error occurs.
95       */
96      @Override
97      public FileObject findLocalFile(final File file) throws FileSystemException {
98          return findLocalFile(UriParser.encode(file.getAbsolutePath()));
99          // return findLocalFile(file.getAbsolutePath());
100     }
101 
102     /**
103      * Creates the file system.
104      */
105     @Override
106     protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
107             throws FileSystemException {
108         // Create the file system
109         final LocalFileName../../../org/apache/commons/vfs2/provider/local/LocalFileName.html#LocalFileName">LocalFileName rootName = (LocalFileName) name;
110         return new LocalFileSystem(rootName, rootName.getRootFile(), fileSystemOptions);
111     }
112 
113     @Override
114     public Collection<Capability> getCapabilities() {
115         return capabilities;
116     }
117 }