001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.lucene.engine.highlighter;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.compass.core.config.CompassSettings;
026: import org.compass.core.engine.SearchEngineException;
027: import org.compass.core.lucene.LuceneEnvironment;
028: import org.compass.core.util.ClassUtils;
029:
030: /**
031: *
032: * @author kimchy
033: *
034: */
035: public class LuceneHighlighterManager {
036:
037: private static final Log log = LogFactory
038: .getLog(LuceneHighlighterManager.class);
039:
040: private LuceneHighlighterSettings defaultHighlighterSettings;
041:
042: private HashMap highlightersSettings = new HashMap();
043:
044: public void configure(CompassSettings settings)
045: throws SearchEngineException {
046: Map highlighterSettingGroups = settings
047: .getSettingGroups(LuceneEnvironment.Highlighter.PREFIX);
048:
049: for (Iterator it = highlighterSettingGroups.keySet().iterator(); it
050: .hasNext();) {
051: String highlighterName = (String) it.next();
052: if (log.isInfoEnabled()) {
053: log.info("Building highlighter [" + highlighterName
054: + "]");
055: }
056: LuceneHighlighterSettings highlighter = buildHighlighter(
057: highlighterName,
058: (CompassSettings) highlighterSettingGroups
059: .get(highlighterName));
060: highlightersSettings.put(highlighterName, highlighter);
061: }
062: defaultHighlighterSettings = (DefaultLuceneHighlighterSettings) highlightersSettings
063: .get(LuceneEnvironment.Highlighter.DEFAULT_GROUP);
064: if (defaultHighlighterSettings == null) {
065: // if no default highlighter is defined, we need to configre one
066: defaultHighlighterSettings = buildHighlighter(
067: LuceneEnvironment.Highlighter.DEFAULT_GROUP,
068: new CompassSettings(settings.getClassLoader()));
069: highlightersSettings.put(
070: LuceneEnvironment.Highlighter.DEFAULT_GROUP,
071: defaultHighlighterSettings);
072: }
073: }
074:
075: private LuceneHighlighterSettings buildHighlighter(
076: String highlighterName, CompassSettings settings) {
077: String highlighterFactorySetting = settings.getSetting(
078: LuceneEnvironment.Highlighter.FACTORY, null);
079: LuceneHighlighterFactory highlighterFactory;
080: if (highlighterFactorySetting == null) {
081: highlighterFactory = new DefaultLuceneHighlighterFactory();
082: } else {
083: try {
084: highlighterFactory = (LuceneHighlighterFactory) ClassUtils
085: .forName(highlighterFactorySetting,
086: settings.getClassLoader())
087: .newInstance();
088: } catch (Exception e) {
089: throw new SearchEngineException(
090: "Cannot create Highlighter factory ["
091: + highlighterFactorySetting
092: + "]. Please verify the highlighter factory setting at ["
093: + LuceneEnvironment.Highlighter.FACTORY
094: + "]", e);
095: }
096: }
097: return highlighterFactory.createHighlighterSettings(
098: highlighterName, settings);
099: }
100:
101: public LuceneHighlighterSettings getDefaultHighlighterSettings() {
102: return defaultHighlighterSettings;
103: }
104:
105: public LuceneHighlighterSettings getHighlighterSettings(
106: String highlighterName) {
107: return (LuceneHighlighterSettings) highlightersSettings
108: .get(highlighterName);
109: }
110:
111: public LuceneHighlighterSettings getHighlighterSettingsMustExists(
112: String highlighterName) {
113: LuceneHighlighterSettings highlighterSettings = (LuceneHighlighterSettings) highlightersSettings
114: .get(highlighterName);
115: if (highlighterSettings == null) {
116: throw new SearchEngineException(
117: "No highlighter is defined for highlighter name ["
118: + highlighterName + "]");
119: }
120: return highlighterSettings;
121: }
122:
123: }
|