KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > indexing > BaseIndexerPlugin


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.indexing;
6
7 import org.apache.lucene.document.Document;
8 import org.apache.lucene.document.Field;
9 import org.apache.lucene.index.Term;
10 import org.apache.lucene.search.IndexSearcher;
11 import org.apache.lucene.search.TermQuery;
12 import org.picocontainer.Startable;
13 /**
14  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
15  * @since Sep 12, 2004
16  * @version $Id: BaseIndexerPlugin.java,v 1.6 2004/10/14 23:27:57 tuan08 Exp $
17  */

18 abstract public class BaseIndexerPlugin implements IndexerPlugin, Startable {
19   final static public String JavaDoc[] MANDATORY_FIELDS =
20     {IndexingService.IDENTIFIER_FIELD, IndexingService.MODULE_FIELD,
21      IndexingService.TITLE_FIELD, IndexingService.DOCUMENT_FIELD};
22   
23   private String JavaDoc[] indexField_ ;
24   private Searcher searcher_ ;
25   protected IndexingService iservice_ ;
26   
27   public BaseIndexerPlugin(IndexingService iservice) {
28     iservice.addIndexerPlugin(this) ;
29     iservice_ = iservice ;
30   }
31   
32   
33   abstract public String JavaDoc getPluginIdentifier() ;
34   
35   final public String JavaDoc[] getMandatoryIndexFields() { return MANDATORY_FIELDS ; }
36   public String JavaDoc[] getCustomizedIndexFields() { return indexField_ ; }
37   
38   synchronized public Searcher getSearcher() throws Exception JavaDoc {
39     if(searcher_ == null) {
40       IndexSearcher isearcher = new IndexSearcher(iservice_.getIndexDatabaseLocation()) ;
41       searcher_ = new Searcher(isearcher, iservice_.getAnalyzer());
42       searcher_.setQueryModules(new TermQuery(new Term(IndexingService.MODULE_FIELD, getPluginIdentifier()))) ;
43     }
44     return searcher_ ;
45   }
46   
47   synchronized public void resetSearcher() {
48     searcher_ = null ;
49   }
50   
51   protected Document createBaseDocument(String JavaDoc identifier, String JavaDoc author,
52                                          String JavaDoc title, String JavaDoc description,
53                                          String JavaDoc textToIndex, String JavaDoc viewRole) {
54     Document doc = new Document();
55     if(description == null) {
56       if(textToIndex.length() < 50 ) description = textToIndex ;
57       else description = textToIndex.substring(0, 50) ;
58     }
59     if(viewRole == null) viewRole = "owner" ;
60     doc.add(Field.Keyword(IndexingService.IDENTIFIER_FIELD, identifier)) ;
61     doc.add(Field.Keyword(IndexingService.MODULE_FIELD, getPluginIdentifier())) ;
62     doc.add(Field.Keyword(IndexingService.AUTHOR_FIELD, author)) ;
63     doc.add(Field.Text(IndexingService.TITLE_FIELD, title)) ;
64     doc.add(Field.Text(IndexingService.DESCRIPTION_FIELD, description)) ;
65     doc.add(Field.UnStored(IndexingService.DOCUMENT_FIELD, textToIndex)) ;
66     doc.add(Field.UnStored(IndexingService.DOCUMENT_ACCESS_ROLE, viewRole)) ;
67     return doc ;
68   }
69   
70   public void removeIndex() throws Exception JavaDoc {
71     Term term = new Term(IndexingService.MODULE_FIELD, getPluginIdentifier()) ;
72     iservice_.queueDeleteDocuments(term) ;
73   }
74   
75   public void reindex() throws Exception JavaDoc {
76     removeIndex() ;
77   }
78   
79   protected String JavaDoc getContentDescription(String JavaDoc text, int size) {
80     char[] chars = text.toCharArray() ;
81     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
82     int counter = 0 ;
83     for(int i = 0 ; i < chars.length && counter < size; i++ ) {
84       if(chars[i] == '<') {
85         while(chars[i] != '>' && i < chars.length) {
86           i++ ;
87         }
88         b.append(" - ") ;
89       } else {
90         b.append(chars[i]) ;
91         counter++ ;
92       }
93     }
94     return b.toString() ;
95   }
96   
97   public void start() {}
98    
99   public void stop() {}
100 }
Popular Tags