KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > lucene > index > Index


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: Index.java 43151 2004-07-29 05:51:54Z michi $ */
19
20 package org.apache.lenya.lucene.index;
21
22 import java.io.File JavaDoc;
23 import java.util.Date JavaDoc;
24
25 import org.apache.lenya.lucene.IndexConfiguration;
26 import org.apache.lenya.xml.DOMUtil;
27 import org.apache.lenya.xml.DocumentHelper;
28 import org.apache.lenya.xml.XPath;
29 import org.w3c.dom.Document JavaDoc;
30
31 public class Index {
32     /**
33      * Command line interface
34      *
35      * @param argv Lucene Index Configuration
36      */

37     public static void main(String JavaDoc[] argv) {
38         try {
39             String JavaDoc index = "index";
40             boolean create = false;
41             File JavaDoc root = null;
42
43             String JavaDoc usage = "Index <lucene.xconf> [file]";
44
45             if (argv.length == 0) {
46                 System.err.println("Usage: " + usage);
47
48                 return;
49             }
50
51             IndexConfiguration ie = new IndexConfiguration(argv[0]);
52             index = ie.resolvePath(ie.getIndexDir());
53             root = new File JavaDoc(ie.resolvePath(ie.getHTDocsDumpDir()));
54
55             if (ie.getUpdateIndexType().equals("new")) {
56                 create = true;
57             } else if (ie.getUpdateIndexType().equals("incremental")) {
58                 create = false;
59             } else {
60                 System.err.println("ERROR: No such update-index/@type: " + ie.getUpdateIndexType());
61
62                 return;
63             }
64
65             Date JavaDoc start = new Date JavaDoc();
66
67             Indexer indexer = (Indexer) ie.getIndexerClass().newInstance();
68
69             DOMUtil du = new DOMUtil();
70             String JavaDoc path = argv[0];
71             
72             Document JavaDoc config = DocumentHelper.readDocument(new File JavaDoc(path));
73             indexer.configure(du.getElement(config.getDocumentElement(), new XPath("indexer")), argv[0]);
74
75             if (argv.length == 2) {
76                 indexer.indexDocument(new File JavaDoc(argv[1]));
77                 return;
78             }
79
80             if (create) {
81                 indexer.createIndex(root, new File JavaDoc(index));
82             } else {
83                 indexer.updateIndex(root, new File JavaDoc(index));
84             }
85
86             Date JavaDoc end = new Date JavaDoc();
87
88             System.out.print(end.getTime() - start.getTime());
89             System.out.println(" total milliseconds");
90         } catch (Exception JavaDoc e) {
91             e.printStackTrace(System.out);
92         }
93     }
94
95     /**
96      * Get indexer from configuration
97      */

98     public static Indexer getIndexer(String JavaDoc luceneConfig) throws Exception JavaDoc {
99         IndexConfiguration ie = new IndexConfiguration(luceneConfig);
100         Indexer indexer = (Indexer) ie.getIndexerClass().newInstance();
101         DOMUtil du = new DOMUtil();
102         Document JavaDoc config = DocumentHelper.readDocument(new File JavaDoc(luceneConfig));
103         indexer.configure(du.getElement(config.getDocumentElement(), new XPath("indexer")), luceneConfig);
104
105         return indexer;
106     }
107 }
108
Popular Tags