/*
	This simple extension of the java.awt.Frame class
	contains all the elements necessary to act as the
	main window of an application.
 */

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

public class WebReader extends Frame
{
	public WebReader()
	{
		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.

		//{{INIT_CONTROLS
		setLayout(null);
		setVisible(false);
		setSize(405,305);
		textArea1 = new java.awt.TextArea();
		textArea1.setBounds(40,109,341,154);
		add(textArea1);
		buttonBegin = new java.awt.Button();
		buttonBegin.setLabel("Click Me To Begin");
		buttonBegin.setBounds(272,270,119,22);
		buttonBegin.setFont(new Font("SansSerif", Font.PLAIN, 12));
		add(buttonBegin);
		labelLook = new java.awt.Label("Looking At:");
		labelLook.setBounds(41,79,75,16);
		labelLook.setFont(new Font("SansSerif", Font.PLAIN, 12));
		add(labelLook);
		labelURL = new java.awt.Label("Enter URL:");
		labelURL.setBounds(47,60,64,16);
		labelURL.setFont(new Font("SansSerif", Font.PLAIN, 12));
		add(labelURL);
		textField2 = new java.awt.TextField();
		textField2.setBounds(120,80,245,15);
		add(textField2);
		textField1 = new java.awt.TextField();
		textField1.setText("http://");
		textField1.setBounds(120,60,244,16);
		add(textField1);
		labelTitle = new java.awt.Label("Web Reader");
		labelTitle.setBounds(145,6,115,29);
		labelTitle.setFont(new Font("SansSerif", Font.BOLD, 18));
		add(labelTitle);
		setTitle("Web Reader");
		setResizable(false);
		//}}

		//{{INIT_MENUS
		mainMenuBar = new java.awt.MenuBar();
		menu1 = new java.awt.Menu("File");
		miAbout = new java.awt.MenuItem("About..");
		menu1.add(miAbout);
		miExit = new java.awt.MenuItem("Exit");
		menu1.add(miExit);
		mainMenuBar.add(menu1);
		setMenuBar(mainMenuBar);
		//$$ mainMenuBar.move(10,8);
		//}}

		//{{REGISTER_LISTENERS
		SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		SymAction lSymAction = new SymAction();
		miAbout.addActionListener(lSymAction);
		miExit.addActionListener(lSymAction);
		buttonBegin.addActionListener(lSymAction);
		//}}
	}

	public WebReader(String title)
	{
		this();
		setTitle(title);
	}

    /**
     * Shows or hides the component depending on the boolean flag buttonBegin.
     * @param buttonBegin  if true, show the component; otherwise, hide the component.
     * @see java.awt.Component#isVisible
     */
    public void setVisible(boolean buttonBegin)
	{
		if(buttonBegin)
		{
			setLocation(50, 50);
		}
		super.setVisible(buttonBegin);
	}

	static public void main(String args[])
	{
		(new WebReader()).setVisible(true);
	}

	public void addNotify()
	{
		// Record the size of the window prior to calling parents addNotify.
		Dimension d = getSize();

		super.addNotify();

		if (fComponentsAdjusted)
			return;

		// Adjust components according to the insets
		setSize(insets().left + insets().right + d.width, insets().top + insets().bottom + d.height);
		Component components[] = getComponents();
		for (int i = 0; i < components.length; i++)
		{
			Point p = components[i].getLocation();
			p.translate(insets().left, insets().top);
			components[i].setLocation(p);
		}
		fComponentsAdjusted = true;
	}

	// Used for addNotify check.
	boolean fComponentsAdjusted = false;

	//{{DECLARE_CONTROLS
	java.awt.TextArea textArea1;
	java.awt.Button buttonBegin;
	java.awt.Label labelLook;
	java.awt.Label labelURL;
	java.awt.TextField textField2;
	java.awt.TextField textField1;
	java.awt.Label labelTitle;
	//}}

