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