The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8090 leisch 1
/*============================================================================
1230 leisch 2
 
3
  Project: Simple JAVA Search Engine for Keyword Search
4
 
5
  JAVA Source file for the class SearchEngine
6
 
8090 leisch 7
  COPYRIGHT (C), 1998-2000, Thomas Baier, R Core Development Team
3765 leisch 8
 
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program; if not, write to the Free Software
5458 ripley 21
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 
8090 leisch 23
  $Revision: 1.5 $
1230 leisch 24
 
8090 leisch 25
  $Date: 2000/02/10 17:03:55 $
1230 leisch 26
 
6608 leisch 27
  $Author: leisch $
1230 leisch 28
 
8090 leisch 29
============================================================================*/
1230 leisch 30
 
31
 
32
import java.applet.*;
33
import java.awt.*;
34
import java.net.*;
35
import java.io.*;
36
import java.util.*;
37
 
38
 
39
public class SearchEngine extends Applet
40
{
41
  public SearchEngine ()
42
  {
43
    iIndexTable = null;
44
    iSearchTerm = null;
8090 leisch 45
 
1230 leisch 46
    Tracer.write ("SearchEngine initializing\n");
8090 leisch 47
 
1230 leisch 48
    return;
49
  }
50
 
51
 
52
  public String getAppletInfo ()
53
  {
54
    return "Name: SearchEngine\r\n" +
55
      "Author: Thomas Baier\r\n" +
8090 leisch 56
      "(C) 1998-2000 Thomas Baier, R Core Development Team";
1230 leisch 57
  }
58
 
59
  public void init ()
60
  {
61
    resize(640, 240);
8090 leisch 62
 
1230 leisch 63
    // get the name of the index file
64
    String indexName = getParameter (cIndexKeyword);
65
    String searchTerm = getParameter (cSearchKeyword);
8090 leisch 66
 
1230 leisch 67
    Tracer.write ("Index file is \"" + indexName + "\"\n");
68
    Tracer.write ("Search term is \"" + searchTerm + "\"\n");
8090 leisch 69
 
1230 leisch 70
    // use a default index file if none specified
71
    if (indexName == null) {
72
      indexName = cIndexFile;
73
    }
74
 
75
    iSearchTerm = searchTerm;
76
 
77
    /*
78
     * examine the URL to get the search term...
79
     *
80
     * if the URL ends with ?SEARCHTERM=xxxxx we know, xxxxx is the search term
81
     */
82
    {
83
      URL url = getDocumentBase ();
84
      String urlString = url.toString ();
85
      int index = urlString.indexOf ("?" + cSearchKeyword + "=");
86
 
87
      Tracer.write ("URL is \"" + urlString + "\"\n");
88
 
89
      // if found, take the rest as the search string
90
      if (index >= 0) {
91
	iSearchTerm =
92
	  urlString.substring (index + 2 + cSearchKeyword.length ());
93
	Tracer.write ("found search term \"" + iSearchTerm + "\" in URL\n");
94
      }
95
    }
96
 
97
    readIndexFile (indexName);
98
 
99
    return;
100
  }
101
 
102
 
103
  public void destroy ()
104
  {
105
    return;
106
  }
107
 
108
 
109
  public void paint (Graphics g)
110
  {
111
 
112
    return;
113
  }
114
 
115
 
116
  public void start ()
117
  {
118
    Tracer.start ();
119
    return;
120
  }
121
 
122
 
123
  public void stop ()
124
  {
125
    Tracer.stop ();
126
    return;
127
  }
128
 
8090 leisch 129
  /* perform the search and return result as string */
130
 
131
  public String search (String key, boolean searchDesc,
132
			boolean searchKeywords, boolean searchAliases)
1230 leisch 133
  {
134
    iSearchTerm = key;
8090 leisch 135
 
136
    Tracer.write ("Search for \"" + iSearchTerm + "\" started\n");
1230 leisch 137
 
8090 leisch 138
    if(searchDesc){
139
      Tracer.write("Searching in Descriptions\n");
3765 leisch 140
    }
141
 
8090 leisch 142
    if(searchKeywords){
143
      Tracer.write("Searching in Keywords\n");
144
    }
1230 leisch 145
 
8090 leisch 146
    if(searchAliases){
147
      Tracer.write("Searching in Aliases\n");
148
    }
1230 leisch 149
 
150
    Vector foundItems = null;
151
 
152
    if (iSearchTerm != null) {
8090 leisch 153
      foundItems = iIndexTable.search (iSearchTerm, searchDesc,
154
				       searchKeywords, searchAliases);
155
    }
156
    else {
1230 leisch 157
      foundItems = null;
158
    }
159
 
160
    String result = null;
161
 
162
    // if nothing found, return a special string
163
    if (foundItems == null) {
164
      result = "No matches for <b>\"" + 
165
	iSearchTerm +
166
	"\"</b> have been found!<hr>";
8090 leisch 167
    }
168
    else {
169
 
1230 leisch 170
      Enumeration cursor = foundItems.elements ();
171
 
172
      result =
173
	"The search string was <b>\"" +
174
	iSearchTerm +
3765 leisch 175
	"</b>\"" +
1230 leisch 176
	"<hr>" +
177
	"<dl>";
178
 
179
      while (cursor.hasMoreElements ()) {
180
	IndexEntry entry = (IndexEntry) cursor.nextElement ();
181
 
182
	result +=
183
	  "<dt><a href=\"" +
184
	  entry.getURL () +
185
	  "\">" +
8090 leisch 186
	  entry.getEntry () +
1230 leisch 187
	  "</a></dt>\n";
188
	result += "<dd>" + entry.getDescription () + "</dd>\n";
189
      }
190
 
191
      result += "</dl>";
192
    }
193
 
194
    return result;
195
  }
196
 
197
  private void readIndexFile (String idxFile)
198
  {
199
    // create the index table
200
    iIndexTable = new IndexTable ();
201
 
202
    URL baseURL = getCodeBase ();
203
 
204
    // get the index file and parse its contents
205
    try {
206
      URL idxFileURL = new URL (baseURL,idxFile);
207
 
208
      // get an IndexStream object for ease of parsing
209
      IndexStream idxStream = new IndexStream (idxFileURL);
210
 
211
      // now start parsing...
212
 
213
      /*
8090 leisch 214
       * An entry consists of a title, keywords, aliases, an URL and
215
       * a description, everything else is ignored.
216
       * Every entry starts with the keyword
1230 leisch 217
       * "Entry" (case is ignored)
218
       *
219
       * must-have entries are "Entry" and "Keywords"
3765 leisch 220
       *
221
       * 98-06-01: bugfix: don't null the variables
1230 leisch 222
       */
3765 leisch 223
      String entry = "";
224
      String keywords = "";
8090 leisch 225
      String aliases = "";
3765 leisch 226
      String url = "";
1230 leisch 227
      String description = "";
228
      String prefix = "";
229
      String suffix = "";
230
 
231
      Value value = idxStream.popEntry ();
232
 
233
      while (value != null) {
234
	// parse the value now
235
	if (value.getKey ().equalsIgnoreCase ("entry")) {
236
	  // if a new entry is about to start, add the current one
8090 leisch 237
	  addEntry (entry,keywords,aliases,description,url,prefix,suffix);
1230 leisch 238
 
239
	  entry = value.getValue ();
8090 leisch 240
	  aliases = entry;
3765 leisch 241
	  keywords = "";
242
	  url = "";
1230 leisch 243
	  description = "";
244
	} else if (value.getKey ().equalsIgnoreCase ("keywords")) {
3765 leisch 245
	  keywords += value.getValue ();
8090 leisch 246
	} else if (value.getKey ().equalsIgnoreCase ("aliases")) {
247
	  aliases += value.getValue ();
1230 leisch 248
	} else if (value.getKey ().equalsIgnoreCase ("url")) {
249
	  url = prefix + value.getValue () + suffix;
250
	} else if (value.getKey ().equalsIgnoreCase ("description")) {
251
	  description = value.getValue ();
252
	} else if (value.getKey ().equalsIgnoreCase ("prefix")) {
253
	  prefix = value.getValue ();
254
	  Tracer.write ("using new URL prefix \"" + prefix + "\"\n");
255
	} else if (value.getKey ().equalsIgnoreCase ("suffix")) {
256
	  suffix = value.getValue ();
257
	  Tracer.write ("using new URL suffix \"" + suffix + "\"\n");
258
	}
259
	value = idxStream.popEntry ();
260
      }
261
 
262
      // the final entry just read
8090 leisch 263
      addEntry (entry,keywords,aliases,description,url,prefix,suffix);
1230 leisch 264
    } catch (MalformedURLException exc) {
265
      // an error occured while reading...
266
    }
267
 
268
    return;
269
  }
270
 
271
  private void addEntry (String entry,String keywords,
8090 leisch 272
			 String aliases,
1230 leisch 273
			 String description,String url,
274
			 String prefix,String suffix)
275
  {
276
    // the entry must be set
3765 leisch 277
    if (entry.length () == 0) {
1230 leisch 278
      return;
279
    }
280
 
8090 leisch 281
    // if the URL is empty, construct one following the rule:
282
    // URL = prefix + entry + suffix
283
    if (url.length () == 0) {
284
      url = prefix + entry + suffix;
1230 leisch 285
    }
8090 leisch 286
    IndexEntry idxEntry =
287
      new IndexEntry (entry,keywords,aliases,description,url);
288
    iIndexTable.addElement (idxEntry);
1230 leisch 289
 
290
    return;
291
  }
292
 
293
 
8090 leisch 294
  /* Instance Variables */
295
 
1230 leisch 296
  private IndexTable iIndexTable;
297
  private String     iSearchTerm;
298
 
8090 leisch 299
 
300
  /* Static Data */
1230 leisch 301
 
302
  private static final String cIndexFile = "index.txt";
303
  private static final String cIndexKeyword = "INDEXFILE";
304
  private static final String cSearchKeyword = "SEARCHTERM";
305
}
306
 
307
 
308
// Local Variables:
309
// mode: Java
310
// mode: font-lock
311
// End: