KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > index > IndexManager


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 Intel 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  * Intel Corporation - initial API and implementation
10  * IBM Corporation - 122967 [Help] Remote help system
11  * 163558 Dynamic content support for all UA
12  * 165168 [Help] Better control of how help content is arranged and ordered
13  *******************************************************************************/

14 package org.eclipse.help.internal.index;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Arrays JavaDoc;
18 import java.util.HashMap JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.ListIterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
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 JavaDoc EXTENSION_POINT_ID_INDEX = HelpPlugin.PLUGIN_ID + ".index"; //$NON-NLS-1$
41
private static final String JavaDoc ELEMENT_NAME_INDEX_PROVIDER = "indexProvider"; //$NON-NLS-1$
42
private static final String JavaDoc ATTRIBUTE_NAME_CLASS = "class"; //$NON-NLS-1$
43

44     private Map JavaDoc indexContributionsByLocale = new HashMap JavaDoc();
45     private Map JavaDoc indexesByLocale = new HashMap JavaDoc();
46     private AbstractIndexProvider[] indexProviders;
47     
48     public synchronized IIndex getIndex(String JavaDoc locale) {
49         Index index = (Index)indexesByLocale.get(locale);
50         if (index == null) {
51             List JavaDoc contributions = new ArrayList JavaDoc(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     /*
61      * Returns all index contributions for the given locale, from all
62      * providers.
63      */

64     public synchronized IndexContribution[] getIndexContributions(String JavaDoc locale) {
65         IndexContribution[] cached = (IndexContribution[])indexContributionsByLocale.get(locale);
66         if (cached == null) {
67             List JavaDoc contributions = new ArrayList JavaDoc();
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 JavaDoc t) {
75                     // log, and skip the offending provider
76
String JavaDoc msg = "Error getting help keyword index data from provider: " + providers[i].getClass().getName() + " (skipping provider)"; //$NON-NLS-1$ //$NON-NLS-2$
77
HelpPlugin.logError(msg, t);
78                     continue;
79                 }
80                 
81                 // check for nulls and root element
82
for (int j=0;j<contrib.length;++j) {
83                     if (contrib[j] == null) {
84                         String JavaDoc msg = "Help keyword index provider \"" + providers[i].getClass().getName() + "\" returned a null contribution (skipping)"; //$NON-NLS-1$ //$NON-NLS-2$
85
HelpPlugin.logError(msg);
86                     }
87                     else if (contrib[j].getIndex() == null) {
88                         String JavaDoc msg = "Help keyword index provider \"" + providers[i].getClass().getName() + "\" returned a contribution with a null root element (expected a \"" + Index.NAME + "\" element; skipping)"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
89
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     /*
108      * Clears all cached contributions, forcing the manager to query the
109      * providers again next time a request is made.
110      */

111     public void clearCache() {
112         indexContributionsByLocale.clear();
113         indexesByLocale.clear();
114     }
115
116     /*
117      * Internal hook for unit testing.
118      */

119     public AbstractIndexProvider[] getIndexProviders() {
120         if (indexProviders == null) {
121             List JavaDoc providers = new ArrayList JavaDoc();
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                         // log and skip
133
String JavaDoc msg = "Error instantiating help keyword index provider class \"" + elem.getAttribute(ATTRIBUTE_NAME_CLASS) + '"'; //$NON-NLS-1$
134
HelpPlugin.logError(msg, e);
135                     }
136                 }
137             }
138             indexProviders = (AbstractIndexProvider[])providers.toArray(new AbstractIndexProvider[providers.size()]);
139         }
140         return indexProviders;
141     }
142     
143     /*
144      * Returns whether or not the index has been completely loaded for the
145      * given locale yet or not.
146      */

147     public boolean isIndexLoaded(String JavaDoc locale) {
148         return indexesByLocale.get(locale) != null;
149     }
150
151     /*
152      * Internal hook for unit testing.
153      */

154     public void setIndexProviders(AbstractIndexProvider[] indexProviders) {
155         this.indexProviders = indexProviders;
156     }
157
158     /*
159      * Filters the given contributions according to product preferences. If
160      * either the contribution's id or its category's id is listed in the
161      * ignoredIndexes, filter the contribution.
162      */

163     private void filterIndexContributions(List JavaDoc unfiltered) {
164         Set JavaDoc indexesToFilter = getIgnoredIndexContributions();
165         ListIterator JavaDoc 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 JavaDoc getIgnoredIndexContributions() {
175         HelpData helpData = HelpData.getProductHelpData();
176         if (helpData != null) {
177             return helpData.getHiddenIndexes();
178         }
179         else {
180             HashSet JavaDoc ignored = new HashSet JavaDoc();
181             Preferences pref = HelpPlugin.getDefault().getPluginPreferences();
182             String JavaDoc preferredIndexes = pref.getString(HelpPlugin.IGNORED_INDEXES_KEY);
183             if (preferredIndexes.length() > 0) {
184                 StringTokenizer JavaDoc suggestdOrderedInfosets = new StringTokenizer JavaDoc(preferredIndexes, " ;,"); //$NON-NLS-1$
185
while (suggestdOrderedInfosets.hasMoreTokens()) {
186                     ignored.add(suggestdOrderedInfosets.nextToken());
187                 }
188             }
189             return ignored;
190         }
191     }
192 }
193
Popular Tags