KickJava   Java API By Example, From Geeks To Geeks.

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


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: IndexFiles.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.lucene;
21
22 import java.io.File JavaDoc;
23 import java.util.Date JavaDoc;
24
25 import org.apache.lucene.analysis.standard.StandardAnalyzer;
26 import org.apache.lucene.index.IndexWriter;
27
28
29 class IndexFiles {
30     /**
31      * DOCUMENT ME!
32      *
33      * @param args DOCUMENT ME!
34      */

35     public static void main(String JavaDoc[] args) {
36         if (args.length != 2) {
37             System.err.println(
38                 "Usage: org.apache.lenya.lucene.IndexFiles \"directory_to_be_indexed\" \"directory_where_index_is_located\"");
39
40             return;
41         }
42
43         File JavaDoc index_directory = new File JavaDoc(args[1]);
44         File JavaDoc directory_to_be_indexed = new File JavaDoc(args[0]);
45
46         if (!directory_to_be_indexed.exists()) {
47             System.err.println("Exception: No such directory: " +
48                 index_directory.getAbsolutePath());
49
50             return;
51         }
52
53         try {
54             Date JavaDoc start = new Date JavaDoc();
55
56             IndexWriter writer = new IndexWriter(index_directory, new StandardAnalyzer(), true);
57             System.out.println("Warning: Directory will be created: " +
58                 index_directory.getAbsolutePath());
59             indexDocs(writer, directory_to_be_indexed);
60
61             writer.optimize();
62             writer.close();
63
64             Date JavaDoc end = new Date JavaDoc();
65
66             System.out.print(end.getTime() - start.getTime());
67             System.out.println(" total milliseconds");
68         } catch (Exception JavaDoc e) {
69             System.out.println(" caught a " + e.getClass() + "\n with message: " + e.getMessage());
70         }
71     }
72
73     /**
74      * DOCUMENT ME!
75      *
76      * @param writer DOCUMENT ME!
77      * @param file DOCUMENT ME!
78      *
79      * @throws Exception DOCUMENT ME!
80      */

81     public static void indexDocs(IndexWriter writer, File JavaDoc file)
82         throws Exception JavaDoc {
83         if (file.isDirectory()) {
84             String JavaDoc[] files = file.list();
85
86             for (int i = 0; i < files.length; i++)
87                 indexDocs(writer, new File JavaDoc(file, files[i]));
88         } else {
89             System.out.println("adding " + file);
90             writer.addDocument(FileDocument.Document(file));
91         }
92     }
93 }
94
Popular Tags