KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > lucene > FileDocument


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: FileDocument.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.lucene;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.Reader JavaDoc;
27
28 import org.apache.lucene.document.DateField;
29 import org.apache.lucene.document.Document;
30 import org.apache.lucene.document.Field;
31
32 /**
33  * A utility for making Lucene Documents from a File.
34  */

35 public class FileDocument {
36     private FileDocument() {
37     }
38
39     /**
40      * Makes a document for a File.
41      *
42      * <p>
43      * The document has three fields:
44      *
45      * <ul>
46      * <li>
47      * <code>path</code>--containing the pathname of the file, as a stored, tokenized field;
48      * </li>
49      * <li>
50      * <code>modified</code>--containing the last modified date of the file as a keyword field as
51      * encoded by <a HREF="lucene.document.DateField.html">DateField</a>; and
52      * </li>
53      * <li>
54      * <code>contents</code>--containing the full contents of the file, as a Reader field.
55      * </li>
56      * </ul>
57      * </p>
58      *
59      * @param f DOCUMENT ME!
60      *
61      * @return DOCUMENT ME!
62      *
63      * @throws java.io.FileNotFoundException DOCUMENT ME!
64      */

65     public static Document Document(File JavaDoc f) throws java.io.FileNotFoundException JavaDoc {
66         // make a new, empty document
67
Document doc = new Document();
68
69         // Add the path of the file as a field named "path". Use a Text field, so
70
// that the index stores the path, and so that the path is searchable
71
doc.add(Field.Text("path", f.getPath()));
72
73         // Add the last modified date of the file a field named "modified". Use a
74
// Keyword field, so that it's searchable, but so that no attempt is made
75
// to tokenize the field into words.
76
doc.add(Field.Keyword("modified", DateField.timeToString(f.lastModified())));
77
78         // Add the contents of the file a field named "contents". Use a Text
79
// field, specifying a Reader, so that the text of the file is tokenized.
80
// ?? why doesn't FileReader work here ??
81
FileInputStream JavaDoc is = new FileInputStream JavaDoc(f);
82         Reader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
83         doc.add(Field.Text("contents", reader));
84
85         // return the document
86
return doc;
87     }
88 }
89
Popular Tags