1 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 ; 43 import java.util.Collections ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.Map ; 47 48 import org.apache.commons.digester.Digester; 49 50 import org.dom4j.Element; 51 52 61 public class CmsSearchConfiguration extends A_CmsXmlConfiguration implements I_CmsXmlConfiguration { 62 63 64 public static final String CONFIGURATION_DTD_NAME = "opencms-search.dtd"; 65 66 67 public static final String DEFAULT_XML_FILE_NAME = "opencms-search.xml"; 68 69 70 public static final String N_ANALYZER = "analyzer"; 71 72 73 public static final String N_ANALYZERS = "analyzers"; 74 75 76 public static final String N_CACHE = "cache"; 77 78 79 public static final String N_CLASS = "class"; 80 81 82 public static final String N_DIRECTORY = "directory"; 83 84 85 public static final String N_DOCUMENTTYPE = "documenttype"; 86 87 88 public static final String N_DOCUMENTTYPES = "documenttypes"; 89 90 91 public static final String N_DOCUMENTTYPES_INDEXED = "documenttypes-indexed"; 92 93 94 public static final String N_EXCERPT = "excerpt"; 95 96 97 public static final String N_HIGHLIGHTER = "highlighter"; 98 99 100 public static final String N_INDEX = "index"; 101 102 103 public static final String N_INDEXER = "indexer"; 104 105 106 public static final String N_INDEXES = "indexes"; 107 108 109 public static final String N_INDEXSOURCE = "indexsource"; 110 111 112 public static final String N_INDEXSOURCES = "indexsources"; 113 114 115 public static final String N_LOCALE = "locale"; 116 117 118 public static final String N_MIMETYPE = "mimetype"; 119 120 121 public static final String N_MIMETYPES = "mimetypes"; 122 123 124 public static final String N_PROJECT = "project"; 125 126 127 public static final String N_REBUILD = "rebuild"; 128 129 130 public static final String N_RESOURCES = "resources"; 131 132 133 public static final String N_RESOURCETYPE = "resourcetype"; 134 135 136 public static final String N_RESOURCETYPES = "resourcetypes"; 137 138 139 public static final String N_SEARCH = "search"; 140 141 142 public static final String N_SOURCE = "source"; 143 144 145 public static final String N_SOURCES = "sources"; 146 147 148 public static final String N_STEMMER = "stemmer"; 149 150 151 public static final String N_TIMEOUT = "timeout"; 152 153 154 private static final String XPATH_SEARCH = "*/" + N_SEARCH; 155 156 157 private CmsSearchManager m_searchManager; 158 159 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 173 public void addXmlDigesterRules(Digester digester) { 174 175 String xPath = null; 176 177 digester.addCallMethod(XPATH_SEARCH, "initializeFinished"); 179 180 digester.addObjectCreate(XPATH_SEARCH, CmsSearchManager.class); 182 183 digester.addSetNext(XPATH_SEARCH, "setSearchManager"); 185 186 digester.addCallMethod(XPATH_SEARCH + "/" + N_CACHE, "setResultCacheSize", 0); 188 189 digester.addCallMethod(XPATH_SEARCH + "/" + N_DIRECTORY, "setDirectory", 0); 191 192 digester.addCallMethod(XPATH_SEARCH + "/" + N_TIMEOUT, "setTimeout", 0); 194 195 digester.addCallMethod(XPATH_SEARCH + "/" + N_EXCERPT, "setMaxExcerptLength", 0); 197 198 digester.addCallMethod(XPATH_SEARCH + "/" + N_HIGHLIGHTER, "setHighlighter", 0); 200 201 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 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 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 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 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 250 public Element generateXml(Element parent) { 251 252 Element searchElement = parent.addElement(N_SEARCH); 254 if (OpenCms.getRunLevel() >= OpenCms.RUNLEVEL_3_SHELL_ACCESS) { 255 m_searchManager = OpenCms.getSearchManager(); 257 } 258 259 searchElement.addElement(N_CACHE).addText(m_searchManager.getResultCacheSize()); 261 searchElement.addElement(N_DIRECTORY).addText(m_searchManager.getDirectory()); 263 searchElement.addElement(N_TIMEOUT).addText(m_searchManager.getTimeout()); 265 searchElement.addElement(N_EXCERPT).addText(String.valueOf(m_searchManager.getMaxExcerptLength())); 267 searchElement.addElement(N_HIGHLIGHTER).addText(m_searchManager.getHighlighter().getClass().getName()); 269 270 Element documenttypesElement = searchElement.addElement(N_DOCUMENTTYPES); 272 List docTypeKeyList = new ArrayList (m_searchManager.getDocumentTypeConfigs().keySet()); 273 Collections.sort(docTypeKeyList); 274 List sortedDocTypeList = new ArrayList (); 275 Iterator i = docTypeKeyList.iterator(); 276 while (i.hasNext()) { 277 CmsSearchDocumentType currDocType = m_searchManager.getDocumentTypeConfig((String )i.next()); 278 sortedDocTypeList.add(currDocType); 279 } 280 Iterator docTypeIterator = sortedDocTypeList.iterator(); 281 while (docTypeIterator.hasNext()) { 282 CmsSearchDocumentType currSearchDocType = (CmsSearchDocumentType)docTypeIterator.next(); 283 Element documenttypeElement = documenttypesElement.addElement(N_DOCUMENTTYPE); 285 documenttypeElement.addElement(N_NAME).addText(currSearchDocType.getName()); 287 documenttypeElement.addElement(N_CLASS).addText(currSearchDocType.getClassName()); 289 Element mimetypesElement = documenttypeElement.addElement(N_MIMETYPES); 291 Iterator mimeTypesIterator = currSearchDocType.getMimeTypes().iterator(); 293 while (mimeTypesIterator.hasNext()) { 294 mimetypesElement.addElement(N_MIMETYPE).addText((String )mimeTypesIterator.next()); 296 } 297 Element restypesElement = documenttypeElement.addElement(N_RESOURCETYPES); 299 Iterator resTypesIterator = currSearchDocType.getResourceTypes().iterator(); 301 while (resTypesIterator.hasNext()) { 302 restypesElement.addElement(N_RESOURCETYPE).addText((String )resTypesIterator.next()); 304 } 305 } 306 308 Element analyzersElement = searchElement.addElement(N_ANALYZERS); 310 List analyzerList = new ArrayList (m_searchManager.getAnalyzers().keySet()); 311 Collections.sort(analyzerList); 313 Iterator analyzersIterator = analyzerList.iterator(); 314 while (analyzersIterator.hasNext()) { 315 CmsSearchAnalyzer searchAnalyzer = m_searchManager.getCmsSearchAnalyzer((String )analyzersIterator.next()); 316 Element analyzerElement = analyzersElement.addElement(N_ANALYZER); 318 analyzerElement.addElement(N_CLASS).addText(searchAnalyzer.getClassName()); 320 if (searchAnalyzer.getStemmerAlgorithm() != null) { 321 analyzerElement.addElement(N_STEMMER).addText(searchAnalyzer.getStemmerAlgorithm()); 323 } 324 analyzerElement.addElement(N_LOCALE).addText(searchAnalyzer.getLocale()); 326 } 327 329 Element indexesElement = searchElement.addElement(N_INDEXES); 331 Iterator indexIterator = m_searchManager.getSearchIndexes().iterator(); 332 while (indexIterator.hasNext()) { 333 CmsSearchIndex searchIndex = (CmsSearchIndex)indexIterator.next(); 334 Element indexElement = indexesElement.addElement(N_INDEX); 336 indexElement.addElement(N_NAME).addText(searchIndex.getName()); 338 indexElement.addElement(N_REBUILD).addText(searchIndex.getRebuildMode()); 340 indexElement.addElement(N_PROJECT).addText(searchIndex.getProject()); 342 indexElement.addElement(N_LOCALE).addText(searchIndex.getLocale()); 344 Element sourcesElement = indexElement.addElement(N_SOURCES); 346 Iterator sourcesIterator = searchIndex.getSourceNames().iterator(); 348 while (sourcesIterator.hasNext()) { 349 sourcesElement.addElement(N_SOURCE).addText((String )sourcesIterator.next()); 351 } 352 Map indexConfiguration = searchIndex.getConfiguration(); 354 if (indexConfiguration != null) { 355 Iterator it = indexConfiguration.keySet().iterator(); 356 while (it.hasNext()) { 357 String name = (String )it.next(); 358 String 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 367 Element indexsourcesElement = searchElement.addElement(N_INDEXSOURCES); 369 List indexSources = new ArrayList (m_searchManager.getSearchIndexSources().values()); 370 Iterator indexsourceIterator = indexSources.iterator(); 371 while (indexsourceIterator.hasNext()) { 372 CmsSearchIndexSource searchIndexSource = (CmsSearchIndexSource)indexsourceIterator.next(); 373 Element indexsourceElement = indexsourcesElement.addElement(N_INDEXSOURCE); 375 indexsourceElement.addElement(N_NAME).addText(searchIndexSource.getName()); 377 Element indexerElement = indexsourceElement.addElement(N_INDEXER).addAttribute( 379 N_CLASS, 380 searchIndexSource.getIndexerClassName()); 381 Map params = searchIndexSource.getParams(); 382 Iterator paramIterator = params.keySet().iterator(); 383 while (paramIterator.hasNext()) { 384 String paramKey = (String )paramIterator.next(); 385 indexerElement.addElement(I_CmsXmlConfiguration.N_PARAM).addAttribute( 387 I_CmsXmlConfiguration.A_NAME, 388 paramKey).addText((String )params.get(paramKey)); 389 } 390 Element resourcesElement = indexsourceElement.addElement(N_RESOURCES); 392 Iterator resourceIterator = searchIndexSource.getResourcesNames().iterator(); 393 while (resourceIterator.hasNext()) { 394 resourcesElement.addElement(N_RESOURCE).addText((String )resourceIterator.next()); 396 } 397 Element doctypes_indexedElement = indexsourceElement.addElement(N_DOCUMENTTYPES_INDEXED); 399 Iterator doctypesIterator = searchIndexSource.getDocumentTypes().iterator(); 400 while (doctypesIterator.hasNext()) { 401 doctypes_indexedElement.addElement(N_NAME).addText((String )doctypesIterator.next()); 403 } 404 } 405 407 return searchElement; 408 } 409 410 413 public String getDtdFilename() { 414 415 return CONFIGURATION_DTD_NAME; 416 } 417 418 423 public CmsSearchManager getSearchManager() { 424 425 return m_searchManager; 426 } 427 428 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 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 |