1 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 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 baseDir, String accessRole) throws Exception { 28 String identifier = file.getAbsolutePath() ; 29 String title = file.getName() ; 30 String textToIndex = IOUtil.getFileContenntAsString(file) ; 31 String 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 getPluginIdentifier() { return IDENTIFIER ; } 38 39 public Object getObject(String user, String objectId) throws Exception { 40 return null ; 41 } 42 43 public String getObjectAsText(String user, String objectId) throws Exception { 44 String content = IOUtil.getFileContenntAsString(objectId) ; 45 return content ; 46 } 47 48 public String getObjectAsXHTML(String user, String objectId) throws Exception { 49 String content = IOUtil.getFileContenntAsString(objectId) ; 50 return content ; 51 } 52 53 public String getObjectAsXML(String user, String objectId) throws Exception { 54 String content = IOUtil.getFileContenntAsString(objectId) ; 55 return content ; 56 } 57 58 public void reindexDirectory(String directory, String accessRole, 59 String [] acceptExt, boolean recursive) throws Exception { 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 directory, String accessRole, 66 String [] acceptExt, boolean recursive) throws Exception { 67 FileFilterByExtension filter = new FileFilterByExtension(acceptExt, recursive) ; 68 File dir = new File(directory) ; 69 if(!dir.exists() || dir.isFile()) { 70 throw new Exception (directory + " is not a valid directory.") ; 71 } 72 traverse(dir, filter, directory, accessRole) ; 73 } 74 75 private void traverse(File file, FileFilter filter, String baseDir, String accessRole) throws Exception { 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 |