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 * Generic implementation for any URL based file system, without depending on a specific library.
025 * <p>
026 * Parses the URL into user/password/host/port/path/queryString.
027 * </p>
028 */
029public class GenericURLFileNameParser extends HostFileNameParser {
030
031    /**
032     * Constructs a new instance.
033     *
034     * @param defaultPort The default port.
035     */
036    public GenericURLFileNameParser(final int defaultPort) {
037        super(defaultPort);
038    }
039
040    @Override
041    public boolean encodeCharacter(final char ch) {
042        return super.encodeCharacter(ch) || ch == '?';
043    }
044
045    @Override
046    public FileName parseUri(final VfsComponentContext context, final FileName base, final String fileName)
047            throws FileSystemException {
048        // FTP URI are generic URI (as per RFC 2396)
049        final StringBuilder name = new StringBuilder();
050
051        // Extract the scheme and authority parts
052        final Authority auth = extractToPath(context, fileName, name);
053
054        // Extract the queryString
055        final String queryString = UriParser.extractQueryString(name);
056
057        // Decode and normalise the file name
058        UriParser.canonicalizePath(name, 0, name.length(), this);
059        UriParser.fixSeparators(name);
060        final FileType fileType = UriParser.normalisePath(name);
061        final String path = name.toString();
062
063        return new GenericURLFileName(auth.getScheme(), auth.getHostName(), auth.getPort(), getDefaultPort(),
064                auth.getUserName(), auth.getPassword(), path, fileType, queryString);
065    }
066}