Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1170bc2d1d09532b22009d13b2b2a9ef171b5a7e (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*******************************************************************************
 * Copyright (c) 2007, 2012 Intel Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Intel Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.help;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.cdt.ui.ICHelpBook;
import org.eclipse.cdt.ui.ICHelpProvider;
import org.eclipse.cdt.ui.ICHelpResourceDescriptor;
import org.eclipse.cdt.ui.IFunctionSummary;
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class CHelpProvider implements ICHelpProvider {

	private static final String EXTENSION_POINT_ID = "org.eclipse.cdt.ui.HelpInfo"; //$NON-NLS-1$
	private static final String ELEMENT_NAME = "helpInfo"; //$NON-NLS-1$
	private static final String ATTRIB_FILE = "file"; //$NON-NLS-1$
	private static final String NODE_HEAD = "documentation"; //$NON-NLS-1$
	private static final String NODE_BOOK = "helpBook"; //$NON-NLS-1$

	private boolean Done = false;

	ICHelpBook[] hbs = null;

	@Override
	public ICHelpBook[] getCHelpBooks() {
		waitForDone();
		return hbs;
	}

	@Override
	public IFunctionSummary getFunctionInfo(ICHelpInvocationContext context, ICHelpBook[] helpBooks, String name) {
		for (int i = 0; i < helpBooks.length; i++) {
			if (helpBooks[i] instanceof CHelpBook) {
				IFunctionSummary fs = ((CHelpBook) helpBooks[i]).getFunctionInfo(context, name);
				if (fs != null) // if null, try with another book
					return fs;
			}
		}
		return null;
	}

	@Override
	public ICHelpResourceDescriptor[] getHelpResources(ICHelpInvocationContext context, ICHelpBook[] helpBooks,
			String name) {

		ArrayList<ICHelpResourceDescriptor> lst = new ArrayList<ICHelpResourceDescriptor>();
		for (ICHelpBook h : helpBooks) {
			if (h instanceof CHelpBook) {
				ICHelpResourceDescriptor hrd = ((CHelpBook) h).getHelpResources(context, name);
				if (hrd != null)
					lst.add(hrd);
			}
		}
		if (lst.size() > 0)
			return lst.toArray(new ICHelpResourceDescriptor[lst.size()]);
		return null;
	}

	@Override
	public IFunctionSummary[] getMatchingFunctions(ICHelpInvocationContext context, ICHelpBook[] helpBooks,
			String prefix) {
		ArrayList<IFunctionSummary> lst = new ArrayList<IFunctionSummary>();
		for (int i = 0; i < helpBooks.length; i++) {
			if (helpBooks[i] instanceof CHelpBook) {
				List<IFunctionSummary> fs = ((CHelpBook) helpBooks[i]).getMatchingFunctions(context, prefix);
				if (fs != null) // if null, try with another book
					lst.addAll(fs);
			}
		}
		if (lst.size() > 0)
			return lst.toArray(new IFunctionSummary[lst.size()]);
		return null;
	}

	@Override
	public void initialize() {
		//		(new Thread() {
		//		public void run() {
		loadExtensions();
		//		  }
		//	    }).run();
	}

	private void waitForDone() {
		if (hbs != null)
			return;
		try {
			while (!Done)
				Thread.sleep(10);
		} catch (InterruptedException e) {
		}
	}

	private void loadExtensions() {
		try {
			IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(EXTENSION_POINT_ID);
			if (extensionPoint != null) {
				IExtension[] extensions = extensionPoint.getExtensions();
				if (extensions != null) {
					ArrayList<ICHelpBook> chbl = new ArrayList<ICHelpBook>();
					for (IExtension ex : extensions) {
						String pluginId = ex.getNamespaceIdentifier();
						for (IConfigurationElement el : ex.getConfigurationElements()) {
							if (el.getName().equals(ELEMENT_NAME)) {
								loadFile(el, chbl, pluginId);
							}
						}
					}
					if (chbl.size() > 0) {
						hbs = chbl.toArray(new ICHelpBook[chbl.size()]);
					}
				}
			}
		} finally {
			Done = true;
		}
	}

	private void loadFile(IConfigurationElement el, ArrayList<ICHelpBook> chbl, String pluginId) {
		String fname = el.getAttribute(ATTRIB_FILE);
		if (fname == null || fname.trim().length() == 0)
			return;
		URL x = FileLocator.find(Platform.getBundle(pluginId), new Path(fname), null);
		if (x == null)
			return;
		try {
			x = FileLocator.toFileURL(x);
		} catch (IOException e) {
			return;
		}
		fname = x.getPath();
		if (fname == null || fname.trim().length() == 0)
			return;

		// format is not supported for now
		// String format = el.getAttribute(ATTRIB_FORMAT);

		Document doc = null;
		try {
			InputStream stream = new FileInputStream(fname);
			BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
			InputSource src = new InputSource(reader);
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			doc = builder.parse(src);
			Element e = doc.getDocumentElement();
			if (NODE_HEAD.equals(e.getNodeName())) {
				NodeList list = e.getChildNodes();
				for (int j = 0; j < list.getLength(); j++) {
					Node node = list.item(j);
					if (node.getNodeType() != Node.ELEMENT_NODE)
						continue;
					if (NODE_BOOK.equals(node.getNodeName())) {
						chbl.add(new CHelpBook((Element) node));
					}
				}
			}
		} catch (ParserConfigurationException e) {
		} catch (SAXException e) {
		} catch (IOException e) {
		}
	}
}

Back to the top