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.lang3.StringUtils;
020import org.apache.commons.vfs2.FileName;
021import org.apache.commons.vfs2.FileSystemException;
022import org.apache.commons.vfs2.FileType;
023
024/**
025 * A general-purpose file name parser.
026 */
027public class GenericFileNameParser extends LocalFileNameParser {
028
029    private static final GenericFileNameParser INSTANCE = new GenericFileNameParser();
030
031    /**
032     * Gets the singleton instance, never null.
033     *
034     * @return the singleton instance.
035     */
036    public static GenericFileNameParser getInstance() {
037        return INSTANCE;
038    }
039
040    /**
041     * Constructs a new instance.
042     */
043    public GenericFileNameParser() {
044        // empty
045    }
046
047    /*
048     * Here the rootFileName can only be "/" (see above) put this "/" is also in the
049     * path name so its of no value for the LocalFileName instance
050     */
051    @Override
052    protected FileName createFileName(final String scheme, final String rootFile, final String path,
053            final FileType type) {
054        return new LocalFileName(scheme, "", path, type);
055    }
056
057    /**
058     * Extracts the root prefix from a URI string, which has had the scheme removed.
059     */
060    @Override
061    protected String extractRootPrefix(final String uri, final StringBuilder name) throws FileSystemException {
062        // TODO - this class isn't generic at all. Need to fix this
063        // Looking for "/"
064        final String prefix = "/";
065        if (!StringUtils.startsWith(name, prefix)) {
066            throw new FileSystemException("vfs.provider.local/not-absolute-file-name.error", uri);
067        }
068        // do not strip the separator, BUT also return it ...
069        return prefix;
070    }
071}