URLUtil.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:7k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.util;
  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.ArrayList;
  5. import java.util.StringTokenizer;
  6. /**
  7.  * Some URL related methods gathered as static methods in this utility class.
  8.  *
  9.  * $Id: URLUtil.java,v 1.13 2003/04/29 17:53:49 vanrogu Exp $
  10.  *
  11.  * @author G黱ther Van Roey
  12.  */
  13. public class URLUtil {
  14.     /**
  15.      * Normalizes the given url by replacing '/./' by '/' and removes trailing slashes
  16.      * @param original the original URL to be normalized
  17.      * @return the normalized url
  18.      */
  19.     public static URL normalize(URL original) {
  20.         URL normalized = null;
  21.         if (original != null) {
  22.             String urlString = original.toString ( );
  23.             urlString = normalizeDotFolders(urlString);
  24.             urlString = normalizeBackSlashes(urlString);
  25.             urlString = normalizeDoubleSlashes(urlString);
  26.             urlString = normalizeStripQuery(urlString) ;
  27.             //urlString = normalizeStripTrailingSlash(urlString) ;
  28.             try {
  29.                 normalized = new URL(urlString);
  30.             } catch (MalformedURLException e) {
  31.             }
  32.         }
  33.         return normalized;
  34.     }
  35.     /**
  36.      * Replaces all backslashes by front slashes in the given url string
  37.      * @param original the original url string
  38.      * @return the url string with the normalization applied
  39.      */
  40.     protected static String normalizeBackSlashes ( String original ) {
  41.         return StringUtil.replace(original, "\", "/");
  42.     }
  43.     /**
  44.      * Replaces all double slashes by single slashes in the given url string
  45.      * @param original the original url string
  46.      * @return the url string with the normalization applied
  47.      */
  48.     protected static String normalizeDoubleSlashes ( String original ) {
  49.         return StringUtil.replace(original, "//", "/", 7);
  50.     }
  51.     /**
  52.      * Removes all dot folders ( abc/./def/index.html, ...) from the given
  53.      * url string
  54.      * @param original the original url string
  55.      * @return the url string with the normalization applied
  56.      */
  57.     protected static String normalizeDotFolders ( String original ) {
  58.         return StringUtil.replace(original, "/./", "/");
  59.     }
  60.     /**
  61.      * Strips an eventual query string from the resource (index.html?id=1
  62.      * becomes index.html for instance).
  63.      * @param original the original url string
  64.      * @return the url string with the normalization applied
  65.      */
  66.     protected static String normalizeStripQuery ( String original ) {
  67.         int index = original.indexOf('?');
  68.         if (index >= 0) {
  69.             return original.substring(0, index);
  70.         } else {
  71.             return original;
  72.         }
  73.     }
  74.     /**
  75.      * Removes an evantual trailing slash from the given url string
  76.      * @param original the original url string
  77.      * @return the url string with the normalization applied
  78.      */
  79.     protected static String normalizeStripTrailingSlash ( String original ) {
  80.         if (original.endsWith("/")) {
  81.             return original.substring(0, original.length() - 1);
  82.         } else {
  83.             return original;
  84.         }
  85.     }
  86.     /**
  87.      * Converts any resource URL to the site's url.
  88.      * @param resourceURL the url of the resource to find the url of the site for
  89.      * @return the URL pointing to the site in which the resource is located
  90.      */
  91.     public static URL getSiteURL(URL resourceURL) {
  92.         URL siteURL = null;
  93.         if (resourceURL != null) {
  94.             try {
  95.                 siteURL = new URL(resourceURL.getProtocol(), resourceURL.getHost(), resourceURL.getPort(), "");
  96.             } catch (MalformedURLException e) {
  97.                 // shouldn't happen, we're only dropping the PATH part of a valid URL ...
  98.             }
  99.         }
  100.         return siteURL;
  101.     }
  102.     /**
  103.      * Reuturns the URL of the robots.txt resource in the site of the given resource.
  104.      * @param resourceURL the URL of the resource to find the site's robots.txt of
  105.      * @return URL pointing to the robots.txt resource of the site in which resourceURL is
  106.      */
  107.     public static URL getRobotsTXTURL(URL resourceURL) {
  108.         URL retVal = null;
  109.         if (resourceURL != null) {
  110.             try {
  111.                 retVal = new URL(getSiteURL(resourceURL), "/robots.txt");
  112.             } catch (MalformedURLException e) {
  113.             }
  114.         }
  115.         return retVal;
  116.     }
  117.     /**
  118.      * returns the resource path without the resource.
  119.      * @param path the path to the resource
  120.      * @return path without the resource itself
  121.      */
  122.     public static String stripResource(String path) {
  123.         String result = null;
  124.         if (path != null) {
  125.             int pos = path.lastIndexOf("/");
  126.             result = path.substring(0, pos + 1);
  127.         }
  128.         return result;
  129.     }
  130.     /**
  131.      * Returns the 'depth' of the resource pointed to by the URL
  132.      * @param url the URL to the resource to calculate the depth of
  133.      * @return the depth of this resource in the site
  134.      */
  135.     public static int getDepth(URL url) {
  136.         int depth = 0;
  137.         if (url != null) {
  138.             String path = url.getPath();
  139.             if (!isFileSpecified(url) && !path.endsWith("/")) {
  140.                 path = path + "/";
  141.             }
  142.             int pos = path.indexOf('/');
  143.             while (pos != -1) {
  144.                 if (pos > 0) {
  145.                     depth++;
  146.                 }
  147.                 pos = path.indexOf('/', pos + 1);
  148.             }
  149.         }
  150.         return depth;
  151.     }
  152.     /**
  153.      * Determines whether a file is specified in the path part of the url.
  154.      * This is assumed to be the case if the string after the last slash
  155.      * contains a dot (aaaaa/bbbb/cccc.dddd).
  156.      * @param url the url to test
  157.      * @return boolean value indicating whether a file is specified
  158.      */
  159.     public static boolean isFileSpecified(URL url) {
  160.         boolean specified = false;
  161.         String path = url.getPath();
  162.         int posLastSlash = path.lastIndexOf('/');
  163.         int posLastDot = path.lastIndexOf('.');
  164.         specified = posLastDot > posLastSlash;
  165.         return specified;
  166.     }
  167.     /**
  168.      * Returns an array of Strings being the folder names of the folders
  169.      * found in the given URL.
  170.      * @param url the url to parse the folders of
  171.      * @return an array of Strings containing all folder names
  172.      */
  173.     public static String[] getFolderNames(URL url) {
  174.         url = normalize(url);
  175.         ArrayList al = new ArrayList();
  176.         String path = url.getPath();
  177.         if (isFileSpecified(url)) {
  178.             path = stripResource(path);
  179.         }
  180.         StringTokenizer st = new StringTokenizer(path, "/");
  181.         while (st.hasMoreTokens()) {
  182.             al.add(st.nextToken());
  183.         }
  184.         return (String[]) al.toArray(new String[al.size()]);
  185.     }
  186.     /**
  187.      * Returns the file name (without the path) of the resource specified
  188.      * by the given url.
  189.      * @param url the url to get the filename out of
  190.      * @return String containing the name of the file, zero-length if none
  191.      */
  192.     public static String getFileName(URL url) {
  193.         return url.getPath().substring(stripResource(url.getPath()).length());
  194.     }
  195. }