KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > toc > TocFileProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.toc;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExtensionRegistry;
18 import org.eclipse.core.runtime.InvalidRegistryObjectException;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.help.AbstractTocProvider;
21 import org.eclipse.help.ITocContribution;
22 import org.eclipse.help.internal.HelpPlugin;
23
24 /*
25  * Provides toc data from toc XML files to the help system.
26  */

27 public class TocFileProvider extends AbstractTocProvider {
28
29     public static final String JavaDoc EXTENSION_POINT_ID_TOC = HelpPlugin.PLUGIN_ID + ".toc"; //$NON-NLS-1$
30
public static final String JavaDoc ELEMENT_NAME_TOC = "toc"; //$NON-NLS-1$
31
public static final String JavaDoc ATTRIBUTE_NAME_FILE = "file"; //$NON-NLS-1$
32
public static final String JavaDoc ATTRIBUTE_NAME_PRIMARY = "primary"; //$NON-NLS-1$
33
public static final String JavaDoc ATTRIBUTE_NAME_EXTRADIR = "extradir"; //$NON-NLS-1$
34
public static final String JavaDoc ATTRIBUTE_NAME_CATEGORY = "category"; //$NON-NLS-1$
35

36     /* (non-Javadoc)
37      * @see org.eclipse.help.AbstractTocProvider#getTocContributions(java.lang.String)
38      */

39     public ITocContribution[] getTocContributions(String JavaDoc locale) {
40         List JavaDoc contributions = new ArrayList JavaDoc();
41         TocFile[] tocFiles = getTocFiles(locale);
42         TocFileParser parser = new TocFileParser();
43         for (int i=0;i<tocFiles.length;++i) {
44             try {
45                 ITocContribution toc = parser.parse(tocFiles[i]);
46                 contributions.add(toc);
47             }
48             catch (Throwable JavaDoc t) {
49                 String JavaDoc msg = "Error reading help table of contents file /\"" + tocFiles[i].getPluginId() + '/' + tocFiles[i].getFile() + "\" (skipping file)"; //$NON-NLS-1$ //$NON-NLS-2$
50
HelpPlugin.logError(msg, t);
51             }
52         }
53         return (ITocContribution[])contributions.toArray(new ITocContribution[contributions.size()]);
54     }
55
56     /*
57      * Returns all available TocFiles for the given locale.
58      */

59     protected TocFile[] getTocFiles(String JavaDoc locale) {
60         List JavaDoc tocFiles = new ArrayList JavaDoc();
61         IExtensionRegistry registry = Platform.getExtensionRegistry();
62         IConfigurationElement[] elements = registry.getConfigurationElementsFor(EXTENSION_POINT_ID_TOC);
63         for (int i=0;i<elements.length;++i) {
64             IConfigurationElement elem = elements[i];
65             String JavaDoc pluginId;
66             try {
67                 pluginId = elem.getNamespaceIdentifier();
68             }
69             catch (InvalidRegistryObjectException e) {
70                 // no longer valid; skip it
71
continue;
72             }
73
74             if (elem.getName().equals(ELEMENT_NAME_TOC)) {
75                 String JavaDoc file = elem.getAttribute(ATTRIBUTE_NAME_FILE);
76                 boolean primary = Boolean.toString(true).equals(elem.getAttribute(ATTRIBUTE_NAME_PRIMARY));
77                 String JavaDoc extradir = elem.getAttribute(ATTRIBUTE_NAME_EXTRADIR);
78                 String JavaDoc category = elem.getAttribute(ATTRIBUTE_NAME_CATEGORY);
79                 TocFile tocFile = new TocFile(pluginId, file, primary, locale, extradir, category);
80                 tocFiles.add(tocFile);
81             }
82         }
83         return (TocFile[])tocFiles.toArray(new TocFile[tocFiles.size()]);
84     }
85 }
86
Popular Tags