KickJava   Java API By Example, From Geeks To Geeks.

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


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: IndexConfiguration.java 190175 2005-06-11 20:56:34Z gregor $ */
19
20 package org.apache.lenya.lucene;
21
22 import java.io.File JavaDoc;
23
24 import org.apache.avalon.excalibur.io.FileUtil;
25 import org.apache.lenya.xml.DOMUtil;
26 import org.apache.lenya.xml.DocumentHelper;
27 import org.apache.lenya.xml.XPath;
28 import org.apache.log4j.Category;
29 import org.w3c.dom.Document JavaDoc;
30 import org.w3c.dom.Element JavaDoc;
31
32
33 public class IndexConfiguration {
34     static Category log = Category.getInstance(IndexConfiguration.class);
35     private String JavaDoc configurationFilePath;
36     private String JavaDoc update_index_type;
37     private String JavaDoc index_dir;
38     private String JavaDoc htdocs_dump_dir;
39     private Class JavaDoc indexerClass;
40
41     /**
42      * Creates a new IndexConfiguration object.
43      *
44      * @param configurationFilePath DOCUMENT ME!
45      */

46     public IndexConfiguration(String JavaDoc configurationFilePath) {
47         this.configurationFilePath = configurationFilePath;
48
49         try {
50             File JavaDoc configFile = new File JavaDoc(configurationFilePath);
51             Document JavaDoc document = DocumentHelper.readDocument(configFile);
52             configure(document.getDocumentElement());
53         } catch (Exception JavaDoc e) {
54             log.error("Cannot load publishing configuration! ", e);
55             System.err.println("Cannot load publishing configuration! " + e);
56         }
57     }
58
59     /**
60      * DOCUMENT ME!
61      *
62      * @param args DOCUMENT ME!
63      */

64     public static void main(String JavaDoc[] args) {
65         if (args.length != 1) {
66             System.err.println("Usage: org.apache.lenya.lucene.IndexConfiguration lucene.xconf");
67
68             return;
69         }
70
71         IndexConfiguration ic = new IndexConfiguration(args[0]);
72         String JavaDoc parameter;
73
74         parameter = ic.getUpdateIndexType();
75         System.out.println("Index type: " + parameter);
76
77         parameter = ic.getIndexDir();
78         System.out.println("Index dir: " + parameter);
79         System.out.println("Index dir (resolved): " + ic.resolvePath(parameter));
80
81         parameter = ic.getHTDocsDumpDir();
82         System.out.println("htdocs_dump: " + parameter);
83         System.out.println("htdocs_dump (resolved): " + ic.resolvePath(parameter));
84
85         System.out.println("Indexer class: " + ic.getIndexerClass());
86     }
87
88     /**
89      * DOCUMENT ME!
90      *
91      * @param configuration DOCUMENT ME!
92      *
93      * @throws Exception DOCUMENT ME!
94      */

95     public void configure(Element JavaDoc root) throws Exception JavaDoc {
96         DOMUtil du = new DOMUtil();
97         update_index_type = du.getAttributeValue(root, new XPath("update-index/@type"));
98         index_dir = du.getAttributeValue(root, new XPath("index-dir/@src"));
99         htdocs_dump_dir = du.getAttributeValue(root, new XPath("htdocs-dump-dir/@src"));
100
101         String JavaDoc indexerClassName = du.getAttributeValue(root, new XPath("indexer/@class"));
102         indexerClass = Class.forName(indexerClassName);
103     }
104
105     /**
106      * DOCUMENT ME!
107      *
108      * @return DOCUMENT ME!
109      */

110     public String JavaDoc getUpdateIndexType() {
111         log.debug(".getUpdateIndexType(): " + update_index_type);
112
113         return update_index_type;
114     }
115
116     /**
117      * DOCUMENT ME!
118      *
119      * @return DOCUMENT ME!
120      */

121     public String JavaDoc getIndexDir() {
122         log.debug(".getIndexDir(): " + index_dir);
123
124         return index_dir;
125     }
126
127     /**
128      * DOCUMENT ME!
129      *
130      * @return DOCUMENT ME!
131      */

132     public String JavaDoc getHTDocsDumpDir() {
133         log.debug(".getHTDocsDumpDir(): " + htdocs_dump_dir);
134
135         return htdocs_dump_dir;
136     }
137
138     /**
139      * DOCUMENT ME!
140      *
141      * @return DOCUMENT ME!
142      */

143     public Class JavaDoc getIndexerClass() {
144         log.debug(".getIndexerClass(): " + indexerClass);
145
146         return indexerClass;
147     }
148
149     /**
150      * DOCUMENT ME!
151      *
152      * @param path DOCUMENT ME!
153      *
154      * @return DOCUMENT ME!
155      */

156     public String JavaDoc resolvePath(String JavaDoc path) {
157
158         // nothing to do if we already have an absolute pathname
159
if ( new File JavaDoc(path) .isAbsolute() ) {
160             return path;
161         }
162
163         // from the Java API doc: "A canonical pathname is both absolute and unique."
164
// however we may get an exception while converting a path to it's canonical form
165
try {
166             String JavaDoc configDir = new File JavaDoc(configurationFilePath) .getAbsoluteFile() .getParent();
167             return new File JavaDoc(configDir, path) .getCanonicalPath();
168
169         } catch (java.io.IOException JavaDoc e) {
170             // FIXME: maybe this Exception should be thrown to the caller ?
171
e.printStackTrace();
172             return null;
173         }
174
175     }
176 }
177
Popular Tags