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 */
017package org.apache.commons.vfs2.provider.local;
018
019import java.io.File;
020import java.util.Arrays;
021import java.util.Collection;
022import java.util.Collections;
023
024import org.apache.commons.vfs2.Capability;
025import org.apache.commons.vfs2.FileName;
026import org.apache.commons.vfs2.FileObject;
027import org.apache.commons.vfs2.FileSystem;
028import org.apache.commons.vfs2.FileSystemException;
029import org.apache.commons.vfs2.FileSystemOptions;
030import org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider;
031import org.apache.commons.vfs2.provider.LocalFileProvider;
032import org.apache.commons.vfs2.provider.UriParser;
033import org.apache.commons.vfs2.util.Os;
034
035/**
036 * A file system provider, which uses direct file access.
037 */
038public class DefaultLocalFileProvider
039    extends AbstractOriginatingFileProvider
040    implements LocalFileProvider
041{
042    /** The provider's capabilities. */
043    public static final Collection<Capability> capabilities =
044        Collections.unmodifiableCollection(Arrays.asList(new Capability[]
045    {
046        Capability.CREATE,
047        Capability.DELETE,
048        Capability.RENAME,
049        Capability.GET_TYPE,
050        Capability.GET_LAST_MODIFIED,
051        Capability.SET_LAST_MODIFIED_FILE,
052        Capability.SET_LAST_MODIFIED_FOLDER,
053        Capability.LIST_CHILDREN,
054        Capability.READ_CONTENT,
055        Capability.URI,
056        Capability.WRITE_CONTENT,
057        Capability.APPEND_CONTENT,
058        Capability.RANDOM_ACCESS_READ,
059        Capability.RANDOM_ACCESS_SET_LENGTH,
060        Capability.RANDOM_ACCESS_WRITE
061    }));
062
063    /**
064     * Constructs a new provider.
065     */
066    public DefaultLocalFileProvider()
067    {
068        super();
069
070        if (Os.isFamily(Os.OS_FAMILY_WINDOWS))
071        {
072            setFileNameParser(new WindowsFileNameParser());
073        }
074        else
075        {
076            setFileNameParser(new GenericFileNameParser());
077        }
078    }
079
080    /**
081     * Determines if a name is an absolute file name.
082     * @param name The file name.
083     * @return true if the name is absolute, false otherwise.
084     */
085    @Override
086    public boolean isAbsoluteLocalName(final String name)
087    {
088        return ((LocalFileNameParser) getFileNameParser()).isAbsoluteName(name);
089    }
090
091    /**
092     * Finds a local file, from its local name.
093     * @param name The name of the file to locate.
094     * @return the located FileObject.
095     * @throws FileSystemException if an error occurs.
096     */
097    @Override
098    public FileObject findLocalFile(final String name)
099        throws FileSystemException
100    {
101        final String scheme = "file:";
102        final StringBuilder uri = new StringBuilder(name.length() + scheme.length());
103        uri.append(scheme);
104        uri.append(name);
105        final FileName filename = parseUri(null, uri.toString());
106        return findFile(filename, null);
107    }
108
109    /**
110     * Finds a local file.
111     * @param file The File to locate.
112     * @return the located FileObject.
113     * @throws FileSystemException if an error occurs.
114     */
115    @Override
116    public FileObject findLocalFile(final File file)
117        throws FileSystemException
118    {
119        return findLocalFile(UriParser.encode(file.getAbsolutePath()));
120        // return findLocalFile(file.getAbsolutePath());
121    }
122
123    /**
124     * Creates the filesystem.
125     */
126    @Override
127    protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
128        throws FileSystemException
129    {
130        // Create the file system
131        final LocalFileName rootName = (LocalFileName) name;
132        return new LocalFileSystem(rootName, rootName.getRootFile(), fileSystemOptions);
133    }
134
135    @Override
136    public Collection<Capability> getCapabilities()
137    {
138        return capabilities;
139    }
140}