Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 02a30832c7b5f46bfd2a9ea4e2b7ed40ccead77a (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*******************************************************************************
 * Copyright (c) 2007 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
 *******************************************************************************/
package org.eclipse.cdt.internal.core.settings.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IPathEntry;
import org.eclipse.cdt.core.settings.model.ICConfigExtensionReference;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICExternalSetting;
import org.eclipse.cdt.core.settings.model.ICSettingBase;
import org.eclipse.cdt.core.settings.model.extension.CConfigurationData;
import org.eclipse.cdt.core.settings.model.extension.CConfigurationDataProvider;
import org.eclipse.cdt.core.settings.model.extension.CFileData;
import org.eclipse.cdt.core.settings.model.extension.CFolderData;
import org.eclipse.cdt.core.settings.model.extension.CLanguageData;
import org.eclipse.cdt.core.settings.model.extension.CResourceData;
import org.eclipse.cdt.core.settings.model.extension.impl.CDataFacroty;
import org.eclipse.cdt.core.settings.model.extension.impl.CDefaultConfigurationData;
import org.eclipse.cdt.core.settings.model.extension.impl.CDefaultFileData;
import org.eclipse.cdt.core.settings.model.extension.impl.CDefaultFolderData;
import org.eclipse.cdt.core.settings.model.extension.impl.CDefaultLanguageData;
import org.eclipse.cdt.core.settings.model.util.CDataUtil;
import org.eclipse.cdt.core.settings.model.util.EntryStore;
import org.eclipse.cdt.core.settings.model.util.PathEntryResolveInfo;
import org.eclipse.cdt.core.settings.model.util.PathEntryTranslator;
import org.eclipse.cdt.core.settings.model.util.PathEntryTranslator.ReferenceSettingsInfo;
import org.eclipse.cdt.internal.core.model.CModelManager;
import org.eclipse.cdt.internal.core.model.PathEntryManager;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

public class PathEntryConfigurationDataProvider extends
		CConfigurationDataProvider {
	private static PathEntryDataFactory fFactory;
	
	
	public static CDataFacroty getDataFactory(){
		if(fFactory == null){
			fFactory = new PathEntryDataFactory();
		}
		return fFactory;
	}

	public PathEntryConfigurationDataProvider(){
		getDataFactory();
	}

	private static class PathEntryFolderData extends CDefaultFolderData {
		private EntryStore fStore;
		
//		public PathEntryFolderData(CConfigurationData cfg, CDataFacroty factory) {
//			super(cfg, factory);
//		}

//		public PathEntryFolderData(String id, IPath path,
//				CConfigurationData cfg, CDataFacroty factory) {
//			super(id, path, cfg, factory);
//		}

		public PathEntryFolderData(String id, IPath path, PathEntryFolderData base,
				CConfigurationData cfg, CDataFacroty factory, boolean clone) {
			super(id, path, cfg, factory);
			
			if(base != null)
				fStore = new EntryStore(base.fStore, true);
			else
				fStore = new EntryStore(true);
			
			copyDataFrom(base, clone);
		}
	}

	private static class PathEntryFileData extends CDefaultFileData {
		private EntryStore fStore;

//		public PathEntryFileData(CConfigurationData cfg, CDataFacroty factory) {
//			super(cfg, factory);
//		}

//		public PathEntryFileData(String id, IPath path, CConfigurationData cfg,
//				CDataFacroty factory) {
//			super(id, path, cfg, factory);
//		}

		public PathEntryFileData(String id, IPath path, PathEntryFileData base,
				CConfigurationData cfg, CDataFacroty factory, boolean clone) {
			super(id, path, cfg, factory);

			fStore = new EntryStore(base.fStore, true);
			
			copyDataFrom(base, clone);
		}

		public PathEntryFileData(String id, IPath path, PathEntryFolderData base,
				CLanguageData baseLangData, CConfigurationData cfg,
				CDataFacroty factory) {
			super(id, path, cfg, factory);

			fStore = new EntryStore(base.fStore, true);
			
			copyDataFrom(base, baseLangData);
		}
		
	}
	
	private static class PathEntryLanguageData extends CDefaultLanguageData {

		public PathEntryLanguageData(String id, CLanguageData base, EntryStore store) {
			fId = id;
			fStore = store;
			copySettingsFrom(base);
		}

		public PathEntryLanguageData(String id, String name, String languageId, int kinds, 
				String[] ids, boolean isContentTypes, EntryStore store) {
			super(id, languageId, ids, isContentTypes);
			fStore = store;
			fName = name;
			fSupportedKinds = kinds;
		}

		protected EntryStore createStore() {
			return fStore;
		}

		protected EntryStore createStore(CLanguageData data) {
			return fStore;
		}
		
	}
	
	private static class PathEntryDataFactory extends CDataFacroty {

		public CConfigurationData createConfigurationdata(String id,
				String name, CConfigurationData base, boolean clone) {
			if(clone){
				id = base.getId();
			} else if(id == null){
				id = CDataUtil.genId(null);
			}
			
			return new CfgData(id, name, base, clone);
		}

		public CFileData createFileData(CConfigurationData cfg,
				CResourceData base, CLanguageData base2, 
				String id, boolean clone,
				IPath path) {
			if(id == null)
				id = clone ? base.getId() : CDataUtil.genId(cfg.getId());
			if(base.getType() == ICSettingBase.SETTING_FILE)
				return new PathEntryFileData(id, path, (PathEntryFileData)base, cfg, this, clone);
			return new PathEntryFileData(id, path, (PathEntryFolderData)base,
					base2, cfg,	this);
		}

		public CFolderData createFolderData(CConfigurationData cfg,
				CFolderData base, String id, boolean clone, IPath path) {
			if(id == null)
				id = clone ? base.getId() : CDataUtil.genId(cfg.getId());
			return new PathEntryFolderData(id, path, (PathEntryFolderData)base, cfg, this, clone);
		}

		public CLanguageData createLanguageData(CConfigurationData cfg,
				CResourceData rcBase, CLanguageData base, String id, boolean clone) {
			if(id == null)
				id = clone ? base.getId() : CDataUtil.genId(rcBase.getId());
			EntryStore store;
			if(rcBase.getType() == ICSettingBase.SETTING_FOLDER)
				store = ((PathEntryFolderData)rcBase).fStore;
			else
				store = ((PathEntryFileData)rcBase).fStore;
			return new PathEntryLanguageData(id, base, store);
		}

		public CLanguageData createLanguageData(CConfigurationData cfg,
				CResourceData rcBase, String id, String name, String languageId, int supportedEntryKinds, String[] rcTypes,
				boolean isContentTypes) {
			if(id == null)
				id = CDataUtil.genId(rcBase.getId());
			EntryStore store;
			if(rcBase.getType() == ICSettingBase.SETTING_FOLDER)
				store = ((PathEntryFolderData)rcBase).fStore;
			else
				store = ((PathEntryFileData)rcBase).fStore;
			return new PathEntryLanguageData(id, name, languageId, supportedEntryKinds, rcTypes, isContentTypes, store);
		}
		
	}

	private static class CfgData extends CDefaultConfigurationData {
//		private PathEntryResolveInfo fResolveInfo;

		public CfgData(String id, String name, CConfigurationData base, boolean clone) {
			super(id, name, base, fFactory, clone);
		}

		public CfgData(String id, String name) {
			super(id, name, fFactory);
		}
		
//		public PathEntryResolveInfo getResolveInfo(){
//			return fResolveInfo;
//		}
//		
//		public void setResolveInfo(PathEntryResolveInfo info){
//			fResolveInfo = info;
//		}
	}
	
	public static boolean isPathEntryData(CConfigurationData data){
		return data instanceof CfgData;
	}
	
	public CConfigurationData applyConfiguration(
			ICConfigurationDescription des,
			ICConfigurationDescription baseDescription,
			CConfigurationData base,
			IProgressMonitor monitor)
			throws CoreException {
		//TODO: check external/reference info here as well.
		if(!fFactory.isModified(base)){
			return createData(des, base, false, true);
		}
		
		
		IProject project = des.getProjectDescription().getProject();
//		ReferenceSettingsInfo refInfo = new ReferenceSettingsInfo(des);
		IPathEntry entries[] = PathEntryTranslator.getPathEntries(project, baseDescription, PathEntryTranslator.INCLUDE_USER);
		CModelManager manager = CModelManager.getDefault();
		ICProject cproject = manager.create(project);
		IPathEntry[] curRawEntries = PathEntryManager.getDefault().getRawPathEntries(cproject);
		
		List list = new ArrayList();
		list.addAll(Arrays.asList(entries));
		for(int i = 0; i < curRawEntries.length; i++){
			if(curRawEntries[i].getEntryKind() == IPathEntry.CDT_CONTAINER){
				list.add(curRawEntries[i]);
			}
		}
		
		IPathEntry[] newEntries = (IPathEntry[])list.toArray(new IPathEntry[list.size()]);
		PathEntryManager.getDefault().setRawPathEntries(cproject, newEntries, new NullProgressMonitor());
		return createData(des, base, false, false);
	}
	
	private CConfigurationData createData(ICConfigurationDescription des, CConfigurationData fallbackData, boolean modifiedFlag, boolean useCache) throws CoreException {
		CConfigurationData dataToReturn;
		try {
			dataToReturn = createData(des, useCache);
		} catch (Exception e){
			if(fallbackData != null)
				dataToReturn = fallbackData;
			else if (e instanceof CoreException)
				throw (CoreException)e;
			else
				throw ExceptionFactory.createCoreException(e);
		}
		fFactory.setModified(dataToReturn, modifiedFlag);
		return dataToReturn;
		
	}

	public CConfigurationData createConfiguration(
			ICConfigurationDescription des, 
			ICConfigurationDescription baseDescription,
			CConfigurationData base,
			boolean clone,
			IProgressMonitor monitor) throws CoreException {
		CfgData copy = new CfgData(des.getId(), des.getName(), base, clone);
		copy.setModified(false);
		return copy;
	}
	
	private CfgData createData(ICConfigurationDescription des, boolean useCache) throws CoreException{
		IProject project = des.getProjectDescription().getProject();
		CModelManager manager = CModelManager.getDefault();
		ICProject cproject = manager.create(project);
		PathEntryResolveInfo rInfo = PathEntryManager.getDefault().getResolveInfo(cproject, useCache);
		
		CfgData data = new CfgData(des.getId(), des.getName());
		data.initEmptyData();
		CDataUtil.adjustConfig(data, getDataFactory());
		//CProjectDescriptionManager.getInstance().adjustDefaultConfig(data);
	
//		data.setResolveInfo(rInfo);
		PathEntryTranslator tr = new PathEntryTranslator(project, data);
		ReferenceSettingsInfo refInfo = tr.applyPathEntries(rInfo, PathEntryTranslator.OP_REPLACE);
		ICExternalSetting extSettings[] = refInfo.getExternalSettings();
		des.removeExternalSettings();
		if(extSettings.length != 0){
			ICExternalSetting setting;
			for(int i = 0; i < extSettings.length; i++){
				setting = extSettings[i];
				des.createExternalSetting(setting.getCompatibleLanguageIds(), 
						setting.getCompatibleContentTypeIds(), 
						setting.getCompatibleExtensions(), 
						setting.getEntries());
			}
		}

//		IPath projPaths[] = refInfo.getReferencedProjectsPaths();
//		if(projPaths.length != 0){
//			Map map = new HashMap(projPaths.length);
//			for(int i = 0; i < projPaths.length; i++){
//				map.put(projPaths[i].segment(0), "");	//$NON-NLS-1$
//			}
//			des.setReferenceInfo(map);
//		}

		cproject.close();
		
		String[] ids = getIds(des.get(CCorePlugin.BINARY_PARSER_UNIQ_ID));
		data.getTargetPlatformData().setBinaryParserIds(ids);
		
		ids = getIds(des.get(CCorePlugin.ERROR_PARSER_UNIQ_ID));
		data.getBuildData().setErrorParserIDs(ids);
		
		data.setModified(false);
		return data;
	}
	
	private String[] getIds(ICConfigExtensionReference refs[]){
		if(refs == null || refs.length == 0)
			return new String[0];
		
		String[] ids = new String[refs.length];
		for(int i = 0; i < refs.length; i++){
			ids[i] = refs[i].getID();
		}
		return ids;
	}

	public CConfigurationData loadConfiguration(ICConfigurationDescription des,
			IProgressMonitor monitor)
			throws CoreException {
		return createData(des, null, false, true);
	}

	public void removeConfiguration(ICConfigurationDescription des,
			CConfigurationData data,
			IProgressMonitor monitor) {
		//do nothing for now
	}

	public void dataCached(ICConfigurationDescription cfgDes,
			CConfigurationData data, IProgressMonitor monitor) {
		fFactory.setModified(data, false);
	}

}

Back to the top