1 11 package org.eclipse.help.internal.search; 12 13 import java.io.*; 14 import com.ibm.icu.text.BreakIterator; 15 import java.util.Locale ; 16 import java.util.StringTokenizer ; 17 18 import org.apache.lucene.analysis.*; 19 import org.eclipse.core.runtime.*; 20 import org.eclipse.help.internal.base.*; 21 22 26 public class DefaultAnalyzer extends Analyzer { 27 30 private Locale locale; 31 32 public DefaultAnalyzer(String localeString) { 33 super(); 34 Locale userLocale = getLocale(localeString); 36 37 Locale [] 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 Locale countryLocale = new Locale (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 Locale language = new Locale (userLocale.getLanguage(), ""); 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 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 " + localeString 74 + ", or Java Virtual Machine needs to be upgraded to version with proper support for locale {0}.", null); 76 locale = new Locale ("en", "US"); } 78 } 79 80 84 public final TokenStream tokenStream(String fieldName, Reader reader) { 85 return new LowerCaseFilter(new WordTokenStream(fieldName, reader, 86 locale)); 87 } 88 89 92 private Locale getLocale(String clientLocale) { 93 if (clientLocale == null) 94 clientLocale = Platform.getNL(); 95 if (clientLocale == null) 96 clientLocale = Locale.getDefault().toString(); 97 98 StringTokenizer locales = new StringTokenizer (clientLocale, "_"); if (locales.countTokens() == 1) 101 return new Locale (locales.nextToken(), ""); else if (locales.countTokens() == 2) 103 return new Locale (locales.nextToken(), locales.nextToken()); 104 else if (locales.countTokens() == 3) 105 return new Locale (locales.nextToken(), locales.nextToken(), locales 106 .nextToken()); 107 else 108 return Locale.getDefault(); 109 } 110 } 111 | Popular Tags |