001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.vfs2.provider.local;
018    
019    import java.io.File;
020    import java.util.Arrays;
021    import java.util.Collection;
022    import java.util.Collections;
023    
024    import org.apache.commons.vfs2.Capability;
025    import org.apache.commons.vfs2.FileName;
026    import org.apache.commons.vfs2.FileObject;
027    import org.apache.commons.vfs2.FileSystem;
028    import org.apache.commons.vfs2.FileSystemException;
029    import org.apache.commons.vfs2.FileSystemOptions;
030    import org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider;
031    import org.apache.commons.vfs2.provider.LocalFileProvider;
032    import org.apache.commons.vfs2.provider.UriParser;
033    import org.apache.commons.vfs2.util.Os;
034    
035    /**
036     * A file system provider, which uses direct file access.
037     *
038     * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
039     */
040    public class DefaultLocalFileProvider
041        extends AbstractOriginatingFileProvider
042        implements LocalFileProvider
043    {
044        /** The provider's capabilities. */
045        public static final Collection<Capability> capabilities =
046            Collections.unmodifiableCollection(Arrays.asList(new Capability[]
047        {
048            Capability.CREATE,
049            Capability.DELETE,
050            Capability.RENAME,
051            Capability.GET_TYPE,
052            Capability.GET_LAST_MODIFIED,
053            Capability.SET_LAST_MODIFIED_FILE,
054            Capability.SET_LAST_MODIFIED_FOLDER,
055            Capability.LIST_CHILDREN,
056            Capability.READ_CONTENT,
057            Capability.URI,
058            Capability.WRITE_CONTENT,
059            Capability.APPEND_CONTENT,
060            Capability.RANDOM_ACCESS_READ,
061            Capability.RANDOM_ACCESS_WRITE
062        }));
063    
064        public DefaultLocalFileProvider()
065        {
066            super();
067    
068            if (Os.isFamily(Os.OS_FAMILY_WINDOWS))
069            {
070                setFileNameParser(new WindowsFileNameParser());
071            }
072            else
073            {
074                setFileNameParser(new GenericFileNameParser());
075            }
076        }
077    
078        /**
079         * Determines if a name is an absolute file name.
080         * @param name The file name.
081         * @return true if the name is absolute, false otherwise.
082         */
083        public boolean isAbsoluteLocalName(final String name)
084        {
085            return ((LocalFileNameParser) getFileNameParser()).isAbsoluteName(name);
086        }
087    
088        /**
089         * Finds a local file, from its local name.
090         * @param name The name of the file to locate.
091         * @return the located FileObject.
092         * @throws FileSystemException if an error occurs.
093         */
094        public FileObject findLocalFile(final String name)
095            throws FileSystemException
096        {
097            StringBuilder uri = new StringBuilder(name.length() + 5);
098            uri.append("file:");
099            uri.append(name);
100            FileName filename = parseUri(null, uri.toString());
101            return findFile(filename, null);
102        }
103    
104        /**
105         * Finds a local file.
106         * @param file The File to locate.
107         * @return the located FileObject.
108         * @throws FileSystemException if an error occurs.
109         */
110        public FileObject findLocalFile(final File file)
111            throws FileSystemException
112        {
113            return findLocalFile(UriParser.encode(file.getAbsolutePath()));
114            // return findLocalFile(file.getAbsolutePath());
115        }
116    
117        /**
118         * Creates the filesystem.
119         */
120        @Override
121        protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
122            throws FileSystemException
123        {
124            // Create the file system
125            final LocalFileName rootName = (LocalFileName) name;
126            return new LocalFileSystem(rootName, rootName.getRootFile(), fileSystemOptions);
127        }
128    
129        public Collection<Capability> getCapabilities()
130        {
131            return capabilities;
132        }
133    }