001    package railo.runtime.search.lucene2.docs;
002    
003    import java.io.IOException;
004    import java.io.InputStream;
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    
012    /** A utility for making Lucene Documents from a File. */
013    
014    public final class PlainDocument {
015        
016        private static final int SUMMERY_SIZE=20;
017    
018        //private static final char FILE_SEPARATOR = System.getProperty("file.separator").charAt(0);
019      /** Makes a document for a File.
020        <p>
021        The document has three fields:
022        <ul>
023        <li><code>path</code>--containing the pathname of the file, as a stored,
024        tokenized field;
025        <li><code>modified</code>--containing the last modified date of the file as
026        a keyword field as encoded by <a
027        href="lucene.document.DateField.html">DateField</a>; and
028        <li><code>contents</code>--containing the full contents of the file, as a
029        Reader field;
030     * @param f
031     * @return matching document
032     * @throws IOException
033        */
034      public static Document Document(Resource f,String charset)
035           throws IOException {
036             
037        // make a new, empty document
038        Document doc = new Document();
039        
040        doc.add(FieldUtil.UnIndexed("path", f.getPath()));
041    
042        InputStream is = null;
043        try {
044            is=IOUtil.toBufferedInputStream(f.getInputStream());
045            String content=IOUtil.toString(is,charset);
046            FieldUtil.setMimeType(doc, "text/plain");
047            FieldUtil.setRaw(doc,content);
048            FieldUtil.setContent(doc, content);
049            //doc.add(FieldUtil.Text("contents", content.toLowerCase()));
050            FieldUtil.setSummary(doc, StringUtil.max(content,SUMMERY_SIZE),false);
051        }
052        finally {
053            IOUtil.closeEL(is);
054        }
055        
056        //Reader reader = new BufferedReader(new InputStreamReader(is));
057       
058    
059        // return the document
060        return doc;
061      }
062    
063      private PlainDocument() {}
064    }
065