KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > configuration > CmsSearchConfiguration


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/configuration/CmsSearchConfiguration.java,v $
3  * Date : $Date: 2006/03/27 14:52:46 $
4  * Version: $Revision: 1.17 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.configuration;
33
34 import org.opencms.main.CmsLog;
35 import org.opencms.main.OpenCms;
36 import org.opencms.search.CmsSearchAnalyzer;
37 import org.opencms.search.CmsSearchDocumentType;
38 import org.opencms.search.CmsSearchIndex;
39 import org.opencms.search.CmsSearchIndexSource;
40 import org.opencms.search.CmsSearchManager;
41
42 import java.util.ArrayList JavaDoc;
43 import java.util.Collections JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import java.util.Map JavaDoc;
47
48 import org.apache.commons.digester.Digester;
49
50 import org.dom4j.Element;
51
52 /**
53  * Lucene search configuration class.<p>
54  *
55  * @author Thomas Weckert
56  *
57  * @version $Revision: 1.17 $
58  *
59  * @since 6.0.0
60  */

61 public class CmsSearchConfiguration extends A_CmsXmlConfiguration implements I_CmsXmlConfiguration {
62
63     /** The name of the DTD for this configuration. */
64     public static final String JavaDoc CONFIGURATION_DTD_NAME = "opencms-search.dtd";
65
66     /** The name of the default XML file for this configuration. */
67     public static final String JavaDoc DEFAULT_XML_FILE_NAME = "opencms-search.xml";
68
69     /** Node name constant. */
70     public static final String JavaDoc N_ANALYZER = "analyzer";
71
72     /** Node name constant. */
73     public static final String JavaDoc N_ANALYZERS = "analyzers";
74
75     /** Node name constant. */
76     public static final String JavaDoc N_CACHE = "cache";
77
78     /** Node name constant. */
79     public static final String JavaDoc N_CLASS = "class";
80
81     /** Node name constant. */
82     public static final String JavaDoc N_DIRECTORY = "directory";
83
84     /** Node name constant. */
85     public static final String JavaDoc N_DOCUMENTTYPE = "documenttype";
86
87     /** Node name constant. */
88     public static final String JavaDoc N_DOCUMENTTYPES = "documenttypes";
89
90     /** Node name constant. */
91     public static final String JavaDoc N_DOCUMENTTYPES_INDEXED = "documenttypes-indexed";
92
93     /** Node name constant. */
94     public static final String JavaDoc N_EXCERPT = "excerpt";
95
96     /** Node name constant. */
97     public static final String JavaDoc N_HIGHLIGHTER = "highlighter";
98
99     /** Node name constant. */
100     public static final String JavaDoc N_INDEX = "index";
101
102     /** Node name constant. */
103     public static final String JavaDoc N_INDEXER = "indexer";
104
105     /** Node name constant. */
106     public static final String JavaDoc N_INDEXES = "indexes";
107
108     /** Node name constant. */
109     public static final String JavaDoc N_INDEXSOURCE = "indexsource";
110
111     /** Node name constant. */
112     public static final String JavaDoc N_INDEXSOURCES = "indexsources";
113
114     /** Node name constant. */
115     public static final String JavaDoc N_LOCALE = "locale";
116
117     /** Node name constant. */
118     public static final String JavaDoc N_MIMETYPE = "mimetype";
119
120     /** Node name constant. */
121     public static final String JavaDoc N_MIMETYPES = "mimetypes";
122
123     /** Node name constant. */
124     public static final String JavaDoc N_PROJECT = "project";
125
126     /** Node name constant. */
127     public static final String JavaDoc N_REBUILD = "rebuild";
128
129     /** Node name constant. */
130     public static final String JavaDoc N_RESOURCES = "resources";
131
132     /** Node name constant. */
133     public static final String JavaDoc N_RESOURCETYPE = "resourcetype";
134
135     /** Node name constant. */
136     public static final String JavaDoc N_RESOURCETYPES = "resourcetypes";
137
138     /** Node name constant. */
139     public static final String JavaDoc N_SEARCH = "search";
140
141     /** Node name constant. */
142     public static final String JavaDoc N_SOURCE = "source";
143
144     /** Node name constant. */
145     public static final String JavaDoc N_SOURCES = "sources";
146
147     /** Node name constant. */
148     public static final String JavaDoc N_STEMMER = "stemmer";
149
150     /** Node name constant. */
151     public static final String JavaDoc N_TIMEOUT = "timeout";
152
153     /** Node name constant. */
154     private static final String JavaDoc XPATH_SEARCH = "*/" + N_SEARCH;
155
156     /** The configured search manager. */
157     private CmsSearchManager m_searchManager;
158
159     /**
160      * Public constructor, will be called by configuration manager.<p>
161      */

162     public CmsSearchConfiguration() {
163
164         setXmlFileName(DEFAULT_XML_FILE_NAME);
165         if (CmsLog.INIT.isInfoEnabled()) {
166             CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SEARCH_CONFIG_INIT_0));
167         }
168     }
169
170     /**
171      * @see org.opencms.configuration.I_CmsXmlConfiguration#addXmlDigesterRules(org.apache.commons.digester.Digester)
172      */

173     public void addXmlDigesterRules(Digester digester) {
174
175         String JavaDoc xPath = null;
176
177         // add finish rule
178
digester.addCallMethod(XPATH_SEARCH, "initializeFinished");
179
180         // creation of the search manager
181
digester.addObjectCreate(XPATH_SEARCH, CmsSearchManager.class);
182
183         // search manager finished
184
digester.addSetNext(XPATH_SEARCH, "setSearchManager");
185
186         // result cache size rule
187
digester.addCallMethod(XPATH_SEARCH + "/" + N_CACHE, "setResultCacheSize", 0);
188
189         // directory rule
190
digester.addCallMethod(XPATH_SEARCH + "/" + N_DIRECTORY, "setDirectory", 0);
191
192         // timeout rule
193
digester.addCallMethod(XPATH_SEARCH + "/" + N_TIMEOUT, "setTimeout", 0);
194
195         // rule for the max. char. lenght of the search result excerpt
196
digester.addCallMethod(XPATH_SEARCH + "/" + N_EXCERPT, "setMaxExcerptLength", 0);
197
198         // rule for the highlighter to highlight the search terms in the excerpt of the search result
199
digester.addCallMethod(XPATH_SEARCH + "/" + N_HIGHLIGHTER, "setHighlighter", 0);
200
201         // document type rule
202
xPath = XPATH_SEARCH + "/" + N_DOCUMENTTYPES + "/" + N_DOCUMENTTYPE;
203         digester.addObjectCreate(xPath, CmsSearchDocumentType.class);
204         digester.addCallMethod(xPath + "/" + N_NAME, "setName", 0);
205         digester.addCallMethod(xPath + "/" + N_CLASS, "setClassName", 0);
206         digester.addCallMethod(xPath + "/" + N_MIMETYPES + "/" + N_MIMETYPE, "addMimeType", 0);
207         digester.addCallMethod(xPath + "/" + N_RESOURCETYPES + "/" + N_RESOURCETYPE, "addResourceType", 0);
208         digester.addSetNext(xPath, "addDocumentTypeConfig");
209
210         // analyzer rule
211
xPath = XPATH_SEARCH + "/" + N_ANALYZERS + "/" + N_ANALYZER;
212         digester.addObjectCreate(xPath, CmsSearchAnalyzer.class);
213         digester.addCallMethod(xPath + "/" + N_CLASS, "setClassName", 0);
214         digester.addCallMethod(xPath + "/" + N_STEMMER, "setStemmerAlgorithm", 0);
215         digester.addCallMethod(xPath + "/" + N_LOCALE, "setLocale", 0);
216         digester.addSetNext(xPath, "addAnalyzer");
217
218         // search index rule
219
xPath = XPATH_SEARCH + "/" + N_INDEXES + "/" + N_INDEX;
220         digester.addObjectCreate(xPath, CmsSearchIndex.class);
221         digester.addCallMethod(xPath + "/" + N_NAME, "setName", 0);
222         digester.addCallMethod(xPath + "/" + N_REBUILD, "setRebuildMode", 0);
223         digester.addCallMethod(xPath + "/" + N_PROJECT, "setProjectName", 0);
224         digester.addCallMethod(xPath + "/" + N_LOCALE, "setLocale", 0);
225         digester.addCallMethod(xPath + "/" + N_SOURCES + "/" + N_SOURCE, "addSourceName", 0);
226         digester.addSetNext(xPath, "addSearchIndex");
227
228         // search index source rule
229
xPath = XPATH_SEARCH + "/" + N_INDEXSOURCES + "/" + N_INDEXSOURCE;
230         digester.addObjectCreate(xPath, CmsSearchIndexSource.class);
231         digester.addCallMethod(xPath + "/" + N_NAME, "setName", 0);
232         digester.addCallMethod(xPath + "/" + N_INDEXER, "setIndexerClassName", 1);
233         digester.addCallParam(xPath + "/" + N_INDEXER, 0, N_CLASS);
234         digester.addCallMethod(xPath + "/" + N_RESOURCES + "/" + N_RESOURCE, "addResourceName", 0);
235         digester.addCallMethod(xPath + "/" + N_DOCUMENTTYPES_INDEXED + "/" + N_NAME, "addDocumentType", 0);
236         digester.addSetNext(xPath, "addSearchIndexSource");
237
238         // generic <param> parameter rules
239
digester.addCallMethod(
240             "*/" + I_CmsXmlConfiguration.N_PARAM,
241             I_CmsConfigurationParameterHandler.ADD_PARAMETER_METHOD,
242             2);
243         digester.addCallParam("*/" + I_CmsXmlConfiguration.N_PARAM, 0, I_CmsXmlConfiguration.A_NAME);
244         digester.addCallParam("*/" + I_CmsXmlConfiguration.N_PARAM, 1);
245     }
246
247     /**
248      * @see org.opencms.configuration.I_CmsXmlConfiguration#generateXml(org.dom4j.Element)
249      */

250     public Element generateXml(Element parent) {
251
252         // add <search> node
253
Element searchElement = parent.addElement(N_SEARCH);
254         if (OpenCms.getRunLevel() >= OpenCms.RUNLEVEL_3_SHELL_ACCESS) {
255             // initialized OpenCms instance is available, use latest values
256
m_searchManager = OpenCms.getSearchManager();
257         }
258
259         // add <cache> element
260
searchElement.addElement(N_CACHE).addText(m_searchManager.getResultCacheSize());
261         // add <directory> element
262
searchElement.addElement(N_DIRECTORY).addText(m_searchManager.getDirectory());
263         // add <timeout> element
264
searchElement.addElement(N_TIMEOUT).addText(m_searchManager.getTimeout());
265         // add <exerpt> element
266
searchElement.addElement(N_EXCERPT).addText(String.valueOf(m_searchManager.getMaxExcerptLength()));
267         // add <highlighter> element
268
searchElement.addElement(N_HIGHLIGHTER).addText(m_searchManager.getHighlighter().getClass().getName());
269
270         // <documenttypes>
271
Element documenttypesElement = searchElement.addElement(N_DOCUMENTTYPES);
272         List JavaDoc docTypeKeyList = new ArrayList JavaDoc(m_searchManager.getDocumentTypeConfigs().keySet());
273         Collections.sort(docTypeKeyList);
274         List JavaDoc sortedDocTypeList = new ArrayList JavaDoc();
275         Iterator JavaDoc i = docTypeKeyList.iterator();
276         while (i.hasNext()) {
277             CmsSearchDocumentType currDocType = m_searchManager.getDocumentTypeConfig((String JavaDoc)i.next());
278             sortedDocTypeList.add(currDocType);
279         }
280         Iterator JavaDoc docTypeIterator = sortedDocTypeList.iterator();
281         while (docTypeIterator.hasNext()) {
282             CmsSearchDocumentType currSearchDocType = (CmsSearchDocumentType)docTypeIterator.next();
283             // add the next <documenttype> element
284
Element documenttypeElement = documenttypesElement.addElement(N_DOCUMENTTYPE);
285             // add <name> element
286
documenttypeElement.addElement(N_NAME).addText(currSearchDocType.getName());
287             // add <class> element
288
documenttypeElement.addElement(N_CLASS).addText(currSearchDocType.getClassName());
289             // add <mimetypes> element
290
Element mimetypesElement = documenttypeElement.addElement(N_MIMETYPES);
291             // get the list of mimetypes to trigger the document factory class
292
Iterator JavaDoc mimeTypesIterator = currSearchDocType.getMimeTypes().iterator();
293             while (mimeTypesIterator.hasNext()) {
294                 // add <mimetype> element(s)
295
mimetypesElement.addElement(N_MIMETYPE).addText((String JavaDoc)mimeTypesIterator.next());
296             }
297             // add <resourcetypes> element
298
Element restypesElement = documenttypeElement.addElement(N_RESOURCETYPES);
299             // get the list of Cms resource types to trigger the document factory
300
Iterator JavaDoc resTypesIterator = currSearchDocType.getResourceTypes().iterator();
301             while (resTypesIterator.hasNext()) {
302                 // add <resourcetype> element(s)
303
restypesElement.addElement(N_RESOURCETYPE).addText((String JavaDoc)resTypesIterator.next());
304             }
305         }
306         // </documenttypes>
307

308         // <analyzers>
309
Element analyzersElement = searchElement.addElement(N_ANALYZERS);
310         List JavaDoc analyzerList = new ArrayList JavaDoc(m_searchManager.getAnalyzers().keySet());
311         // sort Analyzers in ascending order
312
Collections.sort(analyzerList);
313         Iterator JavaDoc analyzersIterator = analyzerList.iterator();
314         while (analyzersIterator.hasNext()) {
315             CmsSearchAnalyzer searchAnalyzer = m_searchManager.getCmsSearchAnalyzer((String JavaDoc)analyzersIterator.next());
316             // add the next <analyzer> element
317
Element analyzerElement = analyzersElement.addElement(N_ANALYZER);
318             // add <class> element
319
analyzerElement.addElement(N_CLASS).addText(searchAnalyzer.getClassName());
320             if (searchAnalyzer.getStemmerAlgorithm() != null) {
321                 // add <stemmer> element
322
analyzerElement.addElement(N_STEMMER).addText(searchAnalyzer.getStemmerAlgorithm());
323             }
324             // add <locale> element
325
analyzerElement.addElement(N_LOCALE).addText(searchAnalyzer.getLocale());
326         }
327         // </analyzers>
328

329         // <indexes>
330
Element indexesElement = searchElement.addElement(N_INDEXES);
331         Iterator JavaDoc indexIterator = m_searchManager.getSearchIndexes().iterator();
332         while (indexIterator.hasNext()) {
333             CmsSearchIndex searchIndex = (CmsSearchIndex)indexIterator.next();
334             // add the next <index> element
335
Element indexElement = indexesElement.addElement(N_INDEX);
336             // add <name> element
337
indexElement.addElement(N_NAME).addText(searchIndex.getName());
338             // add <rebuild> element
339
indexElement.addElement(N_REBUILD).addText(searchIndex.getRebuildMode());
340             // add <project> element
341
indexElement.addElement(N_PROJECT).addText(searchIndex.getProject());
342             // add <locale> element
343
indexElement.addElement(N_LOCALE).addText(searchIndex.getLocale());
344             // add <sources> element
345
Element sourcesElement = indexElement.addElement(N_SOURCES);
346             // iterate above sourcenames
347
Iterator JavaDoc sourcesIterator = searchIndex.getSourceNames().iterator();
348             while (sourcesIterator.hasNext()) {
349                 // add <source> element
350
sourcesElement.addElement(N_SOURCE).addText((String JavaDoc)sourcesIterator.next());
351             }
352             // iterate additional params
353
Map JavaDoc indexConfiguration = searchIndex.getConfiguration();
354             if (indexConfiguration != null) {
355                 Iterator JavaDoc it = indexConfiguration.keySet().iterator();
356                 while (it.hasNext()) {
357                     String JavaDoc name = (String JavaDoc)it.next();
358                     String JavaDoc value = indexConfiguration.get(name).toString();
359                     Element paramNode = indexElement.addElement(N_PARAM);
360                     paramNode.addAttribute(A_NAME, name);
361                     paramNode.addText(value);
362                 }
363             }
364         }
365         // </indexes>
366

367         // <indexsources>
368
Element indexsourcesElement = searchElement.addElement(N_INDEXSOURCES);
369         List JavaDoc indexSources = new ArrayList JavaDoc(m_searchManager.getSearchIndexSources().values());
370         Iterator JavaDoc indexsourceIterator = indexSources.iterator();
371         while (indexsourceIterator.hasNext()) {
372             CmsSearchIndexSource searchIndexSource = (CmsSearchIndexSource)indexsourceIterator.next();
373             // add <indexsource> element(s)
374
Element indexsourceElement = indexsourcesElement.addElement(N_INDEXSOURCE);
375             // add <name> element
376
indexsourceElement.addElement(N_NAME).addText(searchIndexSource.getName());
377             // add <indexer class=""> element
378
Element indexerElement = indexsourceElement.addElement(N_INDEXER).addAttribute(
379                 N_CLASS,
380                 searchIndexSource.getIndexerClassName());
381             Map JavaDoc params = searchIndexSource.getParams();
382             Iterator JavaDoc paramIterator = params.keySet().iterator();
383             while (paramIterator.hasNext()) {
384                 String JavaDoc paramKey = (String JavaDoc)paramIterator.next();
385                 // add <param name=""> element(s)
386
indexerElement.addElement(I_CmsXmlConfiguration.N_PARAM).addAttribute(
387                     I_CmsXmlConfiguration.A_NAME,
388                     paramKey).addText((String JavaDoc)params.get(paramKey));
389             }
390             // add <resources> element
391
Element resourcesElement = indexsourceElement.addElement(N_RESOURCES);
392             Iterator JavaDoc resourceIterator = searchIndexSource.getResourcesNames().iterator();
393             while (resourceIterator.hasNext()) {
394                 // add <resource> element(s)
395
resourcesElement.addElement(N_RESOURCE).addText((String JavaDoc)resourceIterator.next());
396             }
397             // add <documenttypes-indexed> element
398
Element doctypes_indexedElement = indexsourceElement.addElement(N_DOCUMENTTYPES_INDEXED);
399             Iterator JavaDoc doctypesIterator = searchIndexSource.getDocumentTypes().iterator();
400             while (doctypesIterator.hasNext()) {
401                 // add <name> element(s)
402
doctypes_indexedElement.addElement(N_NAME).addText((String JavaDoc)doctypesIterator.next());
403             }
404         }
405         // </indexsources>
406

407         return searchElement;
408     }
409
410     /**
411      * @see org.opencms.configuration.I_CmsXmlConfiguration#getDtdFilename()
412      */

413     public String JavaDoc getDtdFilename() {
414
415         return CONFIGURATION_DTD_NAME;
416     }
417
418     /**
419      * Returns the generated search manager.<p>
420      *
421      * @return the generated search manager
422      */

423     public CmsSearchManager getSearchManager() {
424
425         return m_searchManager;
426     }
427
428     /**
429      * Will be called when configuration of this object is finished.<p>
430      */

431     public void initializeFinished() {
432
433         if (CmsLog.INIT.isInfoEnabled()) {
434             CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SEARCH_CONFIG_FINISHED_0));
435         }
436     }
437
438     /**
439      * Sets the generated search manager.<p>
440      *
441      * @param manager the search manager to set
442      */

443     public void setSearchManager(CmsSearchManager manager) {
444
445         m_searchManager = manager;
446         if (CmsLog.INIT.isInfoEnabled()) {
447             CmsLog.INIT.info(Messages.get().getBundle().key(Messages.INIT_SEARCH_MANAGER_FINISHED_0));
448         }
449     }
450
451 }
452
Popular Tags