KickJava   Java API By Example, From Geeks To Geeks.

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


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

18
19 import java.io.File JavaDoc;
20 import java.io.Reader JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.BufferedReader JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27 import org.apache.lucene.document.DateField;
28
29 /** A utility for making Lucene Documents from a File. */
30
31 public class FileDocument {
32   /** Makes a document for a File.
33     <p>
34     The document has three fields:
35     <ul>
36     <li><code>path</code>--containing the pathname of the file, as a stored,
37     tokenized field;
38     <li><code>modified</code>--containing the last modified date of the file as
39     a keyword field as encoded by <a
40     href="lucene.document.DateField.html">DateField</a>; and
41     <li><code>contents</code>--containing the full contents of the file, as a
42     Reader field;
43     */

44   public static Document Document(File JavaDoc f)
45        throws java.io.FileNotFoundException JavaDoc {
46      
47     // make a new, empty document
48
Document doc = new Document();
49
50     // Add the path of the file as a field named "path". Use a Text field, so
51
// that the index stores the path, and so that the path is searchable
52
doc.add(Field.Text("path", f.getPath()));
53
54     // Add the last modified date of the file a field named "modified". Use a
55
// Keyword field, so that it's searchable, but so that no attempt is made
56
// to tokenize the field into words.
57
doc.add(Field.Keyword("modified",
58               DateField.timeToString(f.lastModified())));
59
60     // Add the contents of the file a field named "contents". Use a Text
61
// field, specifying a Reader, so that the text of the file is tokenized.
62
// ?? why doesn't FileReader work here ??
63
FileInputStream JavaDoc is = new FileInputStream JavaDoc(f);
64     Reader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
65     doc.add(Field.Text("contents", reader));
66
67     // return the document
68
return doc;
69   }
70
71   private FileDocument() {}
72 }
73     
74
Popular Tags