	//{{DECLARE_MENUS
	java.awt.MenuBar mainMenuBar;
	java.awt.Menu menu1;
	java.awt.MenuItem miAbout;
	java.awt.MenuItem miExit;
	//}}

	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == WebReader.this)
				WebReader_WindowClosing(event);
		}
	}

	void WebReader_WindowClosing(java.awt.event.WindowEvent event)
	{
		setVisible(false);	// hide the Frame
		dispose();			// free the system resources
		System.exit(0);		// close the application
	}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == miAbout)
				miAbout_Action(event);
			else if (object == miExit)
				miExit_Action(event);
			else if (object == buttonBegin)
				buttonBegin_ActionPerformed(event);

		}
	}

	void miAbout_Action(java.awt.event.ActionEvent event)
	{
		//{{CONNECTION
		// Action from About Create and show as modal
		(new AboutDialog(this, true)).setVisible(true);
		//}}
	}

	void miExit_Action(java.awt.event.ActionEvent event)
	{
		//{{CONNECTION
		// Action from Exit Create and show as modal
		(new QuitDialog(this, true)).setVisible(true);
		//}}
	}

	public String ReadPage(String WebPage)
	{
		String temp="";
		Character ch;
		int c,response;
		char x;
		long Age,Sent,Modified;
		try
		{
			URL webaddr=new URL(WebPage);
			HttpURLConnection http=(HttpURLConnection) webaddr.openConnection();
			response=http.getResponseCode();
			if ((response >=200) && (response <=299))
			{
			    	InputStream datain=http.getInputStream();
			  	while ((c=datain.read()) != -1)
			  	{
					x=(char) c;
					ch=new Character(x);
					temp=temp+ch;
			 	 }
			 	 Modified=http.getLastModified();
			 	 Sent=http.getDate();
			 	 Age=(Sent-Modified)/(60*60*24*7*365);
			 	 temp=temp+ "\n" + "Page modified " + Age + " day(s) ago.\n";
			}
			else if ((response >=300) && (response <=399))
			             {
			             	temp="Error: Redirection: " + http.getResponseMessage() + "\n";
			             	broken=broken+1;
			             }
				else if ((response >=400) && (response <=499))
					{
						temp="Error: Bad Request: " + http.getResponseMessage() + "\n";
						broken=broken+1;
					}
					else if ((response >=500) && (response <=599))
						{
							temp="Error: Server Error: " + http.getResponseMessage() + "\n";
							broken=broken+1;
						}
		} catch (MalformedURLException urlerr)
			{ System.out.println(urlerr); }
		  catch (IOException ioerr)
		    	{ System.out.println(ioerr); }
		return temp;
	}

	public void LoadLinks(String LinkPage)
	{
		int pagelength=LinkPage.length();
		int index=0;
		int lastindex=0;
		String TempLink=LinkPage.toLowerCase();
		while (index < pagelength)
		{
			index=TempLink.indexOf("<a href=",index);
			if (index != -1)
			{
				index=index+9;
				lastindex=TempLink.indexOf("\"",index);
				links.addElement(TempLink.substring(index,lastindex));
				linkcount=linkcount+1;
			}
			else index=pagelength;
		}
	}

	public int TableCount(String PTable)
	{
		int pagelength=PTable.length();
		int index=0;
		int Tcount=0;
		String TempTable=PTable.toLowerCase();
		while (index < pagelength)
		{
			index=TempTable.indexOf("<table",index);
			if (index !=-1)
			{
				index=index+6;
				Tcount=Tcount+1;
			}
			else index=pagelength;
		}
		return Tcount;
	}
	public int FrameCount(String PFrame)
	{
		int pagelength=PFrame.length();
		int index=0;
		int Fcount=0;
		String TempFrame=PFrame.toLowerCase();

		while (index < pagelength)
		{
			index=TempFrame.indexOf("<frameset",index);
			if (index !=-1)
			{
				index=index+8;
				Fcount=Fcount+1;
			}
			else index=pagelength;
		}
		return Fcount;
	}

	public int BGCount(String PBackground)
	{
		int pagelength=PBackground.length();
		int index=0;
		int BGcount=0;
		String TempBack=PBackground.toLowerCase();
		while (index < pagelength)
		{
			index=TempBack.indexOf("background",index);
			if (index !=-1)
			{
				index=index+10;
				BGcount=BGcount+1;
			}
			else index=pagelength;
		}
		return BGcount;
	}

	public int SSheetCount(String PStyle)
	{
		int pagelength=PStyle.length();
		int index=0;
		int Scount=0;
		String TempStyle=PStyle.toLowerCase();
		while (index < pagelength)
		{
			index=TempStyle.indexOf("<style",index);
			if (index !=-1)
			{
				index=index+6;
				Scount=Scount+1;
			}
			else index=pagelength;
		}
		return Scount;
	}

	public int AppletCount(String PApplet)
	{
		int pagelength=PApplet.length();
		int index=0;
		int Acount=0;
		String TempApplet=PApplet.toLowerCase();
		while (index < pagelength)
		{
			index=TempApplet.indexOf("<applet",index);
			if (index !=-1)
			{
				index=index+7;
				Acount=Acount+1;
			}
			else index=pagelength;
		}
		return Acount;
	}

	public int ObjectCount(String PObject)
	{
		int pagelength=PObject.length();
		int index=0;
		int Ocount=0;
		String TempObject=PObject.toLowerCase();
		while (index < pagelength)
		{
			index=TempObject.indexOf("<object",index);
			if (index !=-1)
			{
				index=index+6;
				Ocount=Ocount+1;
			}
			else index=pagelength;
		}
		return Ocount;
	}

		//{{DECLARE VARIABLES
		java.util.Vector  links=new Vector ();
		java.lang.String PageURL="";
		int broken=0;
		int linkcount=0;
		int ExternalLink=0;
		int InternalLink=0;
		//}}

	void buttonBegin_ActionPerformed(java.awt.event.ActionEvent event)
	{
			String PageURL,BaseURL;
			char ch;
			int URLIndex;
			BaseURL=textField1.getText();
			PageURL=textField1.getText();
			while (PageURL != "")
			{
				URLIndex=PageURL.indexOf(BaseURL);
				if (URLIndex == -1)
				{
					URLIndex=PageURL.indexOf("http://");
					if (URLIndex != -1)
						ExternalLink=ExternalLink+1;
					else
					{
						if ((PageURL.indexOf("mailto:") != -1) || (PageURL.indexOf("ftp:") != -1))
						 	textArea1.append("Not Http protocol: " + PageURL +"\n");
						else
						{
							ch=PageURL.charAt(0);
							Character chars=new Character(ch);
							if (ch == '/')
							{
								if (BaseURL.charAt(BaseURL.length()-1) == '/')
									PageURL=BaseURL + PageURL.substring(2,PageURL.length()-1);
								else PageURL=BaseURL +  "/"+PageURL.substring(1,PageURL.length()-1);
								InternalLink=InternalLink + 1;
								DoNumbers(PageURL);
							}
							else if (ch== '.')
							{
									if (BaseURL.charAt(BaseURL.length()-1)== '/')
										PageURL=BaseURL + PageURL.substring(PageURL.indexOf("/"),PageURL.length()-1);
									else PageURL=BaseURL +  "/"+PageURL.substring(PageURL.indexOf("/"),PageURL.length()-1);
									InternalLink=InternalLink + 1;
									DoNumbers(PageURL);
							}
								else if (chars.isLetter(ch))
								{		URLIndex=BaseURL.length() - 1;
										ch=BaseURL.charAt(URLIndex);
										if (ch== '/')
											PageURL=BaseURL + PageURL;
										else PageURL=BaseURL +  "/" + PageURL;
										InternalLink=InternalLink + 1;
										DoNumbers(PageURL);
								}
						}
					}
					PageURL=LinkSize();
				}
				else
				{
					DoNumbers(PageURL);
					PageURL=LinkSize();
				}
			}
			textArea1.append("The number of internal links is " + InternalLink + "\n");
			textArea1.append("The number of external links is " + ExternalLink +"\n");
			textArea1.append("The number of broken links is " + broken + "\n");
			textArea1.append("The total number of links is " + linkcount + "\n");
			InternalLink=0;
			ExternalLink=0;
			broken=0;
			linkcount=0;
	}

	public String LinkSize()
	{
		String urlpage;
		if (links.size() > 0)
		{
			urlpage=(String) links.firstElement();
			links.removeElementAt(0);
		}
		else urlpage="";
		return urlpage;
	}

	public void DoNumbers(String pagevalue)
	{
		String Page;
		int PageError;
		textArea1.append(pagevalue + "\n");
		textField2.setText(pagevalue);
		Page=ReadPage(pagevalue);
		PageError=Page.indexOf("Error");
		if  (PageError != -1)
		{
			textArea1.append(Page);
		}
		else
		{
			LoadLinks(Page);
			int Tables=TableCount(Page);
			int Frames=FrameCount(Page);
			int Background=BGCount(Page);
			int StyleSheet=SSheetCount(Page);
			int Applets=AppletCount(Page);
			int Objects=ObjectCount(Page);
			textArea1.append("The advanced features of the above link are:\n");
			textArea1.append("Tables: You have  "  + Tables  + " Table(s).\n");
			textArea1.append("Frames: You have  "  + Frames + " Frame(s).\n");
			textArea1.append("Background: You have  " + Background + " Background(s).\n");
			textArea1.append("Style Sheets:  You have " + StyleSheet + " StyleSheet(s).\n");
			textArea1.append("Applets: You have " + Applets + " Applet(s).\n");
			textArea1.append("Object tags: You have " + Objects + " Object(s).\n");
		}
	}
}

