Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart0.png 86% of files have more coverage
32   97   13   10.67
14   62   0.41   3
3     4.33  
1    
 
  IndexFiles       Line # 28 32 13 0% 0.0
 
No Tests
 
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 org.apache.lucene.analysis.standard.StandardAnalyzer;
20    import org.apache.lucene.index.IndexWriter;
21   
22    import java.io.File;
23    import java.io.FileNotFoundException;
24    import java.io.IOException;
25    import java.util.Date;
26   
27    /** Index all text files under a directory. */
 
28    public class IndexFiles {
29   
 
30  0 toggle private IndexFiles() {}
31   
32    static final File INDEX_DIR = new File("index");
33   
34    /** Index all text files under a directory. */
 
35  0 toggle public static void main(String[] args) {
36  0 String usage = "java org.apache.lucene.demo.IndexFiles <root_directory>";
37  0 if (args.length == 0) {
38  0 System.err.println("Usage: " + usage);
39  0 System.exit(1);
40    }
41   
42  0 if (INDEX_DIR.exists()) {
43  0 System.out.println("Cannot save index to '" +INDEX_DIR+ "' directory, please delete it first");
44  0 System.exit(1);
45    }
46   
47  0 final File docDir = new File(args[0]);
48  0 if (!docDir.exists() || !docDir.canRead()) {
49  0 System.out.println("Document directory '" +docDir.getAbsolutePath()+ "' does not exist or is not readable, please check the path");
50  0 System.exit(1);
51    }
52   
53  0 Date start = new Date();
54  0 try {
55  0 IndexWriter writer = new IndexWriter(INDEX_DIR, new StandardAnalyzer(), true);
56  0 System.out.println("Indexing to directory '" +INDEX_DIR+ "'...");
57  0 indexDocs(writer, docDir);
58  0 System.out.println("Optimizing...");
59  0 writer.optimize();
60  0 writer.close();
61   
62  0 Date end = new Date();
63  0 System.out.println(end.getTime() - start.getTime() + " total milliseconds");
64   
65    } catch (IOException e) {
66  0 System.out.println(" caught a " + e.getClass() +
67    "\n with message: " + e.getMessage());
68    }
69    }
70   
 
71  0 toggle static void indexDocs(IndexWriter writer, File file)
72    throws IOException {
73    // do not try to index files that cannot be read
74  0 if (file.canRead()) {
75  0 if (file.isDirectory()) {
76  0 String[] files = file.list();
77    // an IO error could occur
78  0 if (files != null) {
79  0 for (int i = 0; i < files.length; i++) {
80  0 indexDocs(writer, new File(file, files[i]));
81    }
82    }
83    } else {
84  0 System.out.println("adding " + file);
85  0 try {
86  0 writer.addDocument(FileDocument.Document(file));
87    }
88    // at least on windows, some temporary files raise this exception with an "access denied" message
89    // checking if the file can be read doesn't help
90    catch (FileNotFoundException fnfe) {
91  0 ;
92    }
93    }
94    }
95    }
96   
97    }