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;
022
023/**
024 * A general-purpose file name parser.
025 */
026public class GenericFileNameParser extends LocalFileNameParser {
027    private static final GenericFileNameParser INSTANCE = new GenericFileNameParser();
028
029    /**
030     * retrieve a instance to this parser.
031     *
032     * @return the parser
033     */
034    public static GenericFileNameParser getInstance() {
035        return INSTANCE;
036    }
037
038    /**
039     * Pops the root prefix off a URI, which has had the scheme removed.
040     */
041    @Override
042    protected String extractRootPrefix(final String uri, final StringBuilder name) throws FileSystemException {
043        // TODO - this class isn't generic at all. Need to fix this
044
045        // Looking for <sep>
046        if (name.length() == 0 || name.charAt(0) != '/') {
047            throw new FileSystemException("vfs.provider.local/not-absolute-file-name.error", uri);
048        }
049
050        // do not strip the separator, BUT also return it ...
051        return "/";
052    }
053
054    /*
055     * ... this is why whe need this: here the rootFilename can only be "/" (see above) put this "/" is also in the
056     * pathname so its of no value for the LocalFileName instance
057     */
058    @Override
059    protected FileName createFileName(final String scheme, final String rootFile, final String path,
060            final FileType type) {
061        return new LocalFileName(scheme, "", path, type);
062    }
063}