01: package org.apache.lucene.ant;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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, software
14: * distributed under the License is distributed on an "AS IS" BASIS,
15: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16: * See the License for the specific language governing permissions and
17: * limitations under the License.
18: */
19:
20: import org.apache.lucene.document.Document;
21: import org.apache.lucene.document.Field;
22:
23: import java.io.BufferedReader;
24: import java.io.File;
25: import java.io.FileReader;
26: import java.io.IOException;
27: import java.io.StringWriter;
28:
29: /**
30: * A utility for making Lucene Documents from a File.
31: *
32: *@author Erik Hatcher
33: *@since December 6, 2001
34: *@todo Fix JavaDoc comments here
35: */
36:
37: public class TextDocument {
38: private String contents;
39:
40: /**
41: * Constructor for the TextDocument object
42: *
43: *@param file Description of Parameter
44: *@exception IOException Description of Exception
45: */
46: public TextDocument(File file) throws IOException {
47: BufferedReader br = new BufferedReader(new FileReader(file));
48: StringWriter sw = new StringWriter();
49:
50: String line = br.readLine();
51: while (line != null) {
52: sw.write(line);
53: line = br.readLine();
54: }
55: br.close();
56:
57: contents = sw.toString();
58: sw.close();
59: }
60:
61: /**
62: * Makes a document for a File. <p>
63: *
64: * The document has a single field:
65: * <ul>
66: * <li> <code>contents</code>--containing the full contents
67: * of the file, as a Text field;
68: *
69: *@param f Description of Parameter
70: *@return Description of the Returned Value
71: *@exception IOException Description of Exception
72: */
73: public static Document Document(File f) throws IOException {
74:
75: TextDocument textDoc = new TextDocument(f);
76: // make a new, empty document
77: Document doc = new Document();
78:
79: doc.add(new Field("title", f.getName(), Field.Store.YES,
80: Field.Index.TOKENIZED));
81: doc.add(new Field("contents", textDoc.getContents(),
82: Field.Store.YES, Field.Index.TOKENIZED));
83: doc.add(new Field("rawcontents", textDoc.getContents(),
84: Field.Store.YES, Field.Index.NO));
85:
86: // return the document
87: return doc;
88: }
89:
90: /**
91: *@return The contents value
92: *@todo finish this method
93: */
94: public String getContents() {
95: return contents;
96: }
97: }
|