/*
 Javier Kohen's Java Framework Classes.
 Copyright (C) 2000-2001  Javier Kohen <jkohen at tough.com>
 
 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.
 
 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.
 
 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
 package ar.com.jkohen.util;
 
 import java.net.URL;
 import java.net.MalformedURLException;
 import java.util.Enumeration;
 import java.util.Vector;
 
 
 public class LinkProcess
 {
 	/* Perform parsing and various checks to get hyperlinks from a string */
 	
 	private String link;
 	private int length;
 	private boolean scheme, channel;
 	
 	private static Vector start;
 	
 	public LinkProcess(String str)
 	{
 		if (str == null || str.length() == 0)
 			throw new IllegalArgumentException();
 		else
 			link = str;
 	}
 	
 	public static void init()
 	{
 		start = new Vector(5);
 		start.addElement("www.");
 		start.addElement("ftp.");
 		start.addElement("ftp://");
 		start.addElement("http://");
 		start.addElement("mailto:");
 		
 		try
 		{
 			URL u = new URL("https://www.example.com");
 			start.addElement("https://");
 		}
 		catch (MalformedURLException e)
 		{
 			System.out.println("No HTTPS URLs supported.");
 		}
 	}
 	
 	public boolean isLink()
 	{
 		int end = link.indexOf(' ');
 		if (end < 0)
 			end = link.length();
 								
 		if (link.charAt(0) == '#' || link.charAt(0) == '&')
 		{
 			if (link.length() == 1 || " ,:".indexOf(link.charAt(1)) >= 0)
 				return(false);
 			
 			channel = true;
 			link = link.substring(0, end);
 			return(true);
 		}
 
 		if (link.length() < 8)
 			return(false); // 7 chars at least (www.x.yy ftp://x)
 			
 		for (Enumeration e = start.elements(); e.hasMoreElements(); )
 		{
 			String s = (String)e.nextElement();
 			
 			if (link.toLowerCase().startsWith(s))
 			{
 				scheme = (s.indexOf("://") > 1 ? true : false);
 				
 				if (link.length() > s.length())
 				{
 					if (scheme)
 					{
 						try
 						{
 							/* java.net.URL is buggy, but java.net.URI isn't bugfree either */
 							String l = link.substring(0, end);
 							URL u = new URL(l);
 							link = l;
 						}
 						catch (MalformedURLException ex)
 						{
 							return(false);
 						}
 					}
 					
 					link = link.substring(0, end);
 					return(true);
 				}
 			}
 		}
 		
 //		if (s.indexOf("@") > 0)
 //			return(true);
 
 		return(false);
 	}
 	
 	public String getLink()
 	{
 		String l = link;
 		if (!channel && !scheme)
 		{
 			if ((l.toLowerCase().indexOf("www.")) >= 0)
 				l = "http://" + l;
 			else if ((l.toLowerCase().indexOf("ftp.")) >= 0)
 				l = "ftp://" + l;
 		}
 
 		return(l);
 	}
 
 	public int getLength()
 	{
 		return(link.length());
 	}
 	
 	public boolean isChannel()
 	{
 		return(channel);
 	}
 	
 	public boolean hasScheme()
 	{
 		return(scheme);
 	}
 }