Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4f6c3b5699b11df9e73c238c049eb6c0a550aa6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*******************************************************************************
 * Copyright (c) 2005 Intel Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Intel Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.help.internal.index;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.StringTokenizer;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.help.IIndex;
import org.eclipse.help.internal.HelpPlugin;


/**
 * @author sturmash
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class IndexManager {
	public static final String INDEX_XP_NAME = "index"; //$NON-NLS-1$

    private Collection contributingPlugins;
    private Map indexesByLang;

    /**
     * IndexManager constructor
     */
    public IndexManager() {
        indexesByLang = new HashMap();
    }
    
    /**
     * Builds the index from all contributed index files
     * @param locale
     */
    private void build(String locale) {
        Collection contributedIndexFiles = getContributedIndexFiles(locale);
        IndexBuilder builder = new IndexBuilder();
        builder.build(contributedIndexFiles);
        IIndex index = builder.getBuiltIndex();
        indexesByLang.put(locale, index);
    }
    
    /**
     * Returns all index files contributed by separate help plugins
     * @param locale
     * @return
     */
    private Collection getContributedIndexFiles(String locale) {
        contributingPlugins = new HashSet();
        Collection contributedIndexFiles = new ArrayList();
        Collection ignored = getIgnoredIndexes();

        IExtensionPoint xpt = Platform.getExtensionRegistry().getExtensionPoint(HelpPlugin.PLUGIN_ID, INDEX_XP_NAME);
        if (xpt == null)
             return contributedIndexFiles;
        
        IExtension[] extensions = xpt.getExtensions();
        for (int i = 0; i < extensions.length; i++) {
            contributingPlugins.add(extensions[i].getContributor().getName());
            IConfigurationElement[] configElements = extensions[i].getConfigurationElements();
            for (int j = 0; j < configElements.length; j++) {
                if (configElements[j].getName().equals("index")) { //$NON-NLS-1$
                    String pluginId = configElements[j].getDeclaringExtension().getContributor().getName();
                    String href = configElements[j].getAttribute("file"); //$NON-NLS-1$
                    if (href == null
                    		|| ignored.contains("/" + pluginId + "/" + href)) { //$NON-NLS-1$ //$NON-NLS-2$)
                    	continue;
                    }
                    contributedIndexFiles.add(new IndexFile(pluginId, href, locale));
                }
            }
        }
        return contributedIndexFiles;
    }
    
    /**
     * @return Returns the contributingPlugins.
     */
    public Collection getContributingPlugins() {
        return contributingPlugins;
    }
    
    public Index getIndex(String locale) {
        if (locale == null)
            return new Index();

        Index index = (Index) indexesByLang.get(locale);
        if (index == null) {
            synchronized(this) {
                if (index == null) {
                    build(locale);
                }
            }
            index = (Index) indexesByLang.get(locale);
            if (index == null)
                index = new Index();
        }

        return index;
            
    }

    private Collection getIgnoredIndexes() {
    	HashSet ignored = new HashSet();
		try {
			Preferences pref = HelpPlugin.getDefault().getPluginPreferences();
			String ignoredIndexes = pref.getString(HelpPlugin.IGNORED_INDEXES_KEY);
			if (ignoredIndexes != null) {
				StringTokenizer tokens = new StringTokenizer(
						ignoredIndexes, " ;,"); //$NON-NLS-1$

				while (tokens.hasMoreTokens()) {
					ignored.add(tokens.nextToken());
				}
			}
		} catch (Exception e) {
			HelpPlugin.logError(
					"Problems occurred reading plug-in preferences.", e); //$NON-NLS-1$
		}
    	return ignored;
    }
}

Back to the top