1 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 21 public class AnalyzerDescriptor { 22 private Analyzer luceneAnalyzer; 23 24 private String id; 25 26 private String lang; 27 28 31 public AnalyzerDescriptor(String locale) { 32 33 this.luceneAnalyzer = createAnalyzer(locale); 36 37 if (this.luceneAnalyzer == null) { 39 String language = null; 40 if (locale.length() > 2) { 41 language = locale.substring(0, 2); 42 this.luceneAnalyzer = createAnalyzer(language); 43 } 44 } 45 46 if (this.luceneAnalyzer == null) { 48 this.id = HelpBasePlugin.PLUGIN_ID 49 + "#" + HelpBasePlugin.getDefault().getBundle().getHeaders().get( 51 Constants.BUNDLE_VERSION) + "?locale=" + locale; this.luceneAnalyzer = new DefaultAnalyzer(locale); 53 this.lang = locale; 54 } 55 } 56 57 62 public Analyzer getAnalyzer() { 63 return new SmartAnalyzer(lang, luceneAnalyzer); 64 } 65 66 71 public String getId() { 72 return id; 73 } 74 75 80 public String getLang() { 81 return lang; 82 } 83 84 91 private Analyzer createAnalyzer(String locale) { 92 IConfigurationElement configElements[] = Platform 94 .getExtensionRegistry().getConfigurationElementsFor( 95 HelpBasePlugin.PLUGIN_ID, "luceneAnalyzer"); for (int i = 0; i < configElements.length; i++) { 97 if (!configElements[i].getName().equals("analyzer")) continue; 99 String analyzerLocale = configElements[i].getAttribute("locale"); if (analyzerLocale == null || !analyzerLocale.equals(locale)) 101 continue; 102 try { 103 Object analyzer = configElements[i] 104 .createExecutableExtension("class"); if (!(analyzer instanceof Analyzer)) 106 continue; 107 String pluginId = configElements[i].getContributor().getName(); 108 String pluginVersion = (String ) Platform 109 .getBundle(pluginId).getHeaders().get( 110 Constants.BUNDLE_VERSION); 111 this.luceneAnalyzer = (Analyzer) analyzer; 112 this.id = pluginId + "#" + pluginVersion + "?locale=" + locale; this.lang = locale; 114 if (HelpBasePlugin.PLUGIN_ID.equals(pluginId)) { 115 } else { 121 return this.luceneAnalyzer; 123 } 124 } catch (CoreException ce) { 125 HelpBasePlugin.logError( 126 "Exception occurred creating text analyzer " + configElements[i].getAttribute("class") + " for " + locale + " locale.", ce); } 130 } 131 132 return this.luceneAnalyzer; 133 } 134 135 146 public boolean isCompatible(String analyzerId) { 147 if (analyzerId != null) { 148 int numberSignIndex = analyzerId.indexOf('#'); 150 int questionMarkIndex = analyzerId.indexOf('?', numberSignIndex); 151 String pluginId = analyzerId.substring(0, numberSignIndex); 152 String version = analyzerId.substring(numberSignIndex + 1, questionMarkIndex); 153 String locale = analyzerId.substring(questionMarkIndex + 1 + "locale=".length()); 155 String 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 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 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 id) { 182 int idStart = id.indexOf('#'); 183 int idStop = id.indexOf('?'); 184 String value = idStop== -1?id.substring(idStart+1):id.substring(idStart+1, idStop); 185 return new Version(value); 186 } 187 } 188 | Popular Tags |