KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > indexing > impl > FileIndexerPluginImpl


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.impl;
6
7 import java.io.* ;
8 import java.util.* ;
9 import org.apache.lucene.document.Document;
10 import org.apache.lucene.document.Field;
11 import org.apache.lucene.index.Term;
12 import org.exoplatform.commons.utils.IOUtil;
13 import org.exoplatform.commons.utils.io.FileFilterByExtension;
14 import org.exoplatform.services.indexing.*;
15 /**
16  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
17  * @since Sep 13, 2004
18  * @version $Id: FileIndexerPluginImpl.java,v 1.7 2004/10/25 03:41:16 tuan08 Exp $
19  */

20 public class FileIndexerPluginImpl
21   extends BaseIndexerPlugin implements FileIndexerPlugin {
22   
23   public FileIndexerPluginImpl(IndexingService iservice) {
24     super(iservice) ;
25   }
26   
27   public Document createDocument(File file, String JavaDoc baseDir, String JavaDoc accessRole) throws Exception JavaDoc {
28     String JavaDoc identifier = file.getAbsolutePath() ;
29     String JavaDoc title = file.getName() ;
30     String JavaDoc textToIndex = IOUtil.getFileContenntAsString(file) ;
31     String JavaDoc desc = getContentDescription(textToIndex, 200) ;
32     Document doc = createBaseDocument(identifier,"N/A" ,title, desc , textToIndex, accessRole) ;
33     doc.add(Field.Keyword(BASE_DIR_FIELD, baseDir)) ;
34     return doc ;
35   }
36   
37   public String JavaDoc getPluginIdentifier() { return IDENTIFIER ; }
38   
39   public Object JavaDoc getObject(String JavaDoc user, String JavaDoc objectId) throws Exception JavaDoc {
40     return null ;
41   }
42   
43   public String JavaDoc getObjectAsText(String JavaDoc user, String JavaDoc objectId) throws Exception JavaDoc {
44     String JavaDoc content = IOUtil.getFileContenntAsString(objectId) ;
45     return content ;
46   }
47   
48   public String JavaDoc getObjectAsXHTML(String JavaDoc user, String JavaDoc objectId) throws Exception JavaDoc {
49     String JavaDoc content = IOUtil.getFileContenntAsString(objectId) ;
50     return content ;
51   }
52   
53   public String JavaDoc getObjectAsXML(String JavaDoc user, String JavaDoc objectId) throws Exception JavaDoc {
54     String JavaDoc content = IOUtil.getFileContenntAsString(objectId) ;
55     return content ;
56   }
57   
58   public void reindexDirectory(String JavaDoc directory, String JavaDoc accessRole,
59                                String JavaDoc[] acceptExt, boolean recursive) throws Exception JavaDoc {
60     Term term = new Term(BASE_DIR_FIELD, directory) ;
61     iservice_.queueDeleteDocuments(term) ;
62     indexDirectory(directory, accessRole, acceptExt, recursive) ;
63   }
64   
65   public void indexDirectory(String JavaDoc directory, String JavaDoc accessRole,
66                              String JavaDoc[] acceptExt, boolean recursive) throws Exception JavaDoc {
67     FileFilterByExtension filter = new FileFilterByExtension(acceptExt, recursive) ;
68     File dir = new File(directory) ;
69     if(!dir.exists() || dir.isFile()) {
70       throw new Exception JavaDoc(directory + " is not a valid directory.") ;
71     }
72     traverse(dir, filter, directory, accessRole) ;
73   }
74   
75   private void traverse(File file, FileFilter filter, String JavaDoc baseDir, String JavaDoc accessRole) throws Exception JavaDoc {
76     File[] files = file.listFiles(filter) ;
77     List documents = new ArrayList() ;
78     for(int i = 0; i < files.length; i++) {
79       if(files[i].isFile()) {
80         documents.add(createDocument(files[i], baseDir, accessRole)) ;
81       } else {
82         traverse(files[i], filter, baseDir, accessRole) ;
83       }
84     }
85     if(documents.size() > 0) {
86       iservice_.queueIndexDocuments(documents) ;
87     }
88   }
89 }
Popular Tags