1 /*******************************************************************************
2  * Copyright (c) 2006, 2020 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     George Suaridze <suag@1c.ru> (1C-Soft LLC) - Bug 560168
14  *******************************************************************************/
15 package org.eclipse.help.internal.index;
16 
17 import java.util.ArrayList;
18 import java.util.List;
19 
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IExtensionRegistry;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.help.AbstractIndexProvider;
24 import org.eclipse.help.IIndexContribution;
25 import org.eclipse.help.internal.HelpPlugin;
26 import org.eclipse.help.internal.util.ResourceLocator;
27 import org.xml.sax.SAXParseException;
28 
29 /*
30  * Provides index data from index XML files to the help system.
31  */
32 public class IndexFileProvider extends AbstractIndexProvider {
33 
34 	private static final String ERROR_READING_HELP_KEYWORD_INDEX_FILE = "Error reading help keyword index file /\""; //$NON-NLS-1$
35 	public static final String EXTENSION_POINT_ID_INDEX = HelpPlugin.PLUGIN_ID + ".index"; //$NON-NLS-1$
36 	public static final String ELEMENT_NAME_INDEX = "index"; //$NON-NLS-1$
37 	public static final String ATTRIBUTE_NAME_FILE = "file"; //$NON-NLS-1$
38 
39 	@Override
getIndexContributions(String locale)40 	public IIndexContribution[] getIndexContributions(String locale) {
41 		List<IIndexContribution> contributions = new ArrayList<>();
42 		IndexFile[] indexFiles = getIndexFiles(locale);
43 		IndexFileParser parser = new IndexFileParser();
44 		for (int i=0;i<indexFiles.length;++i) {
45 			IndexFile indexFile = indexFiles[i];
46 			try {
47 				IIndexContribution toc = parser.parse(indexFile);
48 				contributions.add(toc);
49 			}  catch (SAXParseException spe) {
50 				StringBuilder buffer = new StringBuilder(ERROR_READING_HELP_KEYWORD_INDEX_FILE);
51 				buffer.append(getIndexFilePath(indexFile));
52 				buffer.append("\" at line "); //$NON-NLS-1$
53 				buffer.append(spe.getLineNumber());
54 				buffer.append(". "); //$NON-NLS-1$
55 				buffer.append(spe.getMessage());
56 
57 				// Use the contained exception.
58 				Exception x = spe;
59 				if (spe.getException() != null)
60 					x = spe.getException();
61 				Platform.getLog(getClass()).error(buffer.toString(), x);
62 
63 			}
64 			catch (Throwable t) {
65 				String msg = ERROR_READING_HELP_KEYWORD_INDEX_FILE + getIndexFilePath(indexFile) + "\" (skipping file)"; //$NON-NLS-1$
66 				Platform.getLog(getClass()).error(msg, t);
67 			}
68 		}
69 		return contributions.toArray(new IIndexContribution[contributions.size()]);
70 	}
71 
getIndexFilePath(IndexFile indexFile)72 	private String getIndexFilePath(IndexFile indexFile) {
73 		String pluginId = indexFile.getPluginId();
74 		String file = indexFile.getFile();
75 		return ResourceLocator.getErrorPath(pluginId, file, indexFile.getLocale());
76 	}
77 
78 	/*
79 	 * Returns all available IndexFiles for the given locale.
80 	 */
getIndexFiles(String locale)81 	private IndexFile[] getIndexFiles(String locale) {
82 		List<IndexFile> indexFiles = new ArrayList<>();
83 		IExtensionRegistry registry = Platform.getExtensionRegistry();
84 		IConfigurationElement[] elements = registry.getConfigurationElementsFor(EXTENSION_POINT_ID_INDEX);
85 		for (int i=0;i<elements.length;++i) {
86 			IConfigurationElement elem = elements[i];
87 			String pluginId = elem.getContributor().getName();
88 			if (elem.getName().equals(ELEMENT_NAME_INDEX)) {
89 				String file = elem.getAttribute(ATTRIBUTE_NAME_FILE);
90 				IndexFile indexFile = new IndexFile(pluginId, file, locale);
91 				indexFiles.add(indexFile);
92 			}
93 		}
94 		return indexFiles.toArray(new IndexFile[indexFiles.size()]);
95 	}
96 }
97