001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.cocoon.components.search.analyzer;
017:
018: import org.apache.avalon.framework.configuration.Configuration;
019: import org.apache.avalon.framework.configuration.ConfigurationException;
020: import org.apache.lucene.analysis.Analyzer;
021: import org.apache.lucene.analysis.PerFieldAnalyzerWrapper;
022:
023: /**
024: * Configurable PerFieldAnalyzerWrapper. Allow one analyzer per field for
025: * indexing a document (useful for multilanguage document)
026: *
027: * (@link org.apache.lucene.analysis.PerFieldAnalyzerWrapper class)
028: *
029: *
030: * A config file for this analyzer is:
031: *
032: * <!-- if a lucene document containing a field not present in the "field" tags,
033: * the defaultAnalyzer would be used --> <config defaultAnalyzer="analyzerEN">
034: * <fields><!-- if a lucene document contains the field "summury" , the
035: * analyzer "analyzerEN" would be used --> <field name="summury"
036: * analyzer="analyzerEN"/> <field name="desc_fr" analyzer="analyzerFR"/> <field
037: * name="desc_en" analyzer="analyzerEN"/> <field name="desc_de"
038: * analyzer="analyzerDE"/> </fields> </config>
039: *
040: * @author Nicolas Maisonneuve
041: */
042: public class ConfigurablePerFieldAnalyzer extends ConfigurableAnalyzer {
043:
044: public static final String CONFIG_DEFAULTANALYZER_ATTRIBUTE = "defaultAnalyzer";
045:
046: public static final String FIELDS_ELEMENT = "fields";
047:
048: public static final String FIELD_ELEMENT = "field";
049:
050: public static final String FIELD_NAME_ATTRIBUTE = "name";
051:
052: public static final String FIELD_ANALYZERID_ATTRIBUTE = "analyzer";
053:
054: /*
055: * (non-Javadoc)
056: *
057: * @see org.apache.cocoon.components.search.analyzer.ConfigurableAnalyzer#configure(org.apache.avalon.framework.configuration.Configuration)
058: */
059: public void configure(Configuration configuration)
060: throws ConfigurationException {
061:
062: String analyzerid = configuration
063: .getAttribute(CONFIG_DEFAULTANALYZER_ATTRIBUTE);
064:
065: Analyzer analyzer = analyzerM.getAnalyzer(analyzerid);
066: if (analyzer == null) {
067: throw new ConfigurationException("analyzer " + analyzerid
068: + " doesn't exist");
069: }
070:
071: PerFieldAnalyzerWrapper tmpanalyzer = new PerFieldAnalyzerWrapper(
072: analyzer);
073: Configuration[] conffield = configuration.getChild(
074: FIELDS_ELEMENT).getChildren(FIELD_ELEMENT);
075:
076: for (int i = 0; i < conffield.length; i++) {
077:
078: String fieldname = conffield[i]
079: .getAttribute(FIELD_NAME_ATTRIBUTE);
080: analyzerid = conffield[i]
081: .getAttribute(FIELD_ANALYZERID_ATTRIBUTE);
082:
083: if (fieldname == null || fieldname.equals("")) {
084: throw new ConfigurationException("element "
085: + FIELD_ELEMENT + " must have the "
086: + FIELD_NAME_ATTRIBUTE + " attribute");
087: }
088: if (analyzerid == null || analyzerid.equals("")) {
089: throw new ConfigurationException("element "
090: + FIELD_ELEMENT + " must have the "
091: + FIELD_ANALYZERID_ATTRIBUTE + " attribute");
092: }
093:
094: analyzer = analyzerM.getAnalyzer(analyzerid);
095:
096: if (analyzer == null) {
097: throw new ConfigurationException("analyzer "
098: + analyzerid + " doesn't exist");
099: }
100: tmpanalyzer.addAnalyzer(fieldname, analyzer);
101: }
102: this.analyzer = tmpanalyzer;
103: }
104:
105: }
|