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.vfs.provider.local;
18  
19  import org.apache.commons.vfs.Capability;
20  import org.apache.commons.vfs.FileName;
21  import org.apache.commons.vfs.FileObject;
22  import org.apache.commons.vfs.FileSystem;
23  import org.apache.commons.vfs.FileSystemException;
24  import org.apache.commons.vfs.FileSystemOptions;
25  import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
26  import org.apache.commons.vfs.provider.LocalFileProvider;
27  import org.apache.commons.vfs.provider.UriParser;
28  import org.apache.commons.vfs.util.Os;
29  
30  import java.io.File;
31  import java.util.Arrays;
32  import java.util.Collection;
33  import java.util.Collections;
34  
35  /***
36   * A file system provider, which uses direct file access.
37   *
38   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
39   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
40   */
41  public class DefaultLocalFileProvider
42      extends AbstractOriginatingFileProvider
43      implements LocalFileProvider
44  {
45      public final static Collection capabilities = Collections.unmodifiableCollection(Arrays.asList(new Capability[]
46      {
47          Capability.CREATE,
48          Capability.DELETE,
49          Capability.RENAME,
50          Capability.GET_TYPE,
51          Capability.GET_LAST_MODIFIED,
52          Capability.SET_LAST_MODIFIED_FILE,
53          Capability.SET_LAST_MODIFIED_FOLDER,
54          Capability.LIST_CHILDREN,
55          Capability.READ_CONTENT,
56          Capability.URI,
57          Capability.WRITE_CONTENT,
58          Capability.APPEND_CONTENT,
59          Capability.RANDOM_ACCESS_READ,
60          Capability.RANDOM_ACCESS_WRITE
61      }));
62  
63      public DefaultLocalFileProvider()
64      {
65          super();
66  
67          if (Os.isFamily(Os.OS_FAMILY_WINDOWS))
68          {
69              setFileNameParser(new WindowsFileNameParser());
70          }
71          else
72          {
73              setFileNameParser(new GenericFileNameParser());
74          }
75      }
76  
77      /***
78       * Determines if a name is an absolute file name.
79       */
80      public boolean isAbsoluteLocalName(final String name)
81      {
82          return ((LocalFileNameParser) getFileNameParser()).isAbsoluteName(name);
83      }
84  
85      /***
86       * Finds a local file, from its local name.
87       */
88      public FileObject findLocalFile(final String name)
89          throws FileSystemException
90      {
91          StringBuffer uri = new StringBuffer(name.length() + 5);
92          uri.append("file:");
93          uri.append(name);
94          FileName filename = parseUri(null, uri.toString());
95          return findFile(filename, null);
96      }
97  
98      /***
99       * Finds a local file.
100      */
101     public FileObject findLocalFile(final File file)
102         throws FileSystemException
103     {
104         return findLocalFile(UriParser.encode(file.getAbsolutePath()));
105         // return findLocalFile(file.getAbsolutePath());
106     }
107 
108     /***
109      * Creates the filesystem.
110      */
111     protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
112         throws FileSystemException
113     {
114         // Create the file system
115         final LocalFileName rootName = (LocalFileName) name;
116         return new LocalFileSystem(rootName, rootName.getRootFile(), fileSystemOptions);
117     }
118 
119     public Collection getCapabilities()
120     {
121         return capabilities;
122     }
123 }