_Design Patterns, Java, and Web Development_
by Martin Remy

Listing One
<html>
<head>

<script language="javascript">
    function setAppletsReady()
    {
        document.appletmonitor.loaded.value=1;
    }
    function appletsReady()
    {
        return parseInt(document.appletmonitor.loaded.value);
    }
    function getKnowledge(strQuery, strPrefix, strPostfix)
    {
        if (!appletsReady()) { return null; }
        return document.Guide.getKnowledge(strQuery, strPrefix, strPostfix);
    }
    function getHistory(strFormat, strPrefix, strPostfix)
    {
        if (!appletsReady()) { return null; }
        return document.Guide.getHistory(strFormat, strPrefix, strPostfix);
    }
    function respond()
    {
      if (appletsReady())
      {
        parent.response.document.open();
        parent.response.document.write("<body background=
                                        /remy/backgrounds/stucco.jpg>");
        parent.response.document.write("<table><tr><td width=
                                         100>&nbsp;</td><td>");
        parent.response.document.write(getKnowledge
                      (document.questionform.query.value,"<br>","<br>"));
        parent.response.document.write("</td><td width=
                                         100>&nbsp;</td></tr></table>");
        parent.response.document.write("</body>");
        parent.response.document.close();
      }
    }
    function showHistory()
    {
      if (appletsReady())
      {
        parent.response.document.open();
        parent.response.document.write("<body background=
                                        /remy/backgrounds/stucco.jpg>");
        parent.response.document.write("<table><tr><td width=
                                              100>&nbsp;</td><td>");
        parent.response.document.write("<h2><i>Here is your personalized 
                                              Yensid guide:</i></h2>");
        parent.response.document.write("Here is a transcript of your session 
                             -- you may print this document if you wish");
        parent.response.document.write("<br>");
        parent.response.document.write(getHistory("<hr>Your query: 
           <h3>%q</h3><h3>Produced the answer:</h3><br>%a","<br>","<br>"));
        parent.response.document.write("</td><td width=
                                              100>&nbsp;</td></tr></table>");
        parent.response.document.write("</body>");
        parent.response.document.close();
      }
    }
</script>
</head>
<body background=/remy/backgrounds/stucco.jpg onLoad="setAppletsReady()">
<applet
        code=Candidate.class
    codebase="/remy/java/"
    name=Guide
        width=2
        height=1>
</applet>
<form name=appletmonitor>
    <input type=hidden name=loaded value=0>
</form>

<center><h4>Enter one or more keywords and click "Query".</h4></center>

<form name=questionform action="javascript:respond()" method=post>
<center><input type=text name=query size=50></center>
<center>
<table><tr>
<a href="javascript:respond()"><img border=0 src="btnQuery.jpg"></a>
<a href="javascript:showhistory()"><img border=0 src="btnHistory.jpg"></a>
</tr>
</table>
</form>
</body>
</html>


Listing Two
import java.applet.*;
import java.util.*;
import java.io.*;
import java.net.*;
import jgl.HashMap;

public class Candidate extends Applet
{
    private Vector vHistory;
    private HashMap hmapKnowledge;
    public static final String ALWAYS = "always";

    public void init()
    {
        hmapKnowledge = new HashMap(true); // allow duplicates
        vHistory = new Vector();
        try 
        {
            URL url = new URL(getDocumentBase(),"knowledge.dat");
            DataInputStream din = new DataInputStream(url.openStream());
            String strKeywords = "";
            String strKnowledge = "";
            boolean fStoreMode=false;

            while (true)
            {
                String buf=din.readLine(); 
                   // break at EOF
                if (buf==null) break;
                   // ignore blank lines
                if (buf.trim().length()<1) continue;
                  // check whether the line is formed as a keyword line
                if ((buf.trim().charAt(0)==':') && 
                                (buf.trim().charAt(buf.length()-1)==':'))
                {
                    if (strKnowledge.length() > 0) 
                    {
                        StringTokenizer tokenizer = 
                                    new StringTokenizer(strKeywords,",");
                        while (tokenizer.hasMoreTokens())
                        {
                            hmapKnowledge.add
                              (tokenizer.nextToken().trim(),strKnowledge);
                        }
                        strKnowledge="";
                    }
                    buf.trim().toLowerCase();
                    strKeywords = buf.substring(1,buf.length()-1);
                    fStoreMode=true;
                }
                else
                {
                    if (fStoreMode)
                    {
                        strKnowledge += buf;
                    }
                }
            }
        } 
        catch (IOException exIO) { exIO.printStackTrace(); }
    }
    public String getKnowledge(String strQuery, String strPrefix, 
                                String strPostfix, boolean fAddToHistory)
    {
          // lowercase the query for search against keywords.
        strQuery=strQuery.toLowerCase();
        Enumeration results;
        Vector vKnowledge = new Vector();
                 
        if (fAddToHistory) vHistory.addElement(strQuery);

        StringTokenizer tokenizer = 
                  new StringTokenizer(strQuery,"'~!@#&();:'\",.<>?/{}[] ");
        while (tokenizer.hasMoreTokens())
        {
            String strWord = tokenizer.nextToken().trim();
            if (strWord.length()<1) continue; // for non-space whitespace
              // first grab relevant items
            results = hmapKnowledge.values(strWord);
            while (results.hasMoreElements())
            {
                String strKnowledgePoint = ((String)results.nextElement());
                if (!vKnowledge.contains(strKnowledgePoint))
                {
                    vKnowledge.addElement(strKnowledgePoint);
                }
            }   
        }
          // now grab the "always" responses
        String strKnowledge = new String();
        results = hmapKnowledge.values(ALWAYS);
        while (results.hasMoreElements())
        {
                String strKnowledgePoint = ((String)results.nextElement());
                if (!vKnowledge.contains(strKnowledgePoint))
                {
                    vKnowledge.addElement(strKnowledgePoint);
                }
        }   
          // now build the string of knowledge
        String strResults = new String();
        results = vKnowledge.elements();

        while (results.hasMoreElements())
        {
            strResults += strPrefix + results.nextElement() + strPostfix;
        }
        return strResults;
    }
    public String getKnowledge(String strQuery, String strPrefix, 
                                                        String strPostfix)
    {
        return getKnowledge(strQuery, strPrefix, strPostfix, true);
    }
    public String getHistory(String strFormat, String strPrefix,
                                                       String strPostfix)
    {
        String strHistory = "";
        Enumeration queries = vHistory.elements();
        int iQPos = strFormat.indexOf("%q");
        int iAPos = strFormat.indexOf("%a");
        while (queries.hasMoreElements())
        {
            String strQuery = ((String)queries.nextElement());
            strHistory += strFormat.substring(0,iQPos) 
                + strQuery
                + strFormat.substring(iQPos+2,iAPos)
                + getKnowledge(strQuery, strPrefix, strPostfix, false);
                // + strFormat.substring(iAPos+2);
        }
        return strHistory;
    }
}


Example 1: 

public class DataStore extends Applet
{
    private HashMap hmap;
    public void init() { /* initialize hmap */ }
    public void put(String strKey, String strValue) { /* enter into hmap */ }
    public String get(String key) { /* retrieve from hmap */ }
    public void applyFilter(Filter f)  { /* ? */ }
    public String currentFilter()  { /* ? */ }
    public void resetFilter()  { /* ? */ }
}


Example 2: 

    public class Processor extends Applet
    {
        public void encryptAndTransfer(String strData) 
{ 
// encipher and send
        }
    }

