_Design Patterns, Java, and Web Development_
by Martin Remy
Listing One
Enter one or more keywords and click "Query".
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
}
}