KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > search > AnalyzerDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.search;
12
13 import org.apache.lucene.analysis.*;
14 import org.eclipse.core.runtime.*;
15 import org.eclipse.help.internal.base.*;
16 import org.osgi.framework.*;
17
18 /**
19  * Text Analyzer Descriptor. Encapsulates Lucene Analyzer
20  */

21 public class AnalyzerDescriptor {
22     private Analyzer luceneAnalyzer;
23
24     private String JavaDoc id;
25
26     private String JavaDoc lang;
27
28     /**
29      * Constructor
30      */

31     public AnalyzerDescriptor(String JavaDoc locale) {
32
33         // try creating the analyzer for the specified locale (usually
34
// lang_country)
35
this.luceneAnalyzer = createAnalyzer(locale);
36
37         // try creating configured analyzer for the language only
38
if (this.luceneAnalyzer == null) {
39             String JavaDoc language = null;
40             if (locale.length() > 2) {
41                 language = locale.substring(0, 2);
42                 this.luceneAnalyzer = createAnalyzer(language);
43             }
44         }
45
46         // if all fails, create default analyzer
47
if (this.luceneAnalyzer == null) {
48             this.id = HelpBasePlugin.PLUGIN_ID
49                     + "#" //$NON-NLS-1$
50
+ HelpBasePlugin.getDefault().getBundle().getHeaders().get(
51                             Constants.BUNDLE_VERSION) + "?locale=" + locale; //$NON-NLS-1$
52
this.luceneAnalyzer = new DefaultAnalyzer(locale);
53             this.lang = locale;
54         }
55     }
56
57     /**
58      * Gets the analyzer.
59      *
60      * @return Returns a Analyzer
61      */

62     public Analyzer getAnalyzer() {
63         return new SmartAnalyzer(lang, luceneAnalyzer);
64     }
65
66     /**
67      * Gets the id.
68      *
69      * @return Returns a String
70      */

71     public String JavaDoc getId() {
72         return id;
73     }
74
75     /**
76      * Gets the language for the analyzer
77      *
78      * @return Returns a String
79      */

80     public String JavaDoc getLang() {
81         return lang;
82     }
83
84     /**
85      * Creates analyzer for a locale, if it is configured in the
86      * org.eclipse.help.luceneAnalyzer extension point. The identifier of the
87      * analyzer and locale and lang are also set.
88      *
89      * @return Analyzer or null if no analyzer is configured for given locale.
90      */

91     private Analyzer createAnalyzer(String JavaDoc locale) {
92         // find extension point
93
IConfigurationElement configElements[] = Platform
94                 .getExtensionRegistry().getConfigurationElementsFor(
95                         HelpBasePlugin.PLUGIN_ID, "luceneAnalyzer"); //$NON-NLS-1$
96
for (int i = 0; i < configElements.length; i++) {
97             if (!configElements[i].getName().equals("analyzer")) //$NON-NLS-1$
98
continue;
99             String JavaDoc analyzerLocale = configElements[i].getAttribute("locale"); //$NON-NLS-1$
100
if (analyzerLocale == null || !analyzerLocale.equals(locale))
101                 continue;
102             try {
103                 Object JavaDoc analyzer = configElements[i]
104                         .createExecutableExtension("class"); //$NON-NLS-1$
105
if (!(analyzer instanceof Analyzer))
106                     continue;
107                 String JavaDoc pluginId = configElements[i].getContributor().getName();
108                 String JavaDoc pluginVersion = (String JavaDoc) Platform
109                         .getBundle(pluginId).getHeaders().get(
110                                 Constants.BUNDLE_VERSION);
111                 this.luceneAnalyzer = (Analyzer) analyzer;
112                 this.id = pluginId + "#" + pluginVersion + "?locale=" + locale; //$NON-NLS-1$ //$NON-NLS-2$
113
this.lang = locale;
114                 if (HelpBasePlugin.PLUGIN_ID.equals(pluginId)) {
115                     // The analyzer is contributed by help plugin.
116
// Continue in case there is another analyzer for the
117
// same locale
118
// let another analyzer take precendence over one from
119
// help
120
} else {
121                     // the analyzer does not come from help
122
return this.luceneAnalyzer;
123                 }
124             } catch (CoreException ce) {
125                 HelpBasePlugin.logError(
126                         "Exception occurred creating text analyzer " //$NON-NLS-1$
127
+ configElements[i].getAttribute("class") //$NON-NLS-1$
128
+ " for " + locale + " locale.", ce); //$NON-NLS-1$ //$NON-NLS-2$
129
}
130         }
131
132         return this.luceneAnalyzer;
133     }
134
135     /**
136      * Checks whether analyzer is compatible with a given analyzer. The ID has
137      * the form [plugin_id]#[plugin_version]?locale=[locale], for example:
138      * org.eclipse.help.base#3.1.0?locale=ru
139      *
140      * @param analyzerId
141      * id of analyzer used in the past by the index; id has a form:
142      * [plugin.id]#[version]?locale=[locale]
143      * @return true when it is known that given analyzer is compatible with this
144      * analyzer
145      */

146     public boolean isCompatible(String JavaDoc analyzerId) {
147         if (analyzerId != null) {
148             // parse the id
149
int numberSignIndex = analyzerId.indexOf('#');
150             int questionMarkIndex = analyzerId.indexOf('?', numberSignIndex);
151             String JavaDoc pluginId = analyzerId.substring(0, numberSignIndex);
152             String JavaDoc version = analyzerId.substring(numberSignIndex + 1, questionMarkIndex);
153             String JavaDoc locale = analyzerId.substring(questionMarkIndex + 1 + "locale=".length()); //$NON-NLS-1$
154

155             // plugin compatible?
156
// must both be org.eclipse.help.base
157
String JavaDoc thisPluginId = id.substring(0, id.indexOf('#'));
158             if (!HelpBasePlugin.PLUGIN_ID.equals(pluginId) || !HelpBasePlugin.PLUGIN_ID.equals(thisPluginId)) {
159                 return false;
160             }
161             
162             // version compatible?
163
// must both be >= 3.1
164
Version vA = getVersion(id);
165             Version vB = new Version(version);
166             Version v3_1 = new Version(3, 1, 0);
167             if (vA.compareTo(v3_1) < 0 && vB.compareTo(v3_1) < 0) {
168                 return false;
169             }
170             
171             // locale compatible?
172
// first part must be equal (first two chars)
173
if (!lang.substring(0, 2).equals(locale.substring(0, 2))) {
174                 return false;
175             }
176             return true;
177         }
178         return false;
179     }
180     
181     private Version getVersion(String JavaDoc id) {
182         int idStart = id.indexOf('#');
183         int idStop = id.indexOf('?');
184         String JavaDoc value = idStop== -1?id.substring(idStart+1):id.substring(idStart+1, idStop);
185         return new Version(value);
186     }
187 }
188
Popular Tags