001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.search.lucene2.docs;
020
021import java.io.IOException;
022import java.io.InputStream;
023
024import lucee.commons.io.IOUtil;
025import lucee.commons.io.res.Resource;
026import lucee.commons.lang.StringUtil;
027
028import org.apache.lucene.document.Document;
029
030/** A utility for making Lucene Documents from a File. */
031
032public final class PlainDocument {
033    
034    private static final int SUMMERY_SIZE=20;
035
036    //private static final char FILE_SEPARATOR = System.getProperty("file.separator").charAt(0);
037  /** Makes a document for a File.
038    <p>
039    The document has three fields:
040    <ul>
041    <li><code>path</code>--containing the pathname of the file, as a stored,
042    tokenized field;
043    <li><code>modified</code>--containing the last modified date of the file as
044    a keyword field as encoded by <a
045    href="lucene.document.DateField.html">DateField</a>; and
046    <li><code>contents</code>--containing the full contents of the file, as a
047    Reader field;
048 * @param f
049 * @return matching document
050 * @throws IOException
051    */
052  public static Document Document(Resource f,String charset)
053       throws IOException {
054         
055    // make a new, empty document
056    Document doc = new Document();
057    
058    doc.add(FieldUtil.UnIndexed("path", f.getPath()));
059
060    InputStream is = null;
061    try {
062        is=IOUtil.toBufferedInputStream(f.getInputStream());
063        String content=IOUtil.toString(is,charset);
064        FieldUtil.setMimeType(doc, "text/plain");
065        FieldUtil.setRaw(doc,content);
066        FieldUtil.setContent(doc, content);
067        //doc.add(FieldUtil.Text("contents", content.toLowerCase()));
068        FieldUtil.setSummary(doc, StringUtil.max(content,SUMMERY_SIZE),false);
069    }
070    finally {
071        IOUtil.closeEL(is);
072    }
073    
074    //Reader reader = new BufferedReader(new InputStreamReader(is));
075   
076
077    // return the document
078    return doc;
079  }
080
081  private PlainDocument() {}
082}
083