The R Project SVN R

Rev

Rev 1230 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1230 Rev 3765
Line 3... Line 3...
3
  Project: Simple JAVA Search Engine for Keyword Search
3
  Project: Simple JAVA Search Engine for Keyword Search
4
  
4
  
5
  JAVA Source file for the class SearchEngine
5
  JAVA Source file for the class SearchEngine
6
  
6
  
7
  COPYRIGHT (C), 1998, Thomas Baier
7
  COPYRIGHT (C), 1998, Thomas Baier
-
 
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
8
  ALL RIGHTS RESERVED.
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
-
 
21
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
9
  
22
  
10
  $Source: /scratch/CVS-ARCHIVE/R/doc/html/search/SearchEngine.java,v $
23
  $Source: /scratch/CVS-ARCHIVE/R/doc/html/search/SearchEngine.java,v $
11
  
24
  
12
  $Revision: 1.1 $
25
  $Revision: 1.2 $
13
 
26
 
14
  $Date: 1998/05/15 10:38:10 $
27
  $Date: 1999/03/04 17:15:18 $
15
  
28
  
16
  $Author: leisch $
29
  $Author: leisch $
17
 
30
 
18
==============================================================================*/
31
==============================================================================*/
19
 
32
 
Line 37... Line 50...
37
  CONF. TO: 
50
  CONF. TO: 
38
  PURPOSE:  
51
  PURPOSE:  
39
  NOTES:    
52
  NOTES:    
40
 
53
 
41
  HISTORY:  98-04-26: created 
54
  HISTORY:  98-04-26: created 
-
 
55
            98-05-15: new member for search mode
42
------------------------------------------------------------------------------*/
56
------------------------------------------------------------------------------*/
43
public class SearchEngine extends Applet
57
public class SearchEngine extends Applet
44
{
58
{
45
  /*============================================================================
59
  /*============================================================================
46
                                Public methods
60
                                Public methods
Line 56... Line 70...
56
    THROWS:    
70
    THROWS:    
57
    RETURNS:   void
71
    RETURNS:   void
58
  
72
  
59
    HISTORY:   98-04-26: created
73
    HISTORY:   98-04-26: created
60
               98-05-03: now reads the index file
74
               98-05-03: now reads the index file
-
 
75
	       98-05-15: init new member
61
  ----------------------------------------------------------------------------*/
76
  ----------------------------------------------------------------------------*/
62
  public SearchEngine ()
77
  public SearchEngine ()
63
  {
78
  {
64
    iIndexTable = null;
79
    iIndexTable = null;
65
    iSearchTerm = null;
80
    iSearchTerm = null;
-
 
81
    iSearchMode = 0;
66
 
82
 
67
    Tracer.write ("SearchEngine initializing\n");
83
    Tracer.write ("SearchEngine initializing\n");
68
 
84
 
69
    return;
85
    return;
70
  }
86
  }
Line 236... Line 252...
236
  
252
  
237
    HISTORY:   98-05-03: created
253
    HISTORY:   98-05-03: created
238
               98-05-08: new format for output
254
               98-05-08: new format for output
239
	       98-05-09: added trace
255
	       98-05-09: added trace
240
	       98-05-10: now a front-end for search()
256
	       98-05-10: now a front-end for search()
-
 
257
	       98-05-15: new parameter for search-mode
241
  ----------------------------------------------------------------------------*/
258
  ----------------------------------------------------------------------------*/
242
  public String search (String key)
259
  public String search (String key,boolean searchTitles)
243
  {
260
  {
244
    iSearchTerm = key;
261
    iSearchTerm = key;
245
 
262
 
-
 
263
    if (searchTitles) {
-
 
264
      iSearchMode = IndexTable.cSearchDescription;
-
 
265
    } else {
-
 
266
      iSearchMode = 0;
-
 
267
    }
-
 
268
 
246
    return internalSearch ();
269
    return internalSearch ();
247
  }
270
  }
248
 
271
 
249
 
272
 
250
  /*----------------------------------------------------------------------------
273
  /*----------------------------------------------------------------------------
Line 256... Line 279...
256
    PARAMS:    
279
    PARAMS:    
257
    THROWS:    
280
    THROWS:    
258
    RETURNS:   void
281
    RETURNS:   void
259
  
282
  
260
    HISTORY:   98-05-10: created
283
    HISTORY:   98-05-10: created
-
 
284
               98-05-15: forward search-mode
261
  ----------------------------------------------------------------------------*/
285
  ----------------------------------------------------------------------------*/
262
  public String internalSearch ()
286
  public String internalSearch ()
263
  {
287
  {
264
    Tracer.write ("Search for \"" + iSearchTerm + "\" started");
288
    Tracer.write ("Search for \"" + iSearchTerm + "\" started");
265
 
289
 
266
    Vector foundItems = null;
290
    Vector foundItems = null;
267
 
291
 
268
    if (iSearchTerm != null) {
292
    if (iSearchTerm != null) {
269
      foundItems = iIndexTable.search (iSearchTerm);
293
      foundItems = iIndexTable.search (iSearchTerm,iSearchMode);
270
    } else {
294
    } else {
271
      foundItems = null;
295
      foundItems = null;
272
    }
296
    }
273
 
297
 
274
    String result = null;
298
    String result = null;
Line 282... Line 306...
282
      Enumeration cursor = foundItems.elements ();
306
      Enumeration cursor = foundItems.elements ();
283
 
307
 
284
      result =
308
      result =
285
	"The search string was <b>\"" +
309
	"The search string was <b>\"" +
286
	iSearchTerm +
310
	iSearchTerm +
287
	"<b>\"" +
311
	"</b>\"" +
288
	"<hr>" +
312
	"<hr>" +
289
	"<dl>";
313
	"<dl>";
290
 
314
 
291
      while (cursor.hasMoreElements ()) {
315
      while (cursor.hasMoreElements ()) {
292
	IndexEntry entry = (IndexEntry) cursor.nextElement ();
316
	IndexEntry entry = (IndexEntry) cursor.nextElement ();
Line 333... Line 357...
333
    RETURNS:   void
357
    RETURNS:   void
334
  
358
  
335
    HISTORY:   98-04-26: created
359
    HISTORY:   98-04-26: created
336
               98-05-08: now use an IndexStream
360
               98-05-08: now use an IndexStream
337
	       98-05-10: also use prefix and suffix, build URL from first key
361
	       98-05-10: also use prefix and suffix, build URL from first key
-
 
362
	       98-05-19: add "Alias:" entry to keywords
-
 
363
	       98-06-01: bugfix: don't null the variables
338
  ----------------------------------------------------------------------------*/
364
  ----------------------------------------------------------------------------*/
339
  private void readIndexFile (String idxFile)
365
  private void readIndexFile (String idxFile)
340
  {
366
  {
341
    // create the index table
367
    // create the index table
342
    iIndexTable = new IndexTable ();
368
    iIndexTable = new IndexTable ();
Line 356... Line 382...
356
       * An entry consists of a title, keywords, an URL and a description.
382
       * An entry consists of a title, keywords, an URL and a description.
357
       * everything else is ignored. Every entry starts with the keyword
383
       * everything else is ignored. Every entry starts with the keyword
358
       * "Entry" (case is ignored)
384
       * "Entry" (case is ignored)
359
       *
385
       *
360
       * must-have entries are "Entry" and "Keywords"
386
       * must-have entries are "Entry" and "Keywords"
-
 
387
       *
-
 
388
       * 98-06-01: bugfix: don't null the variables
361
       */
389
       */
362
      String entry = null;
390
      String entry = "";
363
      String keywords = null;
391
      String keywords = "";
364
      String url = null;
392
      String url = "";
365
      String description = "";
393
      String description = "";
366
      String prefix = "";
394
      String prefix = "";
367
      String suffix = "";
395
      String suffix = "";
368
 
396
 
369
      Value value = idxStream.popEntry ();
397
      Value value = idxStream.popEntry ();
Line 373... Line 401...
373
	if (value.getKey ().equalsIgnoreCase ("entry")) {
401
	if (value.getKey ().equalsIgnoreCase ("entry")) {
374
	  // if a new entry is about to start, add the current one
402
	  // if a new entry is about to start, add the current one
375
	  addEntry (entry,keywords,description,url,prefix,suffix);
403
	  addEntry (entry,keywords,description,url,prefix,suffix);
376
 
404
 
377
	  entry = value.getValue ();
405
	  entry = value.getValue ();
378
	  keywords = null;
406
	  keywords = "";
379
	  url = null;
407
	  url = "";
380
	  description = "";
408
	  description = "";
381
	} else if (value.getKey ().equalsIgnoreCase ("keywords")) {
409
	} else if (value.getKey ().equalsIgnoreCase ("keywords")) {
382
	  keywords = value.getValue ();
410
	  keywords += value.getValue ();
383
	} else if (value.getKey ().equalsIgnoreCase ("url")) {
411
	} else if (value.getKey ().equalsIgnoreCase ("url")) {
384
	  // use prefix and suffix
412
	  // use prefix and suffix
385
	  url = prefix + value.getValue () + suffix;
413
	  url = prefix + value.getValue () + suffix;
386
	} else if (value.getKey ().equalsIgnoreCase ("description")) {
414
	} else if (value.getKey ().equalsIgnoreCase ("description")) {
387
	  description = value.getValue ();
415
	  description = value.getValue ();
Line 389... Line 417...
389
	  prefix = value.getValue ();
417
	  prefix = value.getValue ();
390
	  Tracer.write ("using new URL prefix \"" + prefix + "\"\n");
418
	  Tracer.write ("using new URL prefix \"" + prefix + "\"\n");
391
	} else if (value.getKey ().equalsIgnoreCase ("suffix")) {
419
	} else if (value.getKey ().equalsIgnoreCase ("suffix")) {
392
	  suffix = value.getValue ();
420
	  suffix = value.getValue ();
393
	  Tracer.write ("using new URL suffix \"" + suffix + "\"\n");
421
	  Tracer.write ("using new URL suffix \"" + suffix + "\"\n");
-
 
422
	} else if (value.getKey ().equalsIgnoreCase ("alias")) {
-
 
423
	  keywords += value.getValue ();
394
	}
424
	}
395
	value = idxStream.popEntry ();
425
	value = idxStream.popEntry ();
396
      }
426
      }
397
 
427
 
398
      // the final entry just read
428
      // the final entry just read
Line 414... Line 444...
414
    PARAMS:    
444
    PARAMS:    
415
    THROWS:    
445
    THROWS:    
416
    RETURNS:   void
446
    RETURNS:   void
417
  
447
  
418
    HISTORY:   98-05-10: created
448
    HISTORY:   98-05-10: created
-
 
449
               98-06-01: value is "", not null if empty
419
  ----------------------------------------------------------------------------*/
450
  ----------------------------------------------------------------------------*/
420
  private void addEntry (String entry,String keywords,
451
  private void addEntry (String entry,String keywords,
421
			 String description,String url,
452
			 String description,String url,
422
			 String prefix,String suffix)
453
			 String prefix,String suffix)
423
  {
454
  {
424
    // the entry must be set
455
    // the entry must be set
425
    if (entry == null) {
456
    if (entry.length () == 0) {
426
      return;
457
      return;
427
    }
458
    }
428
 
459
 
429
    // the keywords must be set, else ignore it
460
    // the keywords must be set, else ignore it
430
    if (keywords != null) {
461
    if (keywords.length () != 0) {
431
      if (url == null) {
462
      if (url.length () == 0) {
432
	// if the URL is empty, construct one following the rule:
463
	// if the URL is empty, construct one following the rule:
433
	// URL = prefix + first keyword + suffix
464
	// URL = prefix + first keyword + suffix
434
	int endOfFirstKeyword = keywords.indexOf (" ");
465
	int endOfFirstKeyword = keywords.indexOf (" ");
435
	
466
	
436
	// because we have trimmed the string, the first character must
467
	// because we have trimmed the string, the first character must
Line 466... Line 497...
466
  /*============================================================================
497
  /*============================================================================
467
                             Instance Variables
498
                             Instance Variables
468
  ============================================================================*/
499
  ============================================================================*/
469
  private IndexTable iIndexTable;
500
  private IndexTable iIndexTable;
470
  private String     iSearchTerm;
501
  private String     iSearchTerm;
-
 
502
  private int        iSearchMode;
471
 
503
 
472
 
504
 
473
  /*============================================================================
505
  /*============================================================================
474
                                Static Data
506
                                Static Data
475
  ============================================================================*/
507
  ============================================================================*/
Line 482... Line 514...
482
/*==============================================================================
514
/*==============================================================================
483
 
515
 
484
  HISTORY:
516
  HISTORY:
485
  
517
  
486
  $Log: SearchEngine.java,v $
518
  $Log: SearchEngine.java,v $
487
  Revision 1.1  1998/05/15 10:38:10  leisch
519
  Revision 1.2  1999/03/04 17:15:18  leisch
488
  New: Search Engine
520
  various bugfixes
-
 
521
 
-
 
522
  Revision 1.1.4.1  1999/03/02 15:19:57  leisch
-
 
523
  search used only kewords, no titles
-
 
524
 
-
 
525
  Revision 1.6  1998/05/19 20:23:13  baier
-
 
526
  added alias support
-
 
527
 
-
 
528
  Revision 1.5  1998/05/15 22:10:05  baier
-
 
529
  allow searching in description, fix bug in results list
489
 
530
 
490
  Revision 1.4  1998/05/10 22:56:53  baier
531
  Revision 1.4  1998/05/10 22:56:53  baier
491
  internal search function, parameter expansion
532
  internal search function, parameter expansion
492
 
533
 
493
  Revision 1.3  1998/05/10 02:44:32  baier
534
  Revision 1.3  1998/05/10 02:44:32  baier