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 org.apache.commons.vfs2.FileName;
020import org.apache.commons.vfs2.FileSystemException;
021import org.apache.commons.vfs2.FileType;
022import org.apache.commons.vfs2.provider.AbstractFileName;
023import org.apache.commons.vfs2.provider.UriParser;
024
025/**
026 * A local file URI.
027 */
028public class LocalFileName extends AbstractFileName {
029
030    private final String rootFile;
031
032    /**
033     * Constructs a new instance.
034     *
035     * @param scheme the scheme.
036     * @param rootFile the root file.
037     * @param path the absolute path, maybe empty or null.
038     * @param type the file type.
039     */
040    protected LocalFileName(final String scheme, final String rootFile, final String path, final FileType type) {
041        super(scheme, path, type);
042        this.rootFile = rootFile;
043    }
044
045    /**
046     * Builds the root URI for this file name.
047     */
048    @Override
049    protected void appendRootUri(final StringBuilder buffer, final boolean addPassword) {
050        buffer.append(getScheme());
051        buffer.append("://");
052        buffer.append(rootFile);
053    }
054
055    /**
056     * Factory method for creating name instances.
057     *
058     * @param path The file path.
059     * @param type The file type.
060     * @return The FileName.
061     */
062    @Override
063    public FileName createName(final String path, final FileType type) {
064        return new LocalFileName(getScheme(), rootFile, path, type);
065    }
066
067    /**
068     * Returns the root file for this file.
069     *
070     * @return The root file name.
071     */
072    public String getRootFile() {
073        return rootFile;
074    }
075
076    /**
077     * Returns the decoded URI of the file.
078     *
079     * @return the FileName as a URI.
080     */
081    @Override
082    public String toString() {
083        try {
084            return UriParser.decode(super.getURI());
085        } catch (final FileSystemException e) {
086            return super.getURI();
087        }
088    }
089}