1 14 package org.eclipse.help.internal.index; 15 16 import java.util.ArrayList ; 17 import java.util.Arrays ; 18 import java.util.HashMap ; 19 import java.util.HashSet ; 20 import java.util.List ; 21 import java.util.ListIterator ; 22 import java.util.Map ; 23 import java.util.Set ; 24 import java.util.StringTokenizer ; 25 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IConfigurationElement; 28 import org.eclipse.core.runtime.IExtensionRegistry; 29 import org.eclipse.core.runtime.Platform; 30 import org.eclipse.core.runtime.Preferences; 31 import org.eclipse.help.AbstractIndexProvider; 32 import org.eclipse.help.IIndex; 33 import org.eclipse.help.IIndexContribution; 34 import org.eclipse.help.internal.HelpData; 35 import org.eclipse.help.internal.HelpPlugin; 36 import org.eclipse.help.internal.UAElementFactory; 37 38 public class IndexManager { 39 40 private static final String EXTENSION_POINT_ID_INDEX = HelpPlugin.PLUGIN_ID + ".index"; private static final String ELEMENT_NAME_INDEX_PROVIDER = "indexProvider"; private static final String ATTRIBUTE_NAME_CLASS = "class"; 44 private Map indexContributionsByLocale = new HashMap (); 45 private Map indexesByLocale = new HashMap (); 46 private AbstractIndexProvider[] indexProviders; 47 48 public synchronized IIndex getIndex(String locale) { 49 Index index = (Index)indexesByLocale.get(locale); 50 if (index == null) { 51 List contributions = new ArrayList (Arrays.asList(getIndexContributions(locale))); 52 filterIndexContributions(contributions); 53 IndexAssembler assembler = new IndexAssembler(); 54 index = assembler.assemble(contributions, locale); 55 indexesByLocale.put(locale, index); 56 } 57 return index; 58 } 59 60 64 public synchronized IndexContribution[] getIndexContributions(String locale) { 65 IndexContribution[] cached = (IndexContribution[])indexContributionsByLocale.get(locale); 66 if (cached == null) { 67 List contributions = new ArrayList (); 68 AbstractIndexProvider[] providers = getIndexProviders(); 69 for (int i=0;i<providers.length;++i) { 70 IIndexContribution[] contrib; 71 try { 72 contrib = providers[i].getIndexContributions(locale); 73 } 74 catch (Throwable t) { 75 String msg = "Error getting help keyword index data from provider: " + providers[i].getClass().getName() + " (skipping provider)"; HelpPlugin.logError(msg, t); 78 continue; 79 } 80 81 for (int j=0;j<contrib.length;++j) { 83 if (contrib[j] == null) { 84 String msg = "Help keyword index provider \"" + providers[i].getClass().getName() + "\" returned a null contribution (skipping)"; HelpPlugin.logError(msg); 86 } 87 else if (contrib[j].getIndex() == null) { 88 String msg = "Help keyword index provider \"" + providers[i].getClass().getName() + "\" returned a contribution with a null root element (expected a \"" + Index.NAME + "\" element; skipping)"; HelpPlugin.logError(msg); 90 } 91 else { 92 IndexContribution contribution = new IndexContribution(); 93 contribution.setId(contrib[j].getId()); 94 contribution.setLocale(contrib[j].getLocale()); 95 IIndex index = contrib[j].getIndex(); 96 contribution.setIndex(index instanceof Index ? (Index)index : (Index)UAElementFactory.newElement(index)); 97 contributions.add(contribution); 98 } 99 } 100 } 101 cached = (IndexContribution[])contributions.toArray(new IndexContribution[contributions.size()]); 102 indexContributionsByLocale.put(locale, cached); 103 } 104 return cached; 105 } 106 107 111 public void clearCache() { 112 indexContributionsByLocale.clear(); 113 indexesByLocale.clear(); 114 } 115 116 119 public AbstractIndexProvider[] getIndexProviders() { 120 if (indexProviders == null) { 121 List providers = new ArrayList (); 122 IExtensionRegistry registry = Platform.getExtensionRegistry(); 123 IConfigurationElement[] elements = registry.getConfigurationElementsFor(EXTENSION_POINT_ID_INDEX); 124 for (int i=0;i<elements.length;++i) { 125 IConfigurationElement elem = elements[i]; 126 if (elem.getName().equals(ELEMENT_NAME_INDEX_PROVIDER)) { 127 try { 128 AbstractIndexProvider provider = (AbstractIndexProvider)elem.createExecutableExtension(ATTRIBUTE_NAME_CLASS); 129 providers.add(provider); 130 } 131 catch (CoreException e) { 132 String msg = "Error instantiating help keyword index provider class \"" + elem.getAttribute(ATTRIBUTE_NAME_CLASS) + '"'; HelpPlugin.logError(msg, e); 135 } 136 } 137 } 138 indexProviders = (AbstractIndexProvider[])providers.toArray(new AbstractIndexProvider[providers.size()]); 139 } 140 return indexProviders; 141 } 142 143 147 public boolean isIndexLoaded(String locale) { 148 return indexesByLocale.get(locale) != null; 149 } 150 151 154 public void setIndexProviders(AbstractIndexProvider[] indexProviders) { 155 this.indexProviders = indexProviders; 156 } 157 158 163 private void filterIndexContributions(List unfiltered) { 164 Set indexesToFilter = getIgnoredIndexContributions(); 165 ListIterator iter = unfiltered.listIterator(); 166 while (iter.hasNext()) { 167 IIndexContribution contribution = (IIndexContribution)iter.next(); 168 if (indexesToFilter.contains(contribution.getId())) { 169 iter.remove(); 170 } 171 } 172 } 173 174 private Set getIgnoredIndexContributions() { 175 HelpData helpData = HelpData.getProductHelpData(); 176 if (helpData != null) { 177 return helpData.getHiddenIndexes(); 178 } 179 else { 180 HashSet ignored = new HashSet (); 181 Preferences pref = HelpPlugin.getDefault().getPluginPreferences(); 182 String preferredIndexes = pref.getString(HelpPlugin.IGNORED_INDEXES_KEY); 183 if (preferredIndexes.length() > 0) { 184 StringTokenizer suggestdOrderedInfosets = new StringTokenizer (preferredIndexes, " ;,"); while (suggestdOrderedInfosets.hasMoreTokens()) { 186 ignored.add(suggestdOrderedInfosets.nextToken()); 187 } 188 } 189 return ignored; 190 } 191 } 192 } 193 | Popular Tags |