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.AbstractFileNameParser;
023import org.apache.commons.vfs2.provider.UriParser;
024import org.apache.commons.vfs2.provider.VfsComponentContext;
025
026/**
027 * A name parser.
028 */
029public abstract class LocalFileNameParser extends AbstractFileNameParser {
030    /**
031     * Determines if a name is an absolute file name.
032     *
033     * @param name The file name.
034     * @return true if the name is absolute, false otherwise.
035     */
036    public boolean isAbsoluteName(final String name) {
037        // TODO - this is yucky
038        final StringBuilder b = new StringBuilder(name);
039        try {
040            UriParser.fixSeparators(b);
041            extractRootPrefix(name, b);
042            return true;
043        } catch (final FileSystemException e) {
044            return false;
045        }
046    }
047
048    /**
049     * Pops the root prefix off a URI, which has had the scheme removed.
050     *
051     * @param name the URI to modify.
052     * @param uri the whole URI for error reporting.
053     * @return the root prefix extracted.
054     * @throws FileSystemException if an error occurs.
055     */
056    protected abstract String extractRootPrefix(final String uri, final StringBuilder name) throws FileSystemException;
057
058    @Override
059    public FileName parseUri(final VfsComponentContext context, final FileName base, final String uri)
060            throws FileSystemException {
061        final StringBuilder name = new StringBuilder();
062
063        // Extract the scheme
064        String scheme = UriParser.extractScheme(uri, name);
065        if (scheme == null) {
066            scheme = "file";
067        }
068
069        // Remove encoding, and adjust the separators
070        UriParser.canonicalizePath(name, 0, name.length(), this);
071
072        UriParser.fixSeparators(name);
073
074        // Extract the root prefix
075        final String rootFile = extractRootPrefix(uri, name);
076
077        // Normalise the path
078        final FileType fileType = UriParser.normalisePath(name);
079
080        final String path = name.toString();
081
082        return createFileName(scheme, rootFile, path, fileType);
083    }
084
085    protected abstract FileName createFileName(String scheme, final String rootFile, final String path,
086            final FileType type);
087}