1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.help;
25 
26 import java.io.File;
27 import java.io.Reader;
28 import java.io.FileInputStream;
29 import java.io.InputStreamReader;
30 //import java.io.FileReader;
31 import java.io.StringReader;
32 
33 import org.apache.lucene.document.Document;
34 import org.apache.lucene.document.Field;
35 
36 /** Lucene Document for help files */
37 public class HelpFileDocument
38 {
39     /** Creates reader for UTF-8 files
40     */
getReaderForFile( File aFile )41     private static Reader getReaderForFile( File aFile )
42         throws java.io.FileNotFoundException, java.io.UnsupportedEncodingException {
43 		Reader aReader;
44 		if( aFile != null ) {
45 			FileInputStream fis = new FileInputStream( aFile );
46 			aReader = new InputStreamReader( fis, "UTF-8" );
47 		}
48 		else {
49 			aReader = new StringReader( "" );
50 		}
51 		return aReader;
52     }
53 
54     /** Makes a document for a File.
55     */
Document( String aModule, File aCaptionFile, File aContentFile )56     public static Document Document( String aModule, File aCaptionFile, File aContentFile )
57         throws java.io.FileNotFoundException, java.io.UnsupportedEncodingException {
58 		Document doc = new Document();
59 
60 		// Add the path of the file as a field named "path".  Use a field that is
61 		// indexed (i.e. searchable), but don't tokenize the field into words.
62 		File aFile = aCaptionFile != null ? aCaptionFile : aContentFile;
63 		if( aFile != null )
64 		{
65 			String aPath = "#HLP#" + aModule + "/" + aFile.getName();
66 			doc.add(new Field("path", aPath, Field.Store.YES, Field.Index.NOT_ANALYZED));
67 		}
68 
69 		// Add the caption of the file to a field named "caption".  Specify a Reader,
70 		// so that the text of the file is tokenized and indexed, but not stored.
71 		doc.add( new Field( "caption", getReaderForFile( aCaptionFile ) ) );
72 
73 		// Add the contents of the file to a field named "content".  Specify a Reader,
74 		// so that the text of the file is tokenized and indexed, but not stored.
75 		doc.add( new Field( "content", getReaderForFile( aContentFile ) ) );
76 
77 		// return the document
78 		return doc;
79 	}
80 
HelpFileDocument()81     private HelpFileDocument() {}
82 }
83