Rev 6608 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
/*==============================================================================Project: Simple JAVA Search Engine for Keyword SearchJAVA Source file for the class SearchEngineCOPYRIGHT (C), 1998, Thomas Baier* 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$Source: /scratch/CVS-ARCHIVE/R/doc/html/search/SearchEngine.java,v $$Revision: 1.4 $$Date: 1999/11/18 12:30:58 $$Author: leisch $==============================================================================*//* -------------------------------- Imports --------------------------------- */import java.applet.*;import java.awt.*;import java.net.*;import java.io.*;import java.util.*;/*==============================================================================Interface of class SearchEngine==============================================================================*//*------------------------------------------------------------------------------CLASS: SearchEngineSUPER: AppletCONF. TO:PURPOSE:NOTES:HISTORY: 98-04-26: created98-05-15: new member for search mode------------------------------------------------------------------------------*/public class SearchEngine extends Applet{/*============================================================================Public methods============================================================================*//*----------------------------------------------------------------------------INTERFACE:PURPOSE: default constructorNOTES:PARAMS:THROWS:RETURNS: voidHISTORY: 98-04-26: created98-05-03: now reads the index file98-05-15: init new member----------------------------------------------------------------------------*/public SearchEngine (){iIndexTable = null;iSearchTerm = null;iSearchMode = 0;Tracer.write ("SearchEngine initializing\n");return;}/*----------------------------------------------------------------------------INTERFACE:PURPOSE: return general information about the appletNOTES:PARAMS:THROWS:RETURNS: String: the informationHISTORY: 98-04-26: created----------------------------------------------------------------------------*/public String getAppletInfo (){return "Name: SearchEngine\r\n" +"Author: Thomas Baier\r\n" +"(C) 1998 Thomas Baier, ALL RIGHTS RESERVED";}/*----------------------------------------------------------------------------INTERFACE:PURPOSE: perform initializationNOTES: creates the controlsPARAMS:THROWS:RETURNS: voidHISTORY: 98-04-26: created98-05-03: now a do-nothing----------------------------------------------------------------------------*/public void init (){resize(640, 240);// get the name of the index fileString indexName = getParameter (cIndexKeyword);String searchTerm = getParameter (cSearchKeyword);Tracer.write ("Index file is \"" + indexName + "\"\n");Tracer.write ("Search term is \"" + searchTerm + "\"\n");// use a default index file if none specifiedif (indexName == null) {indexName = cIndexFile;}iSearchTerm = searchTerm;/** examine the URL to get the search term...** if the URL ends with ?SEARCHTERM=xxxxx we know, xxxxx is the search term*/{URL url = getDocumentBase ();String urlString = url.toString ();int index = urlString.indexOf ("?" + cSearchKeyword + "=");Tracer.write ("URL is \"" + urlString + "\"\n");// if found, take the rest as the search stringif (index >= 0) {iSearchTerm =urlString.substring (index + 2 + cSearchKeyword.length ());Tracer.write ("found search term \"" + iSearchTerm + "\" in URL\n");}}readIndexFile (indexName);return;}/*----------------------------------------------------------------------------INTERFACE:PURPOSE: perform "destructor" codeNOTES: not required herePARAMS:THROWS:RETURNS: voidHISTORY: 98-04-26: created----------------------------------------------------------------------------*/public void destroy (){return;}/*----------------------------------------------------------------------------INTERFACE:PURPOSE: perform painting of the appletNOTES: not required for our applet, controls do everythingPARAMS: Graphics g: the graphics context to draw onTHROWS:RETURNS: voidHISTORY: 98-04-26: created----------------------------------------------------------------------------*/public void paint (Graphics g){return;}/*----------------------------------------------------------------------------INTERFACE:PURPOSE: perform startup code everytime visiting the appletNOTES:PARAMS:THROWS:RETURNS: voidHISTORY: 98-04-26: created98-05-10: start the tracer----------------------------------------------------------------------------*/public void start (){Tracer.start ();return;}/*----------------------------------------------------------------------------INTERFACE:PURPOSE: perform cleanup evertime the applet "loses" the focusNOTES:PARAMS:THROWS:RETURNS: voidHISTORY: 98-04-26: created98-05-10: stop the tracer----------------------------------------------------------------------------*/public void stop (){Tracer.stop ();return;}/*----------------------------------------------------------------------------INTERFACE:PURPOSE: perform the search and return the search results as a stringNOTES:PARAMS:THROWS:RETURNS: voidHISTORY: 98-05-03: created98-05-08: new format for output98-05-09: added trace98-05-10: now a front-end for search()98-05-15: new parameter for search-mode----------------------------------------------------------------------------*/public String search (String key,boolean searchTitles){iSearchTerm = key;if (searchTitles) {iSearchMode = IndexTable.cSearchDescription;} else {iSearchMode = 0;}return internalSearch ();}/*----------------------------------------------------------------------------INTERFACE:PURPOSE: perform the search (back-end)NOTES:PARAMS:THROWS:RETURNS: voidHISTORY: 98-05-10: created98-05-15: forward search-mode----------------------------------------------------------------------------*/public String internalSearch (){Tracer.write ("Search for \"" + iSearchTerm + "\" started");Vector foundItems = null;if (iSearchTerm != null) {foundItems = iIndexTable.search (iSearchTerm,iSearchMode);} else {foundItems = null;}String result = null;// if nothing found, return a special stringif (foundItems == null) {result = "No matches for <b>\"" +iSearchTerm +"\"</b> have been found!<hr>";} else {Enumeration cursor = foundItems.elements ();result ="The search string was <b>\"" +iSearchTerm +"</b>\"" +"<hr>" +"<dl>";while (cursor.hasMoreElements ()) {IndexEntry entry = (IndexEntry) cursor.nextElement ();/** the format for every entry is** title* description*/result +="<dt><a href=\"" +entry.getURL () +"\">" +entry.getTitle () +"</a></dt>\n";result += "<dd>" + entry.getDescription () + "</dd>\n";}result += "</dl>";}return result;}/*============================================================================Protected methods============================================================================*//*============================================================================Private methods============================================================================*//*----------------------------------------------------------------------------INTERFACE:PURPOSE: read the index fileNOTES:PARAMS:THROWS:RETURNS: voidHISTORY: 98-04-26: created98-05-08: now use an IndexStream98-05-10: also use prefix and suffix, build URL from first key98-05-19: add "Alias:" entry to keywords98-06-01: bugfix: don't null the variables----------------------------------------------------------------------------*/private void readIndexFile (String idxFile){// create the index tableiIndexTable = new IndexTable ();URL baseURL = getCodeBase ();// get the index file and parse its contentstry {URL idxFileURL = new URL (baseURL,idxFile);// get an IndexStream object for ease of parsingIndexStream idxStream = new IndexStream (idxFileURL);// now start parsing.../** An entry consists of a title, keywords, an URL and a description.* everything else is ignored. Every entry starts with the keyword* "Entry" (case is ignored)** must-have entries are "Entry" and "Keywords"** 98-06-01: bugfix: don't null the variables*/String entry = "";String keywords = "";String url = "";String description = "";String prefix = "";String suffix = "";Value value = idxStream.popEntry ();while (value != null) {// parse the value nowif (value.getKey ().equalsIgnoreCase ("entry")) {// if a new entry is about to start, add the current oneaddEntry (entry,keywords,description,url,prefix,suffix);entry = value.getValue ();keywords = "";url = "";description = "";} else if (value.getKey ().equalsIgnoreCase ("keywords")) {keywords += value.getValue ();} else if (value.getKey ().equalsIgnoreCase ("url")) {// use prefix and suffixurl = prefix + value.getValue () + suffix;} else if (value.getKey ().equalsIgnoreCase ("description")) {description = value.getValue ();} else if (value.getKey ().equalsIgnoreCase ("prefix")) {prefix = value.getValue ();Tracer.write ("using new URL prefix \"" + prefix + "\"\n");} else if (value.getKey ().equalsIgnoreCase ("suffix")) {suffix = value.getValue ();Tracer.write ("using new URL suffix \"" + suffix + "\"\n");} else if (value.getKey ().equalsIgnoreCase ("aliases")) {keywords += value.getValue ();}value = idxStream.popEntry ();}// the final entry just readaddEntry (entry,keywords,description,url,prefix,suffix);} catch (MalformedURLException exc) {// an error occured while reading...}return;}/*----------------------------------------------------------------------------INTERFACE:PURPOSE:NOTES:PARAMS:THROWS:RETURNS: voidHISTORY: 98-05-10: created98-06-01: value is "", not null if empty----------------------------------------------------------------------------*/private void addEntry (String entry,String keywords,String description,String url,String prefix,String suffix){// the entry must be setif (entry.length () == 0) {return;}// the keywords must be set, else ignore itif (keywords.length () != 0) {if (url.length () == 0) {// if the URL is empty, construct one following the rule:// URL = prefix + first keyword + suffixint endOfFirstKeyword = keywords.indexOf (" ");// because we have trimmed the string, the first character must// not be a blankif (endOfFirstKeyword >= 0) {url = keywords.substring (0,endOfFirstKeyword);Tracer.write ("constructing URL, keywords=\"" +keywords + "\"" +"using \"" +url +"\" (results in \"" +prefix + url + suffix + "\")\n");} else {// just a single keywordurl = keywords;Tracer.write ("constructing URL, using keyword \"" +url +"\" (results in \"" +prefix + url + suffix + "\")\n");}// add prefix and suffixurl = prefix + url + suffix;}IndexEntry idxEntry =new IndexEntry (entry,keywords,description,url);iIndexTable.addElement (idxEntry);}return;}/*============================================================================Instance Variables============================================================================*/private IndexTable iIndexTable;private String iSearchTerm;private int iSearchMode;/*============================================================================Static Data============================================================================*/private static final String cIndexFile = "index.txt";private static final String cIndexKeyword = "INDEXFILE";private static final String cSearchKeyword = "SEARCHTERM";}/*==============================================================================HISTORY:$Log: SearchEngine.java,v $Revision 1.4 1999/11/18 12:30:58 leischuse seperate aliases fieldRevision 1.3 1999/08/10 09:56:03 ripleychange FSF address in copyrightsadd some copyrights in src/gnome and elsewhereRevision 1.2 1999/03/04 17:15:18 leischvarious bugfixesRevision 1.1.4.1 1999/03/02 15:19:57 leischsearch used only kewords, no titlesRevision 1.6 1998/05/19 20:23:13 baieradded alias supportRevision 1.5 1998/05/15 22:10:05 baierallow searching in description, fix bug in results listRevision 1.4 1998/05/10 22:56:53 baierinternal search function, parameter expansionRevision 1.3 1998/05/10 02:44:32 baiertraces, output in HTML via JavaScript, new index generationRevision 1.2 1998/04/26 22:36:34 baierdocumentation changesRevision 1.1 1998/04/26 21:32:54 baierInitial revision==============================================================================*/// Local Variables:// mode: Java// mode: font-lock// End: