| /** |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.apache.cocoon.components.search.analyzer; |
| |
| import java.io.Reader; |
| |
| import org.apache.avalon.framework.configuration.Configuration; |
| import org.apache.avalon.framework.configuration.ConfigurationException; |
| import org.apache.avalon.framework.logger.LogEnabled; |
| import org.apache.avalon.framework.logger.Logger; |
| import org.apache.cocoon.components.search.components.AnalyzerManager; |
| import org.apache.cocoon.components.search.utils.SourceHelper; |
| import org.apache.excalibur.source.Source; |
| import org.apache.lucene.analysis.Analyzer; |
| import org.apache.lucene.analysis.TokenStream; |
| |
| /** |
| * Analyzer Wrapper to allow to configure a lucene analyzer with a XML file |
| * |
| * @author Nicolas Maisonneuve |
| */ |
| public abstract class ConfigurableAnalyzer extends Analyzer implements |
| LogEnabled { |
| |
| /** |
| * the lucene analyzer |
| */ |
| protected Analyzer analyzer; |
| |
| /** |
| * a logger |
| */ |
| protected Logger logger; |
| |
| /** |
| * the analyzer manager component |
| */ |
| protected AnalyzerManager analyzerM; |
| |
| /** |
| * Check config file or not (to update the analyzer if the config file |
| * changes) |
| */ |
| private boolean checkConfigFile = false; |
| |
| /** |
| * Configuration file source |
| */ |
| private Source configFile; |
| |
| /** |
| * Configure this analyzer. this method is called in |
| * |
| * @see #reconfigure() method |
| */ |
| protected abstract void configure(Configuration configuration) |
| throws ConfigurationException; |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.apache.lucene.analysis.Analyzer#tokenStream(java.lang.String, |
| * java.io.Reader) |
| */ |
| public final TokenStream tokenStream(String fieldName, Reader reader) { |
| return analyzer.tokenStream(fieldName, reader); |
| } |
| |
| /* |
| * (non-Javadoc) |
| * |
| * @see org.apache.avalon.framework.logger.LogEnabled#enableLogging(org.apache.avalon.framework.logger.Logger) |
| */ |
| public void enableLogging(Logger log) { |
| logger = log; |
| } |
| |
| /** |
| * Enable the check of the config file (to update the analyzer if the config |
| * file changes) when the method |
| * |
| * @see org.apache.cocoon.component.search.components.AnalyzerManager#getAnalyzer(String) |
| * is called |
| * @param check |
| * true if we want that |
| */ |
| public void setEnableCheckFile(boolean check) { |
| this.checkConfigFile = check; |
| } |
| |
| /** |
| * is the checkFile property enable ? |
| */ |
| public boolean enableCheckFile() { |
| return this.checkConfigFile; |
| } |
| |
| /** |
| * reconfigure the analyzer if the config file has changed |
| * |
| * @throws ConfigurationException |
| * @return boolean true if the analyzer is reconfigured (=file has changed) |
| * else false |
| */ |
| public boolean reconfigure() throws ConfigurationException { |
| if (!SourceHelper.checkSourceValidity(configFile)) { |
| logger.info("reconfiguration of " + this.getClass().getName() |
| + " (the source " + configFile.getURI() |
| + " has changed...) "); |
| Configuration conf = SourceHelper.build(configFile); |
| configure(conf); |
| return true; |
| } else { |
| return false; |
| } |
| } |
| |
| /** |
| * Set the configuration file |
| * |
| * @param source |
| * Source configuration file |
| * @throws ConfigurationException |
| */ |
| public void setConfigFile(Source source) throws ConfigurationException { |
| this.configFile = source; |
| SourceHelper.registerSource(configFile); |
| configure(SourceHelper.build(configFile)); |
| } |
| |
| /** |
| * set the analyzerManager |
| * |
| * @param analyzerM |
| * AnalyzerManager |
| */ |
| public void setAnalyerManager(AnalyzerManager analyzerM) { |
| this.analyzerM = analyzerM; |
| } |
| |
| } |