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