/*
 * @(#)URLStreamHandler.java	1.19 97/02/10
 * 
 * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Sun
 * Microsystems, Inc. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Sun.
 * 
 * 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.
 * 
 * CopyrightVersion 1.1_beta
 * 
 */

package java.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.Hashtable;

/**
 * The abstract class <code>URLStreamHandler</code> is the common 
 * superclass for all stream protocol handlers. A stream protocol 
 * handler knows how to make a connection for a particular protocol 
 * type, such as <code>http</code>, <code>ftp</code>, or 
 * <code>gopher</code>. 
 * <p>
 * In most cases, an instance of a <code>URLStreamHandler</code> 
 * subclass is not created directly by an application. Rather, the 
 * first time a protocol name is encountered when constructing a 
 * <code>URL</code>, the appropriate stream protocol handler is 
 * automatically loaded.
 *
 * @author  James Gosling
 * @version 1.19, 10 Feb 1997
 * @see     java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String)
 * @since   JDK1.0
 */
public abstract class URLStreamHandler {
    /**
     * Opens a connection to the object referenced by the 
     * <code>URL</code> argument. 
     * This method should be overridden by a subclass.
     *
     * @param      u   the URL that this connects to.
     * @return     a <code>URLConnection</code> object for the <code>URL</code>.
     * @exception  IOException  if an I/O error occurs while opening the
     *               connection.
     * @since      JDK1.0
     */
    abstract protected URLConnection openConnection(URL u) throws IOException;

    /** 
     * Parses the string representation of a <code>URL</code> into a 
     * <code>URL</code> object. 
     * <p>
     * If there is any inherited context, then it has already been 
     * copied into the <code>URL</code> argument. 
     * <p>
     * The <code>parseURL</code> method of <code>URLStreamHandler</code> 
     * parses the string representation as if it were an 
     * <code>http</code> specification. Most URL protocol families have a 
     * similar parsing. A stream protocol handler for a protocol that has 
     * a different syntax must override this routine. 
     *
     * @param   u       the <code>URL</code> to receive the result of parsing
     *                  the spec.
     * @param   spec    the <code>String</code> representing the URL that
     *                  must be parsed.
     * @param   start   the character index at which to begin parsing. This is
     *                  just past the '<code>:</code>' (if there is one) that
     *                  specifies the determination of the protocol name.
     * @param   limit   the character position to stop parsing at. This is the
     *                  end of the string or the position of the
     *                  "<code>#</code>" character, if present. All information
     *                  after the sharp sign indicates an anchor. 
     * @since   JDK1.0
     */
    protected void parseURL(URL u, String spec, int start, int limit) {
	String protocol = u.getProtocol();
	String host = u.getHost();
	int port = u.getPort();
	String file = u.getFile();
	String ref = u.getRef();

	int i;
	if ((start <= limit - 2) && (spec.charAt(start) == '/') &&
	    (spec.charAt(start + 1) == '/')) {
	    start += 2;
	    i = spec.indexOf('/', start);
	    if (i < 0) {
		i = limit;
	    }
	    int prn = spec.indexOf(':', start);
	    port = -1;
	    if ((prn < i) && (prn >= 0)) {
		try {
		    port = Integer.parseInt(spec.substring(prn + 1, i));
		} catch(Exception e) {
		    // ignore bogus port numbers
		}
		if (prn > start) {
		    host = spec.substring(start, prn);
		}
	    } else {
		host = spec.substring(start, i);
	    }
	    start = i;
	    file = null;
	} else if (host == null) {
	    host = "";
	}
	if (start < limit) {
	    if (spec.charAt(start) == '/') {
		file = spec.substring(start, limit);
	    } else if (file != null && file.length() > 0) {
		/* relative to the context file - use either 
		 * Unix separators || platform separators
		 */
		int ind = Math.max(file.lastIndexOf('/'), 
				   file.lastIndexOf(File.separatorChar));

		file = file.substring(0, ind) + "/" + spec.substring(start, limit);
	    } else {
		file = "/" + spec.substring(start, limit);
	    }
	}
	if ((file == null) || (file.length() == 0)) {
	    file = "/"; 
	}
	while ((i = file.indexOf("/./")) >= 0) {
	    file = file.substring(0, i) + file.substring(i + 2);
	}
	while ((i = file.indexOf("/../")) >= 0) {
	    if ((limit = file.lastIndexOf('/', i - 1)) >= 0) {
		file = file.substring(0, limit) + file.substring(i + 3);
	    } else {
		file = file.substring(i + 3);
	    }
	}

	u.set(protocol, host, port, file, ref);
    }

    /**
     * Converts a <code>URL</code> of a specific protocol to a 
     * <code>String</code>. 
     *
     * @param   u   the URL.
     * @return  a string representation of the <code>URL</code> argument.
     * @since   JDK1.0
     */
    protected String toExternalForm(URL u) {
	String result = u.getProtocol() + ":";
	if ((u.getHost() != null) && (u.getHost().length() > 0)) {
	    result = result + "//" + u.getHost();
	    if (u.getPort() != -1) {
		result += ":" + u.getPort();
	    }
	}
	result += u.getFile();
	if (u.getRef() != null) {
	    result += "#" + u.getRef();
	}
	return result;
    }

    /**
     * Sets the fields of the <code>URL</code> argument to the indicated values.
     * Only classes derived from URLStreamHandler are supposed to be able
     * to call the set method on a URL.
     *
     * @param   u         the URL to modify.
     * @param   protocol  the protocol name.
     * @param   host      the remote host value for the URL.
     * @param   port      the port on the remote machine.
     * @param   file      the file.
     * @param   ref       the reference.
     * @see     java.net.URL#set(java.lang.String, java.lang.String, int, java.lang.String, java.lang.String)
     * @since   JDK1.0
     */
    protected void setURL(URL u, String protocol, String host, int port,
			  String file, String ref) {
        u.set(protocol, host, port, file, ref);
    }
}
