Skip to main content
summaryrefslogtreecommitdiffstats
blob: f8096cd41733f83e6b25c3262dfd9b03a6d49edb (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**********************************************************************
 * Copyright (c) 2004, 2006 Intel Corporation and others.
 * 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
 *     Markus Schorn (Wind River Systems)
 **********************************************************************/
package org.eclipse.cdt.internal.ui;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.ui.text.CHelpBookDescriptor;
import org.eclipse.cdt.internal.ui.text.CHelpSettings;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.ICHelpResourceDescriptor;
import org.eclipse.cdt.ui.IFunctionSummary;
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
import org.eclipse.core.resources.IProject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * This class is used to manage external plugins that contribute 
 * C/C++ help information through the CHelpProvider extension point
 */

public class CHelpProviderManager {
	final private static String C_HELP_SETTINGS_FILE_NAME = "cHelpSettings.xml"; //$NON-NLS-1$
	final private static String ELEMENT_ROOT = "cHelpSettings"; //$NON-NLS-1$

	//private static Map fProjectHelpSettings = null;
	private static CHelpSettings fDefaultHelpSettings = null;
	
	private static File fSettingsFile = null;
	static private CHelpProviderManager fInstance = null;

	private static IProject fCurrentProject = null;
	private static CHelpSettings fCurrentSettings = null;

	private CHelpProviderManager() {
	}
	
	private static File getSettingsFile(){
		if(fSettingsFile == null){
			fSettingsFile = CUIPlugin.getDefault().getStateLocation().append(C_HELP_SETTINGS_FILE_NAME).toFile();
		}
		return fSettingsFile;
	}
	
//	private static Map getSettingsMap(){
//		if(fProjectHelpSettings == null)
//			fProjectHelpSettings = new HashMap();
//		return fProjectHelpSettings;
//	}
	
	private static CHelpSettings getDefaultHelpSettings(){
		if(fDefaultHelpSettings == null){
			fDefaultHelpSettings = new CHelpSettings(null);
		}
		return fDefaultHelpSettings;
	}

	private static CHelpSettings getPersistedHelpSettings(IProject project){
/* uncomment to use Map
		Map settingsMap = getSettingsMap();
		CHelpSettings settings = (CHelpSettings)settingsMap.get(project.getName());
		if(settings == null){
			settings = createHelpSettings(project);
			settingsMap.put(project.getName(),settings);
		}
		return settings;
*/
		return createHelpSettings(project);
	}
	
	private static CHelpSettings createHelpSettings(IProject project){
//		String projectName = project.getName();
		File file = getSettingsFile();
		CHelpSettings settings = null;
		Element rootElement = null;

		if(file.isFile()){
			try{
				DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
				Document doc = builder.parse(file);
				NodeList nodes = doc.getElementsByTagName(ELEMENT_ROOT);
				
				if (nodes.getLength() > 0)
					rootElement = (Element)nodes.item(0);
				
			}catch(ParserConfigurationException e){
			}catch(SAXException e){
			}catch(IOException e){
			}
		}

		settings = new CHelpSettings(project,rootElement);
		return settings;
	}
	
	private static CHelpSettings getHelpSettings(IProject project){
		if(project == null)
			return getDefaultHelpSettings();

		CHelpSettings settings = null;
		if(fCurrentProject != null && fCurrentSettings != null && project == fCurrentProject)
			settings = fCurrentSettings;
		else{
			fCurrentProject = project;
			fCurrentSettings = getPersistedHelpSettings(project);
			settings = fCurrentSettings;
		}
		return settings;
	}
	
	private static CHelpSettings getHelpSettings(ICHelpInvocationContext context){
		IProject project = getProjectFromContext(context);
		
		return getHelpSettings(project);
	}
	
	private static IProject getProjectFromContext(ICHelpInvocationContext context){
		IProject project = context.getProject();
		if(project == null){
			ITranslationUnit unit = context.getTranslationUnit();
			if(unit != null)
				project = unit.getCProject().getProject();
		}
		return project;
	}

	public static CHelpProviderManager getDefault() {
		if (fInstance == null) {
			fInstance = new CHelpProviderManager();
		}
		return fInstance;
	}

	public IFunctionSummary getFunctionInfo(ICHelpInvocationContext context, String name) {
		CHelpSettings settings = getHelpSettings(context);
	
		return settings.getFunctionInfo(context,name);
	}

	public IFunctionSummary[] getMatchingFunctions(ICHelpInvocationContext context, String frag) {
		CHelpSettings settings = getHelpSettings(context);
		
		return settings.getMatchingFunctions(context,frag);
	}

	public ICHelpResourceDescriptor[] getHelpResources(ICHelpInvocationContext context, String name){
		CHelpSettings settings = getHelpSettings(context);
		
		return settings.getHelpResources(context,name);
	}
	
	public CHelpBookDescriptor[] getCHelpBookDescriptors(ICHelpInvocationContext context){
		return getHelpSettings(context).getCHelpBookDescriptors();
	}
	
	public void serialize(ICHelpInvocationContext context){
		CHelpSettings settings = getHelpSettings(context);

		File file = getSettingsFile();

		try{
			DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document doc;
			Element rootElement = null;

			if(file.exists()){
				doc = builder.parse(file);
				NodeList nodes = doc.getElementsByTagName(ELEMENT_ROOT);
				
				if (nodes.getLength() > 0)
					rootElement = (Element)nodes.item(0);
			}
			else{
				doc = builder.newDocument();
			}

			if(rootElement == null){
				rootElement = doc.createElement(ELEMENT_ROOT);
				doc.appendChild(rootElement);
			}

			settings.serialize(doc,rootElement);

			FileWriter writer = new FileWriter(file);

			Transformer transformer=TransformerFactory.newInstance().newTransformer();
			transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
			transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(writer);

			transformer.transform(source, result);

			writer.close();
		}catch(ParserConfigurationException e){
		}catch(SAXException e){
		}catch(TransformerConfigurationException e){
		}catch(TransformerException e){
		}catch(IOException e){
		}
	}
}

Back to the top