|
16 | 16 | *******************************************************************************/ |
17 | 17 | package org.eclipse.help.internal.search; |
18 | 18 |
|
19 | | -import java.text.BreakIterator; |
20 | | -import java.util.Locale; |
21 | | -import java.util.StringTokenizer; |
22 | | - |
23 | 19 | import org.apache.lucene.analysis.Analyzer; |
24 | 20 | import org.apache.lucene.analysis.Tokenizer; |
25 | 21 | import org.apache.lucene.analysis.core.LowerCaseFilter; |
26 | 22 | import org.apache.lucene.analysis.standard.StandardTokenizer; |
27 | | -import org.eclipse.core.runtime.ILog; |
28 | | -import org.eclipse.core.runtime.Platform; |
29 | 23 |
|
30 | 24 | /** |
31 | 25 | * Lucene Analyzer. LowerCaseFilter->StandardTokenizer |
32 | 26 | */ |
33 | 27 | public final class DefaultAnalyzer extends Analyzer { |
34 | 28 |
|
35 | | - private Locale locale; |
36 | | - |
37 | | - /** |
38 | | - * Creates a new analyzer using the given locale. |
39 | | - */ |
40 | | - public DefaultAnalyzer(String localeString) { |
41 | | - super(); |
42 | | - // Create a locale object for a given locale string |
43 | | - Locale userLocale = getLocale(localeString); |
44 | | - |
45 | | - // Check if the locale is supported by BreakIterator |
46 | | - // check here to do it only once. |
47 | | - Locale[] availableLocales = BreakIterator.getAvailableLocales(); |
48 | | - for (Locale availableLocale : availableLocales) { |
49 | | - if (userLocale.equals(availableLocale)) { |
50 | | - locale = userLocale; |
51 | | - break; |
52 | | - } |
53 | | - } |
54 | | - if (locale == null && userLocale.getDisplayVariant().length() > 0) { |
55 | | - // Check if the locale without variant is supported by BreakIterator |
56 | | - Locale countryLocale = Locale.of(userLocale.getLanguage(), userLocale.getCountry()); |
57 | | - for (Locale availableLocale : availableLocales) { |
58 | | - if (countryLocale.equals(availableLocale)) { |
59 | | - locale = countryLocale; |
60 | | - break; |
61 | | - } |
62 | | - } |
63 | | - } |
64 | | - if (locale == null && userLocale.getCountry().length() > 0) { |
65 | | - // Check if at least the language is supported by BreakIterator |
66 | | - Locale language = Locale.of(userLocale.getLanguage(), ""); //$NON-NLS-1$ |
67 | | - for (Locale availableLocale : availableLocales) { |
68 | | - if (language.equals(availableLocale)) { |
69 | | - locale = language; |
70 | | - break; |
71 | | - } |
72 | | - } |
73 | | - } |
74 | | - |
75 | | - if (locale == null) { |
76 | | - // Locale is not supported, will use en_US |
77 | | - ILog.of(getClass()).error( |
78 | | - "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$ |
79 | | - + localeString |
80 | | - + ", or Java Virtual Machine needs to be upgraded to version with proper support for locale {0}.", //$NON-NLS-1$ |
81 | | - null); |
82 | | - locale = Locale.of("en", "US"); //$NON-NLS-1$ //$NON-NLS-2$ |
83 | | - } |
84 | | - } |
85 | | - |
86 | | - /** |
87 | | - * Creates a Locale object out of a string representation |
88 | | - */ |
89 | | - private Locale getLocale(String clientLocale) { |
90 | | - if (clientLocale == null) |
91 | | - clientLocale = Platform.getNL(); |
92 | | - if (clientLocale == null) |
93 | | - clientLocale = Locale.getDefault().toString(); |
94 | | - |
95 | | - // break the string into tokens to get the Locale object |
96 | | - StringTokenizer locales = new StringTokenizer(clientLocale, "_"); //$NON-NLS-1$ |
97 | | - if (locales.countTokens() == 1) |
98 | | - return Locale.of(locales.nextToken(), ""); //$NON-NLS-1$ |
99 | | - else if (locales.countTokens() == 2) |
100 | | - return Locale.of(locales.nextToken(), locales.nextToken()); |
101 | | - else if (locales.countTokens() == 3) |
102 | | - return Locale.of(locales.nextToken(), locales.nextToken(), locales.nextToken()); |
103 | | - else |
104 | | - return Locale.getDefault(); |
105 | | - } |
106 | | - |
107 | 29 | /* |
108 | 30 | * Can't use try-with-resources because the Lucene internally reuses |
109 | 31 | * components. See {@link org.apache.lucene.analysis.Analyzer.ReuseStrategy} |
|
0 commit comments