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 extends AbstractOriginatingFileProvider implements LocalFileProvider {
039    /** The provider's capabilities. */
040    public static final Collection<Capability> capabilities = Collections.unmodifiableCollection(
041            Arrays.asList(new Capability[] { Capability.CREATE, Capability.DELETE, Capability.RENAME,
042                    Capability.GET_TYPE, Capability.GET_LAST_MODIFIED, Capability.SET_LAST_MODIFIED_FILE,
043                    Capability.SET_LAST_MODIFIED_FOLDER, Capability.LIST_CHILDREN, Capability.READ_CONTENT,
044                    Capability.URI, Capability.WRITE_CONTENT, Capability.APPEND_CONTENT, Capability.RANDOM_ACCESS_READ,
045                    Capability.RANDOM_ACCESS_SET_LENGTH, Capability.RANDOM_ACCESS_WRITE }));
046
047    /**
048     * Constructs a new provider.
049     */
050    public DefaultLocalFileProvider() {
051        super();
052
053        if (Os.isFamily(Os.OS_FAMILY_WINDOWS)) {
054            setFileNameParser(new WindowsFileNameParser());
055        } else {
056            setFileNameParser(new GenericFileNameParser());
057        }
058    }
059
060    /**
061     * Determines if a name is an absolute file name.
062     *
063     * @param name The file name.
064     * @return true if the name is absolute, false otherwise.
065     */
066    @Override
067    public boolean isAbsoluteLocalName(final String name) {
068        return ((LocalFileNameParser) getFileNameParser()).isAbsoluteName(name);
069    }
070
071    /**
072     * Finds a local file, from its local name.
073     *
074     * @param name The name of the file to locate.
075     * @return the located FileObject.
076     * @throws FileSystemException if an error occurs.
077     */
078    @Override
079    public FileObject findLocalFile(final String name) throws FileSystemException {
080        final String scheme = "file:";
081        final StringBuilder uri = new StringBuilder(name.length() + scheme.length());
082        uri.append(scheme);
083        uri.append(name);
084        final FileName filename = parseUri(null, uri.toString());
085        return findFile(filename, null);
086    }
087
088    /**
089     * Finds a local file.
090     *
091     * @param file The File to locate.
092     * @return the located FileObject.
093     * @throws FileSystemException if an error occurs.
094     */
095    @Override
096    public FileObject findLocalFile(final File file) throws FileSystemException {
097        return findLocalFile(UriParser.encode(file.getAbsolutePath()));
098        // return findLocalFile(file.getAbsolutePath());
099    }
100
101    /**
102     * Creates the filesystem.
103     */
104    @Override
105    protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
106            throws FileSystemException {
107        // Create the file system
108        final LocalFileName rootName = (LocalFileName) name;
109        return new LocalFileSystem(rootName, rootName.getRootFile(), fileSystemOptions);
110    }
111
112    @Override
113    public Collection<Capability> getCapabilities() {
114        return capabilities;
115    }
116}