Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
../../../../img/srcFileCovDistChart0.png 86% of files have more coverage
66   159   29   16.5
34   112   0.44   4
4     7.25  
1    
 
  IndexHTML       Line # 30 66 29 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.document.Document;
21    import org.apache.lucene.index.IndexReader;
22    import org.apache.lucene.index.IndexWriter;
23    import org.apache.lucene.index.Term;
24    import org.apache.lucene.index.TermEnum;
25    import java.io.File;
26    import java.util.Date;
27    import java.util.Arrays;
28   
29    /** Indexer for HTML files. */
 
30    public class IndexHTML {
 
31  0 toggle private IndexHTML() {}
32   
33    private static boolean deleting = false; // true during deletion pass
34    private static IndexReader reader; // existing index
35    private static IndexWriter writer; // new index being built
36    private static TermEnum uidIter; // document id iterator
37   
38    /** Indexer for HTML files.*/
 
39  0 toggle public static void main(String[] argv) {
40  0 try {
41  0 String index = "index";
42  0 boolean create = false;
43  0 File root = null;
44   
45  0 String usage = "IndexHTML [-create] [-index <index>] <root_directory>";
46   
47  0 if (argv.length == 0) {
48  0 System.err.println("Usage: " + usage);
49  0 return;
50    }
51   
52  0 for (int i = 0; i < argv.length; i++) {
53  0 if (argv[i].equals("-index")) { // parse -index option
54  0 index = argv[++i];
55  0 } else if (argv[i].equals("-create")) { // parse -create option
56  0 create = true;
57  0 } else if (i != argv.length-1) {
58  0 System.err.println("Usage: " + usage);
59  0 return;
60    } else
61  0 root = new File(argv[i]);
62    }
63   
64  0 Date start = new Date();
65   
66  0 if (!create) { // delete stale docs
67  0 deleting = true;
68  0 indexDocs(root, index, create);
69    }
70  0 writer = new IndexWriter(index, new StandardAnalyzer(), create);
71  0 writer.setMaxFieldLength(1000000);
72  0 indexDocs(root, index, create); // add new docs
73   
74  0 System.out.println("Optimizing index...");
75  0 writer.optimize();
76  0 writer.close();
77   
78  0 Date end = new Date();
79   
80  0 System.out.print(end.getTime() - start.getTime());
81  0 System.out.println(" total milliseconds");
82   
83    } catch (Exception e) {
84  0 System.out.println(" caught a " + e.getClass() +
85    "\n with message: " + e.getMessage());
86    }
87    }
88   
89    /* Walk directory hierarchy in uid order, while keeping uid iterator from
90    /* existing index in sync. Mismatches indicate one of: (a) old documents to
91    /* be deleted; (b) unchanged documents, to be left alone; or (c) new
92    /* documents, to be indexed.
93    */
94   
 
95  0 toggle private static void indexDocs(File file, String index, boolean create)
96    throws Exception {
97  0 if (!create) { // incrementally update
98   
99  0 reader = IndexReader.open(index); // open existing index
100  0 uidIter = reader.terms(new Term("uid", "")); // init uid iterator
101   
102  0 indexDocs(file);
103   
104  0 if (deleting) { // delete rest of stale docs
105  0 while (uidIter.term() != null && uidIter.term().field() == "uid") {
106  0 System.out.println("deleting " +
107    HTMLDocument.uid2url(uidIter.term().text()));
108  0 reader.deleteDocuments(uidIter.term());
109  0 uidIter.next();
110    }
111  0 deleting = false;
112    }
113   
114  0 uidIter.close(); // close uid iterator
115  0 reader.close(); // close existing index
116   
117    } else // don't have exisiting
118  0 indexDocs(file);
119    }
120   
 
121  0 toggle private static void indexDocs(File file) throws Exception {
122  0 if (file.isDirectory()) { // if a directory
123  0 String[] files = file.list(); // list its files
124  0 Arrays.sort(files); // sort the files
125  0 for (int i = 0; i < files.length; i++) // recursively index them
126  0 indexDocs(new File(file, files[i]));
127   
128  0 } else if (file.getPath().endsWith(".html") || // index .html files
129    file.getPath().endsWith(".htm") || // index .htm files
130    file.getPath().endsWith(".txt")) { // index .txt files
131   
132  0 if (uidIter != null) {
133  0 String uid = HTMLDocument.uid(file); // construct uid for doc
134   
135  0 while (uidIter.term() != null && uidIter.term().field() == "uid" &&
136    uidIter.term().text().compareTo(uid) < 0) {
137  0 if (deleting) { // delete stale docs
138  0 System.out.println("deleting " +
139    HTMLDocument.uid2url(uidIter.term().text()));
140  0 reader.deleteDocuments(uidIter.term());
141    }
142  0 uidIter.next();
143    }
144  0 if (uidIter.term() != null && uidIter.term().field() == "uid" &&
145    uidIter.term().text().compareTo(uid) == 0) {
146  0 uidIter.next(); // keep matching docs
147  0 } else if (!deleting) { // add new docs
148  0 Document doc = HTMLDocument.Document(file);
149  0 System.out.println("adding " + doc.get("path"));
150  0 writer.addDocument(doc);
151    }
152    } else { // creating a new index
153  0 Document doc = HTMLDocument.Document(file);
154  0 System.out.println("adding " + doc.get("path"));
155  0 writer.addDocument(doc); // add docs unconditionally
156    }
157    }
158    }
159    }