/*
 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;
 
 public class LinkProcess
 {
 	/* Perform parsing and various checks to get hyperlinks from a string */
 	
 	private String link;
 	private int length;
 	private boolean scheme, channel, ipv6;
 	
 	private static boolean allowHTTPS = true;
 	
 	public LinkProcess(String str)
 	{
 		if (str == null || str.length() == 0)
 			throw new IllegalArgumentException();
 		else
 			link = str;
 	}
 	
 	public boolean isLink()
 	{
 		if (link.charAt(0) == '#' || link.charAt(0) == '&')
 		{
 			if (link.length() == 1 || " ,:".indexOf(link.charAt(1)) >= 0)
 				return(false);
 			
 			channel = true;
 			return(true);
 		}
 
 		if (link.length() < 8)
 			return(false); // 7 chars at least (www.x.yy ftp://x)
 			
 		if (link.toLowerCase().startsWith("https://") && LinkProcess.allowHTTPS && link.length() > 8)
 			return(true);
 		
 		String start[] = {"www.", "ftp.", "ftp://", "http://"};
 							
 		for (int j = 0; j < start.length; j++)
 		{
 			if (link.toLowerCase().startsWith(start[j]))
 			{
 				scheme = (start[j].indexOf("://") > 1 ? true : false);
 				
 				if (link.length() > start[j].length())
 					return(true);
 			}
 
 			/* IPv6 URIs */
 /*			if (link.toLowerCase().startsWith("[" + start[j]))
 			{
 				if (link.length() > start[j].length() + 2)
 					if (link.indexOf("]", 1) > start[j].length() + 2)
 					{
 						ipv6 = true;
 						return(true);
 					}
 			}
 */		}
 		
 //		if (s.indexOf("@") > 0)
 //			return(true);
 
 		return(false);
 	}
 	
 	public boolean isChannel()
 	{
 		return(channel);
 	}
 	
 	public boolean hasScheme()
 	{
 		return(scheme);
 	}
 	
 	public int getLength()
 	{
 		int i = 1;
 		int len = link.length();
 
 		if (link.charAt(0) == '#' || link.charAt(0) == '&')
 		{
 			/*
 			** If hyperlink is a channel name,
 			** parse until a reserved character is met.
 			*/
 			boolean ok = true;
 			while(i < len && ok)
 			{
 				ok = (link.charAt(i) > ' ') && (link.charAt(i) != ',') && (link.charAt(i) != ':');
 				i++;
 			}
 
 			if (ok)
 				length = i;
 			else
 				length = i - 1;
 		}
 		else
 		{
 			/*
 			** If hyperlink is a FTP, HTTP, or WWW link,
 			** first, check the end of hostname,
 			** second, check the end of path.
 			*/
 			if (scheme)
 				i = link.indexOf("://") + 4;
 			
 			// Skip these to allow UTF-8 DNS
 			
 			String host = "@:ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-.";
 			while(i < len && host.indexOf(Character.toUpperCase(link.charAt(i))) >= 0)
 				i++;
 
 			if (i < len && link.charAt(i) != '/')
 			{
 				length = i;
 				return(i);
 			}
 			
 			boolean path = true;
 			while(i < len && path)
 				path = link.charAt(i++) > ' ';
 				
 			if (path)
 				length = i;
 			else
 				length = i - 1;
 		}
 		
 		return(length);
 	}
 	
 	public String getLink()
 	{
 		if (length > 0)
 			link = link.substring(0, length);
 		
 		if(channel)
 			return(link);
 
 		int i = 0;
 		// add protocol http:// to URL saved in run if missing so far
 		if (!scheme)
 		{
 			if ((i = link.toLowerCase().indexOf("www.")) < 2)
 				link = "http://" + link;
 			else if ((i = link.toLowerCase().indexOf("ftp.")) < 2)
 				link = "ftp://" + link;
 		}
 
 /*
 		i = link.indexOf("://") + 3;
 		if (i < link.length())
 		{
 			boolean inhostname = true;
 			StringBuffer url = new StringBuffer(link.substring(0, i));
 			char chars[] = link.substring(i).toCharArray();
 			for (int j = 0; j < chars.length; j++)
 			{
 				char c = chars[j];
 				
 				if (c == '/')
 					inhostname = false;
 				
 				if (c == ' ')
 					url.append('+');
 				else if (notEncodable(c) || (ipv6 && inhostname && (c == '[' || c == ']')))
 					url.append(c);
 				else
 				{
 					try
 					{
 						byte b[] = link.substring(i+j, i+j+1).getBytes("UTF8");
 						for(int k = 0; k < b.length; k++)
 							url.append("%" + Integer.toHexString((int)(b[k] & 0xFF)));
 					}
 					catch (java.io.UnsupportedEncodingException ex) {}
 				}
 			}
 			link = url.toString();
 		}
 */
 	
 		return(link);
 	}
 
 	private boolean notEncodable(char c)
 	{
 		if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
 			return(true);
 			
 		String s = "%;/?:@&amp;=+$,-_.!~*'()";
 		for (int i = 0; i < s.length(); i++)
 			if (s.charAt(i) == c)
 				return(true);
 		
 		return(false);
 	}
 	
 	public static void checkHTTPS()
 	{
 		try
 		{
 			URL u = new URL("https://www.example.com");
 		}
 		catch (MalformedURLException e)
 		{
 			System.out.println("No HTTPS URLs allowed.");
 			LinkProcess.allowHTTPS = false;
 		}
 	}
 }