/*
Copyright 2002 Catalin Stefan Samfirescu mdsa@lycos.co.uk

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
*/

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.applet.*;
import java.io.*;
import java.util.*;

public class MyDomainSearch extends Applet implements ActionListener, Runnable{

    URL codeBase, userURL, documentBase;
    BufferedReader index;
    String buf, key, ind, title, param, start, stop, labelText,curFile;
    Vector URLVector;
    Hashtable filetitle;
    char ch;
    java.awt.List list;
    TextField tf;
    Button b;
    Label lb;
    boolean needLine ,search, titlefound, keyfound, reg, comment, flag;
    GridBagLayout gridbag;
    GridBagConstraints cons;
    Thread runner;

    public void init()
    {
        codeBase=getCodeBase();

        flag=false;

        try{
            setBackground(new Color(Integer.parseInt(param()),Integer.parseInt(param()),Integer.parseInt(param())));
        }catch (NullPointerException e){setBackground(new Color(255,255,255));}
        catch(NumberFormatException e){setBackground(new Color(255,255,255));}
        catch(IllegalArgumentException e){setBackground(new Color(255,255,255));}

        gridbag=new GridBagLayout();
        cons=new GridBagConstraints();
        setLayout(gridbag);

        build (cons,0,0,1,1,90,10);
        cons.fill=GridBagConstraints.HORIZONTAL;

        tf=new TextField();
        gridbag.setConstraints(tf,cons);
        add(tf);

        build (cons,1,0,1,1,10,0);
        cons.fill=GridBagConstraints.NONE;
        cons.anchor=GridBagConstraints.CENTER;
        try{
            start=param();
        }catch (StringIndexOutOfBoundsException e) {start="Search";}
        catch (NullPointerException e) {start="Search";}
        b=new Button(start);
        gridbag.setConstraints(b,cons);
        add(b);
        try{
            stop=param();
        }catch (StringIndexOutOfBoundsException e) {stop="Stop";}
        catch (NullPointerException e) {stop="Stop";}

        build (cons,0,1,2,1,0,80);
        cons.fill=GridBagConstraints.BOTH;
        list=new java.awt.List();
        list.addActionListener(this);
        list.setCursor(new Cursor(Cursor.HAND_CURSOR));
        gridbag.setConstraints(list,cons);
        add(list);

        build (cons,0,2,2,1,0,10);
        cons.fill=GridBagConstraints.HORIZONTAL;
        try{
            labelText=param();
        }catch (StringIndexOutOfBoundsException e) {labelText="Enter keyword";}
        catch (NullPointerException e) {labelText="Enter keyword";}
        lb=new Label(labelText, Label.CENTER);
        gridbag.setConstraints(lb,cons);
        add(lb);

        needLine=false;
        search=false;
        comment=false;

        filetitle=new Hashtable(30,(float)0.5);

        try{
            ind=getParameter ("index");
        }catch(NullPointerException e) { ind="index.html"; }

        curFile=ind;
        
        URLVector=new Vector (30,15);
    }

    public void parseHLink (String str) throws StringIndexOutOfBoundsException
    {
        int m;
        /*it is true when quotes are present*/
        boolean flag=false;
        int i=0, j=0, k=0;
        /*We have a frameset page*/
        while ((k=str.toUpperCase().indexOf("")!=-1 && (str.indexOf(">")");
            }
            else if (str.indexOf(">")!=-1)
                i=str.indexOf(">");
        }
        for (;i')
            {
                tag=false;
                comment=false;
            }
            else if (tag==false && comment==false)
            {
                if (i>1 && str.charAt(i-1)=='>')
                {
                    buffer.append (' ');
                    buffer.append (ch);
                }
                else buffer.append (ch);
            }
        }

        if (buffer!=null)
            buf=buffer.toString().trim();

        if (buffer!=null && (i=buf.toUpperCase().indexOf(keyword.toUpperCase()))!=-1 && comment==false)
        {
            if (buf.length()==keyword.length())
            {
                keyfound=true;
                list.addItem(title);
            }
            else
            {
                if (i==0)
                {
                    if (Character.isLetter(buf.charAt(i+keyword.length()))==false)
                    {
                        keyfound=true;
                        list.addItem(title);
                    }
                }
                else
                {
                    if ((i+keyword.length())==buf.length())
                    {
                        if (Character.isLetter(buf.charAt(i-1))==false)
                        {
                            keyfound=true;
                            list.addItem(title);
                        }
                    }
                    else if (Character.isLetter(buf.charAt(i-1))==false && Character.isLetter(buf.charAt(i+keyword.length()))==false)
                    {
                        keyfound=true;
                        list.addItem(title);
                    }
                }
            }
        }
    }

    public void parseTitle (String str, String file)
    {
        int i=0, j=0;
        if (needLine==true)
        {
            if ((j=str.toUpperCase().indexOf(""))!=-1)
            {
                title=title.concat(" "+str.substring(0,j));
                filetitle.put (title, file);
                titlefound=true;
                needLine=false;
            }
            else
                title=title.concat(" "+str);
        }

        else if ((i=str.toUpperCase().indexOf(""))!=-1)
            {
                i+=7;
                if ((j=str.toUpperCase().indexOf(""))!=-1)
                {
                    title=str.substring(i,j);
                    filetitle.put (title, file);
                    titlefound=true;
                }
                else
                {
                    needLine=true;
                    title=str.substring(i,str.length());
                }
            }

        if ((str.toUpperCase().indexOf("")!=-1 || str.toUpperCase().indexOf("0)
            return (react());
        else if ((evt.target instanceof Button) && tf.getText().length()>0)
            return (react());
        else return false;
    }

    public void build (GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy)
    {
        gbc.gridx=gx;
        gbc.gridy=gy;
        gbc.gridwidth=gw;
        gbc.gridheight=gh;
        gbc.weightx=wx;
        gbc.weighty=wy;
    }

    public void actionPerformed (ActionEvent e)
    {
        try{
            this.getAppletContext().showDocument (new URL(codeBase, filetitle.get(list.getSelectedItem()).toString()), "_blank");
        }catch (MalformedURLException exc) { }
    }

    public void run ()
    {
        while (runner==Thread.currentThread())
        {
            search();
            runner.stop();
        }
    }

    public void stop()
    {
		try{
	        if (runner.isAlive())
				runner.stop();
		}catch (NullPointerException e){}
    }

    public void destroy ()
    {
        runner=null;
    }

    public boolean react()
    {
        key=tf.getText();
        if (b.getLabel()==stop)
        {
            runner.stop();
            b.setLabel(start);
            return true;
        }
        else
        {
            runner=new Thread (this,"runner");
            runner.start();
            b.setLabel(stop);
            return true;
        }
    }

    public String param () throws NullPointerException, StringIndexOutOfBoundsException
    {
        if (flag==false)
        {
            param=getParameter("param");
            flag=true;
        }
        buf=param.substring(0,param.indexOf(","));
        param=param.substring(param.indexOf(",")+1);
        return buf;
    }
}