1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.help;
29 
30 import java.io.File;
31 import java.io.Reader;
32 import java.io.FileInputStream;
33 import java.io.InputStreamReader;
34 //import java.io.FileReader;
35 import java.io.StringReader;
36 
37 import org.apache.lucene.document.Document;
38 import org.apache.lucene.document.Field;
39 
40 /** Lucene Document for help files */
41 public class HelpFileDocument
42 {
43     /** Creates reader for UTF-8 files
44     */
45     private static Reader getReaderForFile( File aFile )
46         throws java.io.FileNotFoundException, java.io.UnsupportedEncodingException {
47 		Reader aReader;
48 		if( aFile != null ) {
49 			FileInputStream fis = new FileInputStream( aFile );
50 			aReader = new InputStreamReader( fis, "UTF-8" );
51 		}
52 		else {
53 			aReader = new StringReader( "" );
54 		}
55 		return aReader;
56     }
57 
58     /** Makes a document for a File.
59     */
60     public static Document Document( String aModule, File aCaptionFile, File aContentFile )
61         throws java.io.FileNotFoundException, java.io.UnsupportedEncodingException {
62 		Document doc = new Document();
63 
64 		// Add the path of the file as a field named "path".  Use a field that is
65 		// indexed (i.e. searchable), but don't tokenize the field into words.
66 		File aFile = aCaptionFile != null ? aCaptionFile : aContentFile;
67 		if( aFile != null )
68 		{
69 			String aPath = "#HLP#" + aModule + "/" + aFile.getName();
70 			doc.add(new Field("path", aPath, Field.Store.YES, Field.Index.UN_TOKENIZED));
71 		}
72 
73 		// Add the caption of the file to a field named "caption".  Specify a Reader,
74 		// so that the text of the file is tokenized and indexed, but not stored.
75 		doc.add( new Field( "caption", getReaderForFile( aCaptionFile ) ) );
76 
77 		// Add the contents of the file to a field named "content".  Specify a Reader,
78 		// so that the text of the file is tokenized and indexed, but not stored.
79 		doc.add( new Field( "content", getReaderForFile( aContentFile ) ) );
80 
81 		// return the document
82 		return doc;
83 	}
84 
85     private HelpFileDocument() {}
86 }
87