⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uriutil.java

📁 Light in the box 抓取程序。 使用HttpClient
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/util/URIUtil.java,v 1.27 2004/05/05 20:34:01 olegk Exp $ * $Revision: 507321 $ * $Date: 2007-02-14 01:10:51 +0100 (Wed, 14 Feb 2007) $ * * ==================================================================== * *  Licensed to the Apache Software Foundation (ASF) under one or more *  contributor license agreements.  See the NOTICE file distributed with *  this work for additional information regarding copyright ownership. *  The ASF licenses this file to You under the Apache License, Version 2.0 *  (the "License"); you may not use this file except in compliance with *  the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * *  Unless required by applicable law or agreed to in writing, software *  distributed under the License is distributed on an "AS IS" BASIS, *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *  See the License for the specific language governing permissions and *  limitations under the License. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */package org.apache.commons.httpclient.util;import java.util.BitSet;import org.apache.commons.codec.DecoderException;import org.apache.commons.codec.net.URLCodec;import org.apache.commons.httpclient.URI;import org.apache.commons.httpclient.URIException;/** * The URI escape and character encoding and decoding utility. * It's compatible with {@link org.apache.commons.httpclient.HttpURL} rather * than {@link org.apache.commons.httpclient.URI}. * * @author <a href="mailto:jericho@apache.org">Sung-Gu</a> * @version $Revision: 507321 $ $Date: 2002/03/14 15:14:01  */public class URIUtil {    // ----------------------------------------------------- Instance variables    protected static final BitSet empty = new BitSet(1);    // ---------------------------------------------------------- URI utilities    /**     * Get the basename of an URI.   It's possibly an empty string.     *     * @param uri a string regarded an URI     * @return the basename string; an empty string if the path ends with slash     */    public static String getName(String uri) {        if (uri == null || uri.length() == 0) { return uri; }         String path = URIUtil.getPath(uri);        int at = path.lastIndexOf("/");        int to = path.length();        return (at >= 0) ? path.substring(at + 1, to) : path;    }    /**     * Get the query of an URI.     *     * @param uri a string regarded an URI     * @return the query string; <code>null</code> if empty or undefined     */    public static String getQuery(String uri) {        if (uri == null || uri.length() == 0) { return null; }         // consider of net_path        int at = uri.indexOf("//");        int from = uri.indexOf(            "/",             at >= 0 ? (uri.lastIndexOf("/", at - 1) >= 0 ? 0 : at + 2) : 0        );        // the authority part of URI ignored        int to = uri.length();        // reuse the at and from variables to consider the query        at = uri.indexOf("?", from);        if (at >= 0) {            from = at + 1;        } else {            return null;        }        // check the fragment        if (uri.lastIndexOf("#") > from) {            to = uri.lastIndexOf("#");        }        // get the path and query.        return (from < 0 || from == to) ? null : uri.substring(from, to);    }    /**     * Get the path of an URI.     *     * @param uri a string regarded an URI     * @return the path string     */    public static String getPath(String uri) {        if (uri == null) {            return null;        }         // consider of net_path        int at = uri.indexOf("//");        int from = uri.indexOf(            "/",             at >= 0 ? (uri.lastIndexOf("/", at - 1) >= 0 ? 0 : at + 2) : 0        );        // the authority part of URI ignored         int to = uri.length();        // check the query        if (uri.indexOf('?', from) != -1) {            to = uri.indexOf('?', from);        }        // check the fragment        if (uri.lastIndexOf("#") > from && uri.lastIndexOf("#") < to) {            to = uri.lastIndexOf("#");        }        // get only the path.        return (from < 0) ? (at >= 0 ? "/" : uri) : uri.substring(from, to);    }    /**     * Get the path and query of an URI.     *     * @param uri a string regarded an URI     * @return the path and query string     */    public static String getPathQuery(String uri) {        if (uri == null) {            return null;        }         // consider of net_path        int at = uri.indexOf("//");        int from = uri.indexOf(            "/",             at >= 0 ? (uri.lastIndexOf("/", at - 1) >= 0 ? 0 : at + 2) : 0        );        // the authority part of URI ignored        int to = uri.length();        // Ignore the '?' mark so to ignore the query.        // check the fragment        if (uri.lastIndexOf("#") > from) {            to = uri.lastIndexOf("#");        }        // get the path and query.        return (from < 0) ? (at >= 0 ? "/" : uri) : uri.substring(from, to);    }    /**     * Get the path of an URI and its rest part.     *     * @param uri a string regarded an URI     * @return the string from the path part     */    public static String getFromPath(String uri) {        if (uri == null) {            return null;        }         // consider of net_path        int at = uri.indexOf("//");        int from = uri.indexOf(            "/",             at >= 0 ? (uri.lastIndexOf("/", at - 1) >= 0 ? 0 : at + 2) : 0        );        // get the path and its rest.        return (from < 0) ? (at >= 0 ? "/" : uri) : uri.substring(from);    }    // ----------------------------------------------------- Encoding utilities    /**     * Get the all escaped and encoded string with the default protocl charset.     * It's the same function to use <code>encode(String unescaped, Bitset     * empty, URI.getDefaultProtocolCharset())</code>.     *     * @param unescaped an unescaped string     * @return the escaped string     *      * @throws URIException if the default protocol charset is not supported     *     * @see URI#getDefaultProtocolCharset     * @see #encode     */    public static String encodeAll(String unescaped) throws URIException {        return encodeAll(unescaped, URI.getDefaultProtocolCharset());    }     /**     * Get the all escaped and encoded string with a given charset.     * It's the same function to use <code>encode(String unescaped, Bitset     * empty, String charset)</code>.     *     * @param unescaped an unescaped string     * @param charset the charset     * @return the escaped string     *      * @throws URIException if the charset is not supported     *      * @see #encode     */    public static String encodeAll(String unescaped, String charset)        throws URIException {        return encode(unescaped, empty, charset);    }      /**     * Escape and encode a string regarded as within the authority component of     * an URI with the default protocol charset.     * Within the authority component, the characters ";", ":", "@", "?", and     * "/" are reserved.     *     * @param unescaped an unescaped string     * @return the escaped string     *      * @throws URIException if the default protocol charset is not supported     *      * @see URI#getDefaultProtocolCharset     * @see #encode     */    public static String encodeWithinAuthority(String unescaped)        throws URIException {        return encodeWithinAuthority(unescaped, URI.getDefaultProtocolCharset());    }    /**     * Escape and encode a string regarded as within the authority component of     * an URI with a given charset.     * Within the authority component, the characters ";", ":", "@", "?", and     * "/" are reserved.     *     * @param unescaped an unescaped string     * @param charset the charset     * @return the escaped string     *      * @throws URIException if the charset is not supported     *      * @see #encode     */    public static String encodeWithinAuthority(String unescaped, String charset)        throws URIException {        return encode(unescaped, URI.allowed_within_authority, charset);    }    /**     * Escape and encode a string regarded as the path and query components of     * an URI with the default protocol charset.     *     * @param unescaped an unescaped string     * @return the escaped string     *      * @throws URIException if the default protocol charset is not supported     *      * @see URI#getDefaultProtocolCharset     * @see #encode     */    public static String encodePathQuery(String unescaped) throws URIException {        return encodePathQuery(unescaped, URI.getDefaultProtocolCharset());    }    /**     * Escape and encode a string regarded as the path and query components of     * an URI with a given charset.     *     * @param unescaped an unescaped string     * @param charset the charset     * @return the escaped string     *      * @throws URIException if the charset is not supported     *      * @see #encode     */    public static String encodePathQuery(String unescaped, String charset)        throws URIException {        int at = unescaped.indexOf('?');        if (at < 0) {            return encode(unescaped, URI.allowed_abs_path, charset);        }        // else        return  encode(unescaped.substring(0, at), URI.allowed_abs_path, charset)            + '?' + encode(unescaped.substring(at + 1), URI.allowed_query, charset);    }    /**     * Escape and encode a string regarded as within the path component of an     * URI with the default protocol charset.     * The path may consist of a sequence of path segments separated by a     * single slash "/" character.  Within a path segment, the characters     * "/", ";", "=", and "?" are reserved.     *     * @param unescaped an unescaped string     * @return the escaped string     *      * @throws URIException if the default protocol charset is not supported     *      * @see URI#getDefaultProtocolCharset     * @see #encode     */    public static String encodeWithinPath(String unescaped)        throws URIException {        return encodeWithinPath(unescaped, URI.getDefaultProtocolCharset());    }    /**     * Escape and encode a string regarded as within the path component of an     * URI with a given charset.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -