#!/bin/bash
#
# Copyright (C) 2003 Jennifer Lhotak
#
# 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
#

###########################################################################
#
# Javadoc Search Applet installer, version 1.1
#
###########################################################################
#
# To install the Javadoc Search Applet, just change to the directory
# that is the root of your javadoc-generated documentation, then run this
# script.
#
###########################################################################
###########################################################################

# Make a list of class names and put them in file javadoc_search.html
cat > javadoc_search.html <<EOF
<html>
<head>
<title>
Java Doc Search Applet
</title>
</head>
<body>
<APPLET  CODE = "javadocsearch.jdsearch" CODEBASE = "classes/" WIDTH = "180" HEIGHT = "30" NAME = "JavaDocSearchApplet" ALIGN = "center" VSPACE = "0" HSPACE = "0"></XMP>
<PARAM NAME = CODE VALUE = "javadocsearch.jdsearch" >
<PARAM NAME = CODEBASE VALUE = "classes/" >
<PARAM NAME = NAME VALUE = "JavaDocSearchApplet" >

<PARAM NAME="type" VALUE="null">
<PARAM NAME="scriptable" VALUE="false">
EOF

sed 's/^\(.*\)[Tt][Aa][Rr][Gg][Ee][Tt]="classFrame">\([^<]*\).*/\2/g; t; d;' < allclasses-frame.html |
tr '\n' ' ' |
sed ':start; s/  / /g; t start;' |
sed 's/^/<PARAM NAME="CLASSNAMES" VALUE="/' |
sed 's/ $/">/' >> javadoc_search.html

sed 's/^\(.*\) [Hh][Rr][Ee][Ff]="\([^"]*\)"[^>]*[Tt][Aa][Rr][Gg][Ee][Tt]="classFrame">\(<I>\)*\([^<]*\).*/<PARAM NAME="CNAME\4" VALUE="\2">/g; t; d;' < allclasses-frame.html >> javadoc_search.html

mkdir javadocsearch
cat > javadocsearch/classLinks.java <<EOF
package javadocsearch;
class classLinks {
	public static String[] getClassNames() {
		String[] a = {
EOF
sed 's/^\(.*\) [Hh][Rr][Ee][Ff]="\([^"]*\)"[^>]*[Tt][Aa][Rr][Gg][Ee][Tt]="classFrame">\(<I>\)*\([^<]*\).*/"\4",/g; t; d;' < allclasses-frame.html >> javadocsearch/classLinks.java
cat >> javadocsearch/classLinks.java <<EOF
		"" };
		return a;
	}
	public static String[] getClassLinks() {
		String[] a = {
EOF
sed 's/^\(.*\) [Hh][Rr][Ee][Ff]="\([^"]*\)"[^>]*[Tt][Aa][Rr][Gg][Ee][Tt]="classFrame">\(<I>\)*\([^<]*\).*/"\2",/g; t; d;' < allclasses-frame.html >> javadocsearch/classLinks.java
cat >> javadocsearch/classLinks.java <<EOF
		"" };
		return a;
	}
}
EOF

cat > javadocsearch/jdsearch.java <<EOF
package javadocsearch;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.net.*;
import java.io.*;

public class jdsearch extends JApplet implements KeyListener, ActionListener {
  
  JTextField input;                    // input field
  Vector data = new Vector();          // holds list of class files
  String path;                         // url path
  HashMap dataAndLinks;                // maps classes to links

  /**
   * constructor
   */
  public jdsearch() {
    init();
  }

  /**
   * start applet running
   */
  public void start () {
    buildList();
    buildPath();
    super.start();
  }

  /**
   * construct gui only - create input field and give it focus
   * add listeners
   * initialize content pane
   */
  public void init() {
    input = new JTextField();
    input.requestFocus();
    input.addActionListener(this);
    input.addKeyListener(this);

    JPanel contentPane = new JPanel();
    contentPane.setLayout(new BorderLayout());
    contentPane.add(input, BorderLayout.CENTER);
    setContentPane(contentPane);
  }

  /**
   * process keyTyped event
   */
  public void keyTyped(KeyEvent e) {
  }

  /**
   * process keyPressed event
   */
  public void keyPressed(KeyEvent e) {
  }

  /**
   * process keyReleased event
   * find match and scroll taggedclasses list to correct position
   */
  public void keyReleased(KeyEvent e) {

    String field = input.getText();
    if ((field.length() == 0) || (field == null)){
      return;
    }

    String value = find( field, data, 0);
    
    try {
      //System.out.println(this.getDocumentBase());
      URL go = new URL(this.getDocumentBase().getProtocol(), "", path+"/taggedclasses-frame.html#"+value);
      //URL go = new URL("file", "", path+"/taggedclasses-frame.html#"+value);
      System.out.println(go.toString());
      //System.out.println(go.getProtocol()+" "+go.getHost()+" "+go.getFile());
      this.getAppletContext().showDocument(go, "packageListFrame");
    }
    catch(MalformedURLException e1) {
      System.out.println("MalformedURL Exception: "+e1.getMessage());
    }
  }

  /**
   * display requested doc
   */
  public void actionPerformed(ActionEvent e) {

    String field = input.getText();
    String file;
    
    if ((field.length() == 0) || (field == null)) {
      file = "overview-summary.html";
    }
    else {
      file = (String)dataAndLinks.get(find(field, data, 0));
    }
    
    try {

      //System.out.println(this.getDocumentBase());
      URL go = new URL(this.getDocumentBase().getProtocol(), "", path+"/"+file);
      //URL go = new URL("file", "", path+"/"+file);
      System.out.println(go.toString());
      //System.out.println(go.getProtocol()+" "+go.getHost()+" "+go.getFile());
      this.getAppletContext().showDocument(go, "classFrame");
      
    }
    catch(MalformedURLException e1) {
      System.out.println("MalformedURL Exception: "+e1.getMessage());
    }
  }

  /**
   * finds URL path of docs
   */
  public void buildPath() {
    path = this.getCodeBase().toString();
    int endSlash = path.lastIndexOf("/");
    int start = path.indexOf("/");
    path = path.substring(start, endSlash);
    endSlash = path.lastIndexOf("/");
    path = path.substring(0, endSlash);
  }

  /**
   * constructs lists of classes and links
   */
  private void buildList() {
    data = new Vector();
    dataAndLinks = new HashMap();
    String [] classNamesAll = classLinks.getClassNames();
    String [] classLinksAll = classLinks.getClassLinks();
    for (int i = 0; i < classNamesAll.length; i++) {
      data.add(classNamesAll[i]);
      dataAndLinks.put(classNamesAll[i], classLinksAll[i]);
    }
  }

  /**
   * matches input with class
   */
  public String find(String typed, Vector in, int pos){

    if (pos >= typed.length()) {
      return (String)in.firstElement();
    }
    Vector new_In = new Vector();

    Iterator it = in.iterator();
    while (it.hasNext()){
      String temp = (String)it.next();
      if ((temp.length() == 0) || (temp == null)) {
        break;
      }
      if (pos >= temp.length()) {
      }
      else if (Character.toLowerCase(temp.charAt(pos)) ==
        Character.toLowerCase(typed.charAt(pos))) {
        new_In.add(temp);
      }
    }

    if (new_In.isEmpty()) {
      return (String)in.firstElement();
    }
    else if (new_In.size() == 1) {
      return (String)new_In.firstElement();
    }
    else {
      pos = pos + 1;
      return find(typed, new_In, pos);
    }

  }
}
  
EOF

mkdir classes
javac -d "classes/" javadocsearch/*.java

cat >> javadoc_search.html <<EOF

</APPLET>
</NOEMBED></EMBED></OBJECT>

<!--"END_CONVERTED_APPLET"-->

</body>
</html>
EOF

# Make anchors for classes
sed 's/^\(.*\)[Tt][Aa][Rr][Gg][Ee][Tt]="classFrame">\(<I>\)*\([^<]*\)/<A NAME="\3"><\/A>\1TARGET="classFrame">\2\3/g' < allclasses-frame.html > taggedclasses-frame.html

# Redirect index.html to point to new frame
cp index.html new-allclasses-frame.html
sed 's/"allclasses-frame/"new-allclasses-frame/g' < new-allclasses-frame.html > index.html

# Make new frame
cat > new-allclasses-frame.html <<EOF
<html>
<frameset rows="8%,92%">
<frame src="javadoc_search.html" name="javadocSearchFrame">
<frame src="taggedclasses-frame.html" name="packageListFrame">
</frameset>
</html>
EOF


