KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.*;
14 import com.ibm.icu.text.BreakIterator;
15 import java.util.Locale JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17
18 import org.apache.lucene.analysis.*;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.help.internal.base.*;
21
22 /**
23  * Lucene Analyzer. LowerCaseTokenizer->WordTokenStream (uses word breaking in
24  * java.text)
25  */

26 public class DefaultAnalyzer extends Analyzer {
27     /**
28      * Constructor for Analyzer.
29      */

30     private Locale JavaDoc locale;
31
32     public DefaultAnalyzer(String JavaDoc localeString) {
33         super();
34         // Create a locale object for a given locale string
35
Locale JavaDoc userLocale = getLocale(localeString);
36
37         // Check if the locale is supported by BreakIterator
38
// check here to do it only once.
39
Locale JavaDoc[] availableLocales = BreakIterator.getAvailableLocales();
40         for (int i = 0; i < availableLocales.length; i++) {
41             if (userLocale.equals(availableLocales[i])) {
42                 locale = userLocale;
43                 break;
44             }
45         }
46         if (locale == null && userLocale.getDisplayVariant().length() > 0) {
47             // Check if the locale without variant is supported by BreakIterator
48
Locale JavaDoc countryLocale = new Locale JavaDoc(userLocale.getLanguage(),
49                     userLocale.getCountry());
50             for (int i = 0; i < availableLocales.length; i++) {
51                 if (countryLocale.equals(availableLocales[i])) {
52                     locale = countryLocale;
53                     break;
54                 }
55             }
56         }
57         if (locale == null && userLocale.getCountry().length() > 0) {
58             // Check if at least the language is supported by BreakIterator
59
Locale JavaDoc language = new Locale JavaDoc(userLocale.getLanguage(), ""); //$NON-NLS-1$
60
for (int i = 0; i < availableLocales.length; i++) {
61                 if (language.equals(availableLocales[i])) {
62                     locale = language;
63                     break;
64                 }
65             }
66         }
67
68         if (locale == null) {
69             // Locale is not supported, will use en_US
70
HelpBasePlugin
71                     .logError(
72                             "Text Analyzer could not be created for locale {0}. An analyzer that extends org.eclipse.help.luceneAnalyzer extension point needs to be plugged in for locale " //$NON-NLS-1$
73
+ localeString
74                                     + ", or Java Virtual Machine needs to be upgraded to version with proper support for locale {0}.", //$NON-NLS-1$
75
null);
76             locale = new Locale JavaDoc("en", "US"); //$NON-NLS-1$ //$NON-NLS-2$
77
}
78     }
79
80     /**
81      * Creates a TokenStream which tokenizes all the text in the provided
82      * Reader.
83      */

84     public final TokenStream tokenStream(String JavaDoc fieldName, Reader reader) {
85         return new LowerCaseFilter(new WordTokenStream(fieldName, reader,
86                 locale));
87     }
88
89     /**
90      * Creates a Locale object out of a string representation
91      */

92     private Locale JavaDoc getLocale(String JavaDoc clientLocale) {
93         if (clientLocale == null)
94             clientLocale = Platform.getNL();
95         if (clientLocale == null)
96             clientLocale = Locale.getDefault().toString();
97
98         // break the string into tokens to get the Locale object
99
StringTokenizer JavaDoc locales = new StringTokenizer JavaDoc(clientLocale, "_"); //$NON-NLS-1$
100
if (locales.countTokens() == 1)
101             return new Locale JavaDoc(locales.nextToken(), ""); //$NON-NLS-1$
102
else if (locales.countTokens() == 2)
103             return new Locale JavaDoc(locales.nextToken(), locales.nextToken());
104         else if (locales.countTokens() == 3)
105             return new Locale JavaDoc(locales.nextToken(), locales.nextToken(), locales
106                     .nextToken());
107         else
108             return Locale.getDefault();
109     }
110 }
111
Popular Tags