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