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;
018
019import org.apache.commons.vfs2.FileName;
020import org.apache.commons.vfs2.FileSystemException;
021import org.apache.commons.vfs2.FileType;
022
023/**
024 * Implementation for layered filesystems.
025 * <p>
026 * Additionally encodes the '!' character.
027 */
028public class LayeredFileNameParser extends AbstractFileNameParser {
029    private static final LayeredFileNameParser INSTANCE = new LayeredFileNameParser();
030
031    /**
032     * Return the Parser.
033     *
034     * @return The Parser.
035     */
036    public static LayeredFileNameParser getInstance() {
037        return INSTANCE;
038    }
039
040    /**
041     * Determines if a character should be encoded.
042     *
043     * @param ch The character to check.
044     * @return true if the character should be encoded.
045     */
046    @Override
047    public boolean encodeCharacter(final char ch) {
048        return super.encodeCharacter(ch) || ch == '!';
049    }
050
051    /**
052     * Parse the base and name into a FileName.
053     *
054     * @param context The component context.
055     * @param base The base FileName.
056     * @param filename The target file name.
057     * @return The constructed FileName.
058     * @throws FileSystemException if an error occurs.
059     */
060    @Override
061    public FileName parseUri(final VfsComponentContext context, final FileName base, final String filename)
062            throws FileSystemException {
063        final StringBuilder name = new StringBuilder();
064
065        // Extract the scheme
066        final String scheme = UriParser.extractScheme(filename, name);
067
068        // Extract the Layered file URI
069        final String rootUriName = extractRootName(name);
070        FileName rootUri = null;
071        if (rootUriName != null) {
072            rootUri = context.parseURI(rootUriName);
073        }
074
075        // Decode and normalise the path
076        UriParser.canonicalizePath(name, 0, name.length(), this);
077        UriParser.fixSeparators(name);
078        final FileType fileType = UriParser.normalisePath(name);
079        final String path = name.toString();
080
081        return new LayeredFileName(scheme, rootUri, path, fileType);
082    }
083
084    /**
085     * Pops the root prefix off a URI, which has had the scheme removed.
086     *
087     * @param uri string builder which gets modified.
088     * @return the extracted root name.
089     * @throws FileSystemException if error occurs.
090     */
091    protected String extractRootName(final StringBuilder uri) throws FileSystemException {
092        // Looking for <name>!<abspath> (staring at the end)
093        final int maxlen = uri.length();
094        int pos = maxlen - 1;
095        for (; pos > 0 && uri.charAt(pos) != '!'; pos--) {
096        }
097
098        if (pos == 0 && uri.charAt(pos) != '!') {
099            // not ! found, so take the whole path a root
100            // e.g. zip:/my/zip/file.zip
101            pos = maxlen;
102        }
103
104        // Extract the name
105        final String prefix = uri.substring(0, pos);
106        if (pos < maxlen) {
107            uri.delete(0, pos + 1);
108        } else {
109            uri.setLength(0);
110        }
111
112        return prefix;
113    }
114
115}