/**
 *#########################################################################
 *
 * A component of the Gatherer application, part of the Greenstone digital
 * library suite from the New Zealand Digital Library Project at the
 * University of Waikato, New Zealand.
 *
 * <BR><BR>
 *
 * Author: John Thompson, Greenstone Digital Library, University of Waikato
 *
 * <BR><BR>
 *
 * Copyright (C) 1999 New Zealand Digital Library Project
 *
 * <BR><BR>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * <BR><BR>
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * <BR><BR>
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *########################################################################
 */
package org.greenstone.gatherer.util;

import java.io.*;
import java.lang.*;
import java.net.*;
import java.util.*;

/**
 * Extends the standard URL class to include several new data elements and
 * the ability to take a string and parse a valid url.
 */
public class GURL
    implements Serializable {

    private Integer type;

    private URL url;

    private String file;
    private String host;
    private String path;
    private String protocol;
    private String title = "empty";

    /** This Vector maintains a record of the url links from within
     * this resource, and their type (EXTERNAL vs INTERNAL). Internal
     * links can be 'hidden' from the user if desired, or a user can
     * opt to have all internal links automatically moved into a
     * collection if the page that depends upon them is moved. <BR>
     * Each entry in the vector is of the form.
     * links[i] = String url
     * links[i+1] = int type
     */
    private Vector links;

    /* Static Integer identifiers for ease of use. */
    static final public Integer INTERNAL = new Integer(0);
    static final public Integer EXTERNAL = new Integer(1);

    public GURL(String url_str) {
	this(url_str, GURL.EXTERNAL);
    }

    public GURL(URL url) {
	this(url, GURL.EXTERNAL);
    }

    public GURL(URL url, Integer type) {
	this.type = type;
	this.url = url;
    }

    public GURL(String url_str, Integer type) {
	this.type = type;

	try {
	    if(!url_str.startsWith("file:") && url_str.indexOf("://") == -1) {
		url_str = "http://" + url_str;
	    }
	    url = new URL(url_str);
	}
	catch (MalformedURLException e) {
	    // Fix it
	}
    }

    public void addLink(String url) {
	addLink(url, GURL.EXTERNAL);
    }

    public void addLink(String url, Integer type) {
	if(links == null) {
	    links = new Vector();
	}
	links.add(url);
	links.add(type);
    }

    /** Used by tree renderer to display name. So we'll only return the
     * filename.
     */
    public String getFile() {
	return parseFile(url.getFile());
    }

    public String getHost() {
	return url.getHost();
    }

    public String getPath() {
	return getHost() + parsePath(url.getFile());
    }

    public int getPort() {
	return url.getPort();
    }

    public String getProtocol() {
	String protocol = url.getProtocol();
	if(protocol.equals("file")) {
	    return protocol + ":";
	}
	return protocol + "://";
    }

    public URL getURL() {
	return url;
    }

    public String toString() {
	return url.toString();
    }

    public boolean valid() {
	return (url != null);
    }

    private String parseFile(String raw) {
	if(raw != null && !raw.equals("")) {
	    // Remove everything upto and including the last '/'
	    if(raw.indexOf("/") != -1) {
		return raw.substring(raw.lastIndexOf("/") + 1);
	    }
	}
	return raw;
    }

    private String parsePath(String raw) {
	if(raw != null && !raw.equals("")) {
	    // Remove everything after the last "/"
	    if(raw.indexOf("/") != -1) {
		return raw.substring(0, raw.lastIndexOf("/"));
	    }
	}
	return raw;
    }
}
