home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-01-27 | 3.0 KB | 97 lines |
- /*
- * @(#)URLCanonicalizer.java 1.2 96/03/21
- *
- * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
- *
- * Permission to use, copy, modify, and distribute this software
- * and its documentation for NON-COMMERCIAL purposes and without
- * fee is hereby granted provided that this copyright notice
- * appears in all copies. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
- * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
- * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
- * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
- package sun.net;
-
- /**
- * Helper class to map URL "abbreviations" to real URLs.
- * The default implementation supports the following mappings:
- * ftp.mumble.bar/... => ftp://ftp.mumble.bar/...
- * gopher.mumble.bar/... => gopher://gopher.mumble.bar/...
- * other.name.dom/... => http://other.name.dom/...
- * /foo/... => file:/foo/...
- *
- * Full URLs (those including a protocol name) are passed through unchanged.
- *
- * Subclassers can override or extend this behavior to support different
- * or additional canonicalization policies.
- *
- * @version 1.2, 03/21/96
- * @author Steve Byrne
- */
-
- public class URLCanonicalizer {
- /**
- * Creates the default canonicalizer instance.
- */
- public URLCanonicalizer() { }
-
- /**
- * Given a possibly abbreviated URL (missing a protocol name, typically),
- * this method's job is to transform that URL into a canonical form,
- * by including a protocol name and additional syntax, if necessary.
- *
- * For a correctly formed URL, this method should just return its argument.
- */
- public String canonicalize(String simpleURL) {
- String resultURL = simpleURL;
- if (simpleURL.startsWith("ftp.")) {
- resultURL = "ftp://" + simpleURL;
- } else if (simpleURL.startsWith("gopher.")) {
- resultURL = "gopher://" + simpleURL;
- } else if (simpleURL.startsWith("/")) {
- resultURL = "file:" + simpleURL;
- } else if (!hasProtocolName(simpleURL)) {
- resultURL = "http://" + simpleURL;
- }
-
- return resultURL;
- }
-
- /**
- * Given a possibly abbreviated URL, this predicate function returns
- * true if it appears that the URL contains a protocol name
- */
- public boolean hasProtocolName(String url) {
- int index = url.indexOf(':');
- if (index <= 0) { // treat ":foo" as not having a protocol spec
- return false;
- }
-
- for (int i = 0; i < index; i++) {
- char c = url.charAt(i);
-
- // REMIND: this is a guess at legal characters in a protocol --
- // need to be verified
- if ((c >= 'A' && c <= 'Z')
- || (c >= 'a' && c <= 'z')
- || (c == '-')) {
- continue;
- }
-
- // found an illegal character
- return false;
- }
-
- return true;
- }
- }
-
-
-