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