KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > impl > lucene > LuceneAnalyser


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.search.impl.lucene;
18
19 import java.io.Reader JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.alfresco.repo.search.impl.lucene.analysis.PathAnalyser;
24 import org.alfresco.service.cmr.dictionary.DictionaryService;
25 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
26 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
27 import org.alfresco.service.namespace.QName;
28 import org.apache.lucene.analysis.Analyzer;
29 import org.apache.lucene.analysis.TokenStream;
30 import org.apache.lucene.analysis.WhitespaceAnalyzer;
31 import org.apache.lucene.analysis.standard.StandardAnalyzer;
32
33 /**
34  * Analyse properties according to the property definition.
35  *
36  * The default is to use the standard tokeniser. The tokeniser should not have
37  * been called when indexeing properties that require no tokenisation. (tokenise
38  * should be set to false when adding the field to the document)
39  *
40  * @author andyh
41  *
42  */

43
44 public class LuceneAnalyser extends Analyzer
45 {
46
47     private DictionaryService dictionaryService;
48
49     private Analyzer defaultAnalyser;
50
51     private Map JavaDoc<String JavaDoc, Analyzer> analysers = new HashMap JavaDoc<String JavaDoc, Analyzer>();
52
53     /**
54      * Constructs with a default standard analyser
55      *
56      * @param defaultAnalyzer
57      * Any fields not specifically defined to use a different
58      * analyzer will use the one provided here.
59      */

60     public LuceneAnalyser(DictionaryService dictionaryService)
61     {
62         this(new StandardAnalyzer());
63         this.dictionaryService = dictionaryService;
64     }
65
66     /**
67      * Constructs with default analyzer.
68      *
69      * @param defaultAnalyzer
70      * Any fields not specifically defined to use a different
71      * analyzer will use the one provided here.
72      */

73     public LuceneAnalyser(Analyzer defaultAnalyser)
74     {
75         this.defaultAnalyser = defaultAnalyser;
76     }
77
78     public TokenStream tokenStream(String JavaDoc fieldName, Reader JavaDoc reader)
79     {
80         Analyzer analyser = (Analyzer) analysers.get(fieldName);
81         if (analyser == null)
82         {
83             analyser = findAnalyser(fieldName);
84         }
85         return analyser.tokenStream(fieldName, reader);
86     }
87
88     private Analyzer findAnalyser(String JavaDoc fieldName)
89     {
90         Analyzer analyser;
91         if (fieldName.equals("PATH"))
92         {
93             analyser = new PathAnalyser();
94         }
95         else if (fieldName.equals("QNAME"))
96         {
97             analyser = new PathAnalyser();
98         }
99         else if (fieldName.equals("TYPE"))
100         {
101             throw new UnsupportedOperationException JavaDoc("TYPE must not be tokenised");
102         }
103         else if (fieldName.equals("ASPECT"))
104         {
105             throw new UnsupportedOperationException JavaDoc("ASPECT must not be tokenised");
106         }
107         else if (fieldName.equals("ANCESTOR"))
108         {
109             analyser = new WhitespaceAnalyzer();
110         }
111         else if (fieldName.equals("TEXT"))
112         {
113             DataTypeDefinition dataType = dictionaryService.getDataType(DataTypeDefinition.TEXT);
114             analyser = loadAnalyzer(dataType);
115         }
116         else if (fieldName.startsWith("@"))
117         {
118             QName propertyQName = QName.createQName(fieldName.substring(1));
119             PropertyDefinition propertyDef = dictionaryService.getProperty(propertyQName);
120             DataTypeDefinition dataType = (propertyDef == null) ? dictionaryService.getDataType(DataTypeDefinition.TEXT) : propertyDef.getDataType();
121             analyser = loadAnalyzer(dataType);
122         }
123         else
124         {
125             analyser = defaultAnalyser;
126         }
127         analysers.put(fieldName, analyser);
128         return analyser;
129     }
130     
131     private Analyzer loadAnalyzer(DataTypeDefinition dataType)
132     {
133         String JavaDoc analyserClassName = dataType.getAnalyserClassName();
134         try
135         {
136             Class JavaDoc<?> clazz = Class.forName(analyserClassName);
137             Analyzer analyser = (Analyzer)clazz.newInstance();
138             return analyser;
139         }
140         catch (ClassNotFoundException JavaDoc e)
141         {
142             throw new RuntimeException JavaDoc("Unable to load analyser for property of type " + dataType.getName() + " using " + analyserClassName);
143         }
144         catch (InstantiationException JavaDoc e)
145         {
146             throw new RuntimeException JavaDoc("Unable to load analyser for property of type " + dataType.getName() + " using " + analyserClassName);
147         }
148         catch (IllegalAccessException JavaDoc e)
149         {
150             throw new RuntimeException JavaDoc("Unable to load analyser for property of type " + dataType.getName() + " using " + analyserClassName);
151         }
152     }
153     
154 }
155
Popular Tags