KickJava   Java API By Example, From Geeks To Geeks.

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


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: ConfigurableIndexer.java 169196 2005-05-09 00:02:50Z gregor $ */
19
20 package org.apache.lenya.lucene.index;
21
22 import java.io.File JavaDoc;
23 import java.io.FileFilter JavaDoc;
24 import java.io.StringWriter JavaDoc;
25 import java.io.Writer JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URL JavaDoc;
28
29 import javax.xml.transform.Transformer JavaDoc;
30 import javax.xml.transform.TransformerFactory JavaDoc;
31 import javax.xml.transform.dom.DOMSource JavaDoc;
32 import javax.xml.transform.stream.StreamResult JavaDoc;
33 import javax.xml.transform.stream.StreamSource JavaDoc;
34
35 import org.apache.cocoon.util.NetUtils;
36 import org.apache.lenya.xml.DocumentHelper;
37 import org.apache.log4j.Category;
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.Node JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42
43 public class ConfigurableIndexer extends AbstractIndexer {
44     Category log = Category.getInstance(ConfigurableIndexer.class);
45
46     /**
47      * Instantiate a Document Creator for creating Lucene Documents
48      *
49      * @param element <code>indexer</code> node
50      *
51      * @return DocumentCreator
52      *
53      * @throws Exception DOCUMENT ME!
54      */

55     public DocumentCreator createDocumentCreator(Element JavaDoc indexer, String JavaDoc configFileName) throws Exception JavaDoc {
56         log.debug(".createDocumentCreatort(): Element name: " + indexer.getNodeName());
57
58         // FIXME: concat these files ...
59
String JavaDoc configurationFileName = new File JavaDoc(configFileName).getParent() + File.separator + getLuceneDocConfigFileName(indexer);
60         File JavaDoc configurationFile = new File JavaDoc(configurationFileName);
61         String JavaDoc stylesheet = getStylesheet(configurationFile);
62         return new ConfigurableDocumentCreator(stylesheet);
63     }
64
65     public static final String JavaDoc CONFIGURATION_CREATOR_STYLESHEET = "org/apache/lenya/lucene/index/configuration2xslt.xsl";
66
67     /**
68      * Converts the configuration file to an XSLT stylesheet and returns a reader that reads this stylesheet.
69      */

70     protected String JavaDoc getStylesheet(File JavaDoc configurationFile) throws Exception JavaDoc {
71         log.debug(".getStylesheet(): Configuration file: " + configurationFile.getAbsolutePath());
72
73         URL JavaDoc configurationCreatorURL = ConfigurableIndexer.class.getClassLoader().getResource(CONFIGURATION_CREATOR_STYLESHEET);
74         File JavaDoc configurationStylesheetFile = new File JavaDoc(new URI JavaDoc(NetUtils.encodePath(configurationCreatorURL.toString())));
75         Document JavaDoc configurationDocument = DocumentHelper.readDocument(configurationFile);
76
77         TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
78         Transformer JavaDoc configurationTransformer = tFactory.newTransformer(new StreamSource JavaDoc(configurationStylesheetFile));
79
80         DOMSource JavaDoc source = new DOMSource JavaDoc(configurationDocument);
81         Writer JavaDoc stylesheetWriter = new StringWriter JavaDoc();
82         configurationTransformer.transform(source, new StreamResult JavaDoc(stylesheetWriter));
83
84         // Show meta stylesheet which has been created by configuration2xslt.xsl
85
log.debug(".getStylesheet(): Meta Stylesheet: " + stylesheetWriter.toString());
86
87         return stylesheetWriter.toString();
88     }
89
90     /**
91      * Returns the filter used to receive the indexable files.
92      */

93     public FileFilter JavaDoc getFilter(Element JavaDoc indexer, String JavaDoc configFileName) {
94         if (extensionsExists(indexer)) {
95             String JavaDoc[] indexableExtensions = new String JavaDoc[1];
96         indexableExtensions[0] = getExtensions(indexer);
97             return new AbstractIndexer.DefaultIndexFilter(indexableExtensions);
98         } else if (filterExists(indexer)) {
99             return getFilterFromConfiguration(indexer);
100         }
101
102         return new AbstractIndexer.DefaultIndexFilter();
103     }
104
105     /**
106      *
107      */

108     private String JavaDoc getLuceneDocConfigFileName(Element JavaDoc indexer) {
109         String JavaDoc luceneDocConfigFileName = null;
110
111         NodeList JavaDoc nl = indexer.getChildNodes();
112         for (int i = 0; i < nl.getLength(); i++) {
113             Node JavaDoc node = nl.item(i);
114             if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("configuration")) {
115                 log.debug(".getLuceneDocConfigFileName(): Node configuration exists!");
116                 luceneDocConfigFileName = ((Element JavaDoc)node).getAttribute("src");
117             }
118         }
119         if (luceneDocConfigFileName == null) {
120             log.error(".getLuceneDocConfigFileName(): ERROR: Lucene Document Configuration is not specified (indexer/configuration/@src)");
121         }
122         log.debug(".getLuceneDocConfigFileName(): Lucene Document Configuration: " + luceneDocConfigFileName);
123         return luceneDocConfigFileName;
124     }
125
126     /**
127      *
128      */

129     private String JavaDoc getExtensions(Element JavaDoc indexer) {
130         String JavaDoc extensions = null;
131
132         NodeList JavaDoc nl = indexer.getChildNodes();
133         for (int i = 0; i < nl.getLength(); i++) {
134             Node JavaDoc node = nl.item(i);
135             if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("extensions")) {
136                 log.debug("Node extensions exists!");
137                 extensions = ((Element JavaDoc)node).getAttribute("src");
138             }
139         }
140         if (extensions == null) {
141             log.error("Extensions have not been specified (indexer/extensions/@src)");
142         }
143         log.debug("Extensions: " + extensions);
144         return extensions;
145     }
146
147     /**
148      *
149      */

150     private FileFilter JavaDoc getFilterFromConfiguration(Element JavaDoc indexer) {
151         String JavaDoc className = null;
152
153         NodeList JavaDoc nl = indexer.getChildNodes();
154         for (int i = 0; i < nl.getLength(); i++) {
155             Node JavaDoc node = nl.item(i);
156             if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("filter")) {
157                 log.debug("Node filter exists!");
158                 className = ((Element JavaDoc)node).getAttribute("class");
159             }
160         }
161         if (className == null) {
162             log.error("Class name has not been specified (indexer/filter/@class)");
163             return null;
164         }
165         log.debug("Class name: " + className);
166         try {
167             return (FileFilter JavaDoc)Class.forName(className).newInstance();
168         } catch(Exception JavaDoc e) {
169             log.error("" + e);
170         }
171         return null;
172     }
173
174     /**
175      * Check if node <extensions SRC="..."/> exists
176      */

177     private boolean extensionsExists(Element JavaDoc indexer) {
178         NodeList JavaDoc nl = indexer.getChildNodes();
179         for (int i = 0; i < nl.getLength(); i++) {
180             Node JavaDoc node = nl.item(i);
181             if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("extensions")) {
182                 log.debug("Node <extensions SRC=\"...\"/> exist");
183                 return true;
184             }
185         }
186         return false;
187     }
188
189     /**
190      *
191      */

192     private boolean filterExists(Element JavaDoc indexer) {
193         NodeList JavaDoc nl = indexer.getChildNodes();
194         for (int i = 0; i < nl.getLength(); i++) {
195             Node JavaDoc node = nl.item(i);
196             if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals("filter")) {
197                 log.debug("Node filter exists!");
198                 return true;
199             }
200         }
201         return false;
202     }
203 }
204
Popular Tags