home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / net / URLCanonicalizer.java < prev   
Encoding:
Java Source  |  1997-01-27  |  3.0 KB  |  97 lines

  1. /*
  2.  * @(#)URLCanonicalizer.java    1.2 96/03/21  
  3.  *
  4.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package sun.net;
  21.  
  22. /**
  23.  * Helper class to map URL "abbreviations" to real URLs.
  24.  * The default implementation supports the following mappings:
  25.  *   ftp.mumble.bar/... => ftp://ftp.mumble.bar/...
  26.  *   gopher.mumble.bar/... => gopher://gopher.mumble.bar/...
  27.  *   other.name.dom/... => http://other.name.dom/...
  28.  *   /foo/... => file:/foo/...
  29.  * 
  30.  * Full URLs (those including a protocol name) are passed through unchanged.
  31.  *
  32.  * Subclassers can override or extend this behavior to support different
  33.  * or additional canonicalization policies.
  34.  *
  35.  * @version     1.2, 03/21/96
  36.  * @author    Steve Byrne
  37.  */
  38.  
  39. public class URLCanonicalizer {
  40.     /**
  41.      * Creates the default canonicalizer instance.
  42.      */
  43.     public URLCanonicalizer() { }
  44.  
  45.     /**
  46.      * Given a possibly abbreviated URL (missing a protocol name, typically),
  47.      * this method's job is to transform that URL into a canonical form,
  48.      * by including a protocol name and additional syntax, if necessary.
  49.      * 
  50.      * For a correctly formed URL, this method should just return its argument.
  51.      */
  52.     public String canonicalize(String simpleURL) {
  53.     String resultURL = simpleURL;
  54.     if (simpleURL.startsWith("ftp.")) {
  55.         resultURL = "ftp://" + simpleURL;
  56.     } else if (simpleURL.startsWith("gopher.")) {
  57.         resultURL = "gopher://" + simpleURL;
  58.     } else if (simpleURL.startsWith("/")) {
  59.         resultURL = "file:" + simpleURL;
  60.     } else if (!hasProtocolName(simpleURL)) {
  61.         resultURL = "http://" + simpleURL;
  62.     }
  63.  
  64.     return resultURL;
  65.     }
  66.  
  67.     /**
  68.      * Given a possibly abbreviated URL, this predicate function returns
  69.      * true if it appears that the URL contains a protocol name
  70.      */
  71.     public boolean hasProtocolName(String url) {
  72.     int index = url.indexOf(':');
  73.     if (index <= 0) {    // treat ":foo" as not having a protocol spec
  74.         return false;
  75.     }
  76.  
  77.     for (int i = 0; i < index; i++) {
  78.         char c = url.charAt(i);
  79.         
  80.         // REMIND: this is a guess at legal characters in a protocol --
  81.         // need to be verified
  82.         if ((c >= 'A' && c <= 'Z')
  83.         || (c >= 'a' && c <= 'z')
  84.         || (c == '-')) {
  85.         continue;
  86.         }
  87.         
  88.         // found an illegal character
  89.         return false;
  90.     }
  91.  
  92.     return true;
  93.     }
  94. }
  95.  
  96.  
  97.