001 package railo.runtime.search.lucene2.docs; 002 003 import java.io.IOException; 004 import java.io.Reader; 005 006 import org.apache.lucene.document.Document; 007 008 import railo.commons.io.IOUtil; 009 import railo.commons.io.res.Resource; 010 import railo.commons.lang.StringUtil; 011 import railo.runtime.op.Caster; 012 013 /** A utility for making Lucene Documents from a File. */ 014 015 public final class FileDocument { 016 017 //private static final char FILE_SEPARATOR = System.getProperty("file.separator").charAt(0); 018 private static final int SUMMERY_SIZE=200; 019 020 /** Makes a document for a File. 021 <p> 022 The document has three fields: 023 <ul> 024 <li><code>path</code>--containing the pathname of the file, as a stored, 025 tokenized field; 026 <li><code>modified</code>--containing the last modified date of the file as 027 a keyword field as encoded by <a 028 href="lucene.document.DateField.html">DateField</a>; and 029 <li><code>contents</code>--containing the full contents of the file, as a 030 Reader field; 031 * @param res 032 * @return matching document 033 * @throws IOException 034 */ 035 public static Document getDocument(Resource res,String charset) 036 throws IOException { 037 038 // make a new, empty document 039 Document doc = new Document(); 040 doc.add(FieldUtil.UnIndexed("mime-type", "text/plain")); 041 042 String content=IOUtil.toString(res,charset); 043 FieldUtil.setRaw(doc,content); 044 //doc.add(FieldUtil.UnIndexed("raw", content)); 045 doc.add(FieldUtil.Text("contents", content.toLowerCase())); 046 doc.add(FieldUtil.UnIndexed("summary",StringUtil.max(content,SUMMERY_SIZE))); 047 return doc; 048 } 049 050 051 public static Document getDocument(StringBuffer content, Reader r) throws IOException { 052 053 // make a new, empty document 054 Document doc = new Document(); 055 FieldUtil.setMimeType(doc, "text/plain"); 056 // 057 String contents=IOUtil.toString(r); 058 if(content!=null)content.append(contents); 059 doc.add(FieldUtil.UnIndexed("size", Caster.toString(contents.length()))); 060 FieldUtil.setContent(doc, contents); 061 FieldUtil.setRaw(doc, contents); 062 FieldUtil.setSummary(doc, StringUtil.max(contents,SUMMERY_SIZE),false); 063 return doc; 064 } 065 066 private FileDocument() {} 067 } 068