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     */
017    package org.apache.commons.vfs2.provider;
018    
019    import org.apache.commons.vfs2.FileName;
020    import org.apache.commons.vfs2.FileSystemException;
021    import 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     *
027     * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
028     */
029    public class URLFileNameParser extends HostFileNameParser
030    {
031        public URLFileNameParser(final int defaultPort)
032        {
033            super(defaultPort);
034        }
035    
036        @Override
037        public boolean encodeCharacter(char ch)
038        {
039            return super.encodeCharacter(ch) || ch == '?';
040        }
041    
042        @Override
043        public FileName parseUri(final VfsComponentContext context, FileName base, final String filename)
044                throws FileSystemException
045        {
046            // FTP URI are generic URI (as per RFC 2396)
047            final StringBuilder name = new StringBuilder();
048    
049            // Extract the scheme and authority parts
050            final Authority auth = extractToPath(filename, name);
051    
052            // Extract the queryString
053            String queryString = UriParser.extractQueryString(name);
054    
055            // Decode and normalise the file name
056            UriParser.canonicalizePath(name, 0, name.length(), this);
057            UriParser.fixSeparators(name);
058            FileType fileType = UriParser.normalisePath(name);
059            final String path = name.toString();
060    
061            return new URLFileName(
062                auth.getScheme(),
063                auth.getHostName(),
064                auth.getPort(),
065                getDefaultPort(),
066                auth.getUserName(),
067                auth.getPassword(),
068                path,
069                fileType,
070                queryString);
071        }
072    }