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.res;
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 resource file URI.
027 */
028public class ResourceFileName extends AbstractFileName {
029
030    /**
031     * Constructs a new instance.
032     *
033     * @param scheme The scheme.
034     * @param path the absolute path, maybe empty or null.
035     * @param type the file type.
036     */
037    protected ResourceFileName(final String scheme, final String path, final FileType type) {
038        super(scheme, path, type);
039    }
040
041    /**
042     * Builds the root URI for this file name.
043     */
044    @Override
045    protected void appendRootUri(final StringBuilder buffer, final boolean addPassword) {
046        buffer.append(getScheme());
047        buffer.append(":");
048    }
049
050    /**
051     * Factory method for creating name instances.
052     *
053     * @param path The file path.
054     * @param type The file type.
055     * @return The FileName.
056     */
057    @Override
058    public FileName createName(final String path, final FileType type) {
059        return new ResourceFileName(getScheme(), path, type);
060    }
061
062    @Override
063    public String getRootURI() {
064        // resource URIs have a blank root.
065        return "";
066    }
067
068    /**
069     * Returns the decoded URI of the file.
070     *
071     * @return the FileName as a URI.
072     */
073    @Override
074    public String toString() {
075        try {
076            return UriParser.decode(super.getURI());
077        } catch (final FileSystemException e) {
078            return super.getURI();
079        }
080    }
081}