Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4eca580537cef7e3b6cad0f3911f420e0248b10c (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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*******************************************************************************
 * Copyright (c) 2002, 2003, 2004 QNX Software Systems Ltd. and others. All
 * rights reserved. This program and the accompanying materials are made
 * available under the terms of the Common Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: QNX Software Systems - Initial API and implementation
 ******************************************************************************/
package org.eclipse.cdt.internal.core;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

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.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.CDescriptorEvent;
import org.eclipse.cdt.core.ICDescriptor;
import org.eclipse.cdt.core.ICExtension;
import org.eclipse.cdt.core.ICExtensionReference;
import org.eclipse.cdt.core.ICOwnerInfo;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IPluginRegistry;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.ProcessingInstruction;

public class CDescriptor implements ICDescriptor {

	final CDescriptorManager fManager;
	final IProject fProject;
	private COwner fOwner;

	private HashMap extMap = new HashMap(4);
	private HashMap extInfoMap = new HashMap(4);
	private Document dataDoc;

	static final String DESCRIPTION_FILE_NAME = ".cdtproject"; //$NON-NLS-1$

	private static final String CEXTENSION_NAME = "cextension"; //$NON-NLS-1$

	private static final String PROJECT_DESCRIPTION = "cdtproject"; //$NON-NLS-1$
	private static final String PROJECT_OWNER_ID = "id"; //$NON-NLS-1$
	private static final String PROJECT_EXTENSION = "extension"; //$NON-NLS-1$
	private static final String PROJECT_EXTENSION_ATTR_POINT = "point"; //$NON-NLS-1$
	private static final String PROJECT_EXTENSION_ATTR_ID = "id"; //$NON-NLS-1$
	private static final String PROJECT_EXTENSION_ATTRIBUTE = "attribute"; //$NON-NLS-1$
	private static final String PROJECT_EXTENSION_ATTRIBUTE_KEY = "key"; //$NON-NLS-1$
	private static final String PROJECT_EXTENSION_ATTRIBUTE_VALUE = "value"; //$NON-NLS-1$

	private static final String PROJECT_DATA = "data"; //$NON-NLS-1$
	private static final String PROJECT_DATA_ITEM = "item"; //$NON-NLS-1$
	private static final String PROJECT_DATA_ID = "id"; //$NON-NLS-1$

	boolean fUpdating;
	boolean isInitializing = true;

	protected CDescriptor(CDescriptorManager manager, IProject project, String id) throws CoreException {
		fProject = project;
		fManager = manager;
		IPath projectLocation = project.getDescription().getLocation();

		if (projectLocation == null) {
			projectLocation = getProjectDefaultLocation(project);
		}
		IPath descriptionPath = projectLocation.append(DESCRIPTION_FILE_NAME);

		if (descriptionPath.toFile().exists()) {
			IStatus status;
			String ownerID = readCDTProjectFile(descriptionPath);
			if (ownerID.equals(id)) {
				status = new Status(IStatus.WARNING, CCorePlugin.PLUGIN_ID, CCorePlugin.STATUS_CDTPROJECT_EXISTS,
						CCorePlugin.getResourceString("CDescriptor.exception.projectAlreadyExists"), (Throwable) null); //$NON-NLS-1$
			} else {
				status = new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, CCorePlugin.STATUS_CDTPROJECT_MISMATCH,
						CCorePlugin.getResourceString("CDescriptor.exception.unmatchedOwnerId"), (Throwable) null); //$NON-NLS-1$
			}
			throw new CoreException(status);
		}
		fOwner = new COwner(manager.getOwnerConfiguration(id));
		fOwner.configure(project, this);
		isInitializing = false;
		save();
	}

	protected CDescriptor(CDescriptorManager manager, IProject project) throws CoreException {
		fProject = project;
		fManager = manager;
		IPath projectLocation = project.getDescription().getLocation();

		if (projectLocation == null) {
			projectLocation = getProjectDefaultLocation(project);
		}
		IPath descriptionPath = projectLocation.append(DESCRIPTION_FILE_NAME);

		if (!descriptionPath.toFile().exists()) {
			fOwner = new COwner(manager.getOwnerConfiguration(project));
			fOwner.configure(project, this);
			isInitializing = false;
			fManager.updateDescriptor(this);
		} else {
			String ownerId = readCDTProjectFile(descriptionPath);
			fOwner = new COwner(manager.getOwnerConfiguration(ownerId));
		}
	}

	protected CDescriptor(CDescriptorManager manager, IProject project, COwner owner) throws CoreException {
		fProject = project;
		fManager = manager;
		IPath projectLocation = project.getDescription().getLocation();

		if (projectLocation == null) {
			projectLocation = getProjectDefaultLocation(project);
		}
		IPath descriptionPath = projectLocation.append(DESCRIPTION_FILE_NAME);

		if (descriptionPath.toFile().exists()) {
			readCDTProjectFile(descriptionPath);
		}
		fOwner = owner;
		fOwner.configure(project, this);
		isInitializing = false;
		save();
	}

	private String readCDTProjectFile(IPath descriptionPath) throws CoreException {
		FileInputStream file = null;
		String ownerID = ""; //$NON-NLS-1$
		try {
			file = new FileInputStream(descriptionPath.toFile());
			DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document document = parser.parse(file);
			NodeList nodeList = document.getElementsByTagName(PROJECT_DESCRIPTION);
			if (nodeList != null && nodeList.getLength() > 0) {
				Node node = nodeList.item(0);
				if (node.hasAttributes()) {
					ownerID = node.getAttributes().getNamedItem(PROJECT_OWNER_ID).getNodeValue();
				}
				readProjectDescription(node);
				return ownerID;
			}
			IStatus status = new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1,
					CCorePlugin.getResourceString("CDescriptor.exception.missingElement"), null); //$NON-NLS-1$
			throw new CoreException(status);
		} catch (Exception e) {
			IStatus status = new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, e.toString(), e);
			throw new CoreException(status);
		} finally {
			if (file != null) {
				try {
					file.close();
				} catch (IOException e) {
				}
			}
		}
	}

	private static IPath getProjectDefaultLocation(IProject project) {
		return Platform.getLocation().append(project.getFullPath());
	}

	public ICOwnerInfo getProjectOwner() {
		return fOwner;
	}

	public String getPlatform() {
		return fOwner.getPlatform();
	}

	public IProject getProject() {
		return fProject;
	}

	synchronized public ICExtensionReference[] get(String extensionID) {
		CExtensionReference[] refs = (CExtensionReference[]) extMap.get(extensionID);
		if (refs == null)
			return new ICExtensionReference[0];
		return refs;
	}

	synchronized public ICExtensionReference[] get(String extensionID, boolean update) {
		ICExtensionReference[] refs = get(extensionID);
		if (refs.length == 0 && update) {
			try {
				boolean oldIsInitializing = isInitializing;
				isInitializing = true;
				fOwner.update(fProject, this, extensionID);
				isInitializing = oldIsInitializing;
				if (!isInitializing) {
					updateOnDisk();
				}
				refs = get(extensionID);
			} catch (CoreException e) {
			}
		}
		return refs;
	}

	private CExtensionReference createRef(String extensionPoint, String extension) {
		CExtensionReference extensions[] = (CExtensionReference[]) extMap.get(extensionPoint);
		if (extensions == null) {
			extensions = new CExtensionReference[1];
			extMap.put(extensionPoint, extensions);
		} else {
			CExtensionReference[] newExtensions = new CExtensionReference[extensions.length + 1];
			System.arraycopy(extensions, 0, newExtensions, 0, extensions.length);
			extensions = newExtensions;
			extMap.put(extensionPoint, extensions);
		}
		extensions[extensions.length - 1] = new CExtensionReference(this, extensionPoint, extension);
		return extensions[extensions.length - 1];
	}

	synchronized public ICExtensionReference create(String extensionPoint, String extension) throws CoreException {
		CExtensionReference extRef = createRef(extensionPoint, extension);
		if (!isInitializing) {
			updateOnDisk();
			fManager.fireEvent(new CDescriptorEvent(this, CDescriptorEvent.CDTPROJECT_CHANGED, CDescriptorEvent.EXTENSION_CHANGED));
		}
		return extRef;
	}

	synchronized public void remove(ICExtensionReference ext) throws CoreException {
		CExtensionReference extensions[] = (CExtensionReference[]) extMap.get(ext.getExtension());
		for (int i = 0; i < extensions.length; i++) {
			if (extensions[i] == ext) {
				System.arraycopy(extensions, i, extensions, i + 1, extensions.length - 1 - i);
				if (extensions.length > 1) {
					CExtensionReference[] newExtensions = new CExtensionReference[extensions.length - 1];
					System.arraycopy(extensions, 0, newExtensions, 0, newExtensions.length);
					extMap.put(ext.getExtension(), newExtensions);
				} else {
					extMap.remove(ext.getExtension());
				}
				if (!isInitializing) {
					updateOnDisk();
					fManager.fireEvent(new CDescriptorEvent(this, CDescriptorEvent.CDTPROJECT_CHANGED,
							CDescriptorEvent.EXTENSION_CHANGED));
				}
			}
		}
	}

	synchronized public void remove(String extensionPoint) throws CoreException {
		CExtensionReference extensions[] = (CExtensionReference[]) extMap.get(extensionPoint);
		if (extensions != null) {
			extMap.remove(extensionPoint);
			if (!isInitializing) {
				updateOnDisk();
				fManager.fireEvent(new CDescriptorEvent(this, CDescriptorEvent.CDTPROJECT_CHANGED,
						CDescriptorEvent.EXTENSION_CHANGED));
			}
		}
	}

	synchronized CExtensionInfo getInfo(CExtensionReference cProjectExtension) {
		CExtensionInfo info = (CExtensionInfo) extInfoMap.get(cProjectExtension);
		if (info == null) {
			info = new CExtensionInfo();
			extInfoMap.put(cProjectExtension, info);
		}
		return info;
	}

	protected IFile getFile() {
		return getProject().getFile(DESCRIPTION_FILE_NAME);
	}

	void save() throws CoreException {
		fManager.getWorkspace().run(new IWorkspaceRunnable() {

			public void run(IProgressMonitor mon) throws CoreException {
				String xml;
				if (!fProject.isAccessible()) {
					return;
				}
				fUpdating = true;

				try {
					xml = getAsXML();
				} catch (IOException e) {
					IStatus s = new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, e.getMessage(), e);
					throw new CoreException(s);
				} catch (TransformerException e) {
					IStatus s = new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, e.getMessage(), e);
					throw new CoreException(s);
				} catch (ParserConfigurationException e) {
					IStatus s = new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1, e.getMessage(), e);
					throw new CoreException(s);
				}

				IFile rscFile = getFile();
				InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
				// update the resource content
				if (rscFile.exists()) {
					if (rscFile.isReadOnly()) {
						// provide opportunity to checkout read-only .cdtproject file
						fManager.getWorkspace().validateEdit(new IFile[]{rscFile}, null);
					}
					rscFile.setContents(inputStream, IResource.FORCE, null);
				} else {
					rscFile.create(inputStream, IResource.FORCE, null);
				}
				fUpdating = false;
			}
		}, fProject, IWorkspace.AVOID_UPDATE, null);
	}

	boolean isUpdating() {
		return fUpdating;
	}

	synchronized void updateOnDisk() {
		if (isUpdating()) {
			return;
		}
		fUpdating = true;
		fManager.updateDescriptor(this);
	}

	synchronized void updateFromDisk() throws CoreException {
		IPath projectLocation = fProject.getDescription().getLocation();

		if (projectLocation == null) {
			projectLocation = getProjectDefaultLocation(fProject);
		}
		IPath descriptionPath = projectLocation.append(DESCRIPTION_FILE_NAME);
		if (!descriptionPath.toFile().exists()) {
			updateOnDisk();
			return;
		}
		COwner origOwner = fOwner;
		HashMap origExtMap = extMap;
		HashMap origExtInfoMap = extInfoMap;
		Document origDataDoc = dataDoc;

		extMap = new HashMap(4);
		extInfoMap = new HashMap(4);
		dataDoc = null;

		try {
			String ownerId = readCDTProjectFile(descriptionPath);
			fOwner = new COwner(fManager.getOwnerConfiguration(ownerId));
		} catch (CoreException e) {
			CCorePlugin.log(e);
			fOwner = origOwner;
			extMap = origExtMap;
			extInfoMap = origExtInfoMap;
			dataDoc = origDataDoc;
		}
		if (!fOwner.equals(origOwner)) {
			fManager.fireEvent(new CDescriptorEvent(this, CDescriptorEvent.CDTPROJECT_CHANGED, CDescriptorEvent.OWNER_CHANGED));
		} else {
			boolean extChanges = true;
			if (extMap.size() == origExtMap.size() && extInfoMap.size() == origExtInfoMap.size()) {
				extChanges = false;
				Iterator entries = extMap.entrySet().iterator();
				while (entries.hasNext()) {
					Entry entry = (Entry) entries.next();
					if (!origExtMap.containsKey(entry.getKey())) {
						extChanges = true;
						break;
					}
					CExtensionReference origExt[] = (CExtensionReference[]) origExtMap.get(entry.getKey());
					CExtensionReference newExt[] = (CExtensionReference[]) entry.getValue();
					if (!Arrays.equals(origExt, newExt)) {
						extChanges = true;
						break;
					}
				}
			}
			if (extChanges) {
				fManager.fireEvent(new CDescriptorEvent(this, CDescriptorEvent.CDTPROJECT_CHANGED,
						CDescriptorEvent.EXTENSION_CHANGED));
			} else {
				fManager.fireEvent(new CDescriptorEvent(this, CDescriptorEvent.CDTPROJECT_CHANGED, 0));
			}
		}
	}

	private void readProjectDescription(Node node) {
		Node childNode;
		NodeList list = node.getChildNodes();
		for (int i = 0; i < list.getLength(); i++) {
			childNode = list.item(i);
			if (childNode.getNodeType() == Node.ELEMENT_NODE) {
				if (childNode.getNodeName().equals(PROJECT_EXTENSION)) {
					try {
						decodeProjectExtension((Element) childNode);
					} catch (CoreException e) {
						CCorePlugin.log(e);
					}
				} else if (childNode.getNodeName().equals(PROJECT_DATA)) {
					try {
						decodeProjectData((Element) childNode);
					} catch (CoreException e) {
						CCorePlugin.log(e);
					}
				}
			}
		}
	}

	private void decodeProjectExtension(Element element) throws CoreException {
		String point = element.getAttribute(PROJECT_EXTENSION_ATTR_POINT);
		String id = element.getAttribute(PROJECT_EXTENSION_ATTR_ID);
		CExtensionReference ext = createRef(point, id);
		NodeList extAttrib = element.getChildNodes();
		for (int j = 0; j < extAttrib.getLength(); j++) {
			if (extAttrib.item(j).getNodeName().equals(PROJECT_EXTENSION_ATTRIBUTE)) {
				NamedNodeMap attrib = extAttrib.item(j).getAttributes();
				getInfo(ext).setAttribute(attrib.getNamedItem(PROJECT_EXTENSION_ATTRIBUTE_KEY).getNodeValue(),
						attrib.getNamedItem(PROJECT_EXTENSION_ATTRIBUTE_VALUE).getNodeValue());
			}
		}
	}

	private void encodeProjectExtensions(Document doc, Element configRootElement) {
		Element element;
		Iterator extIterator = extMap.values().iterator();
		while (extIterator.hasNext()) {
			CExtensionReference extension[] = (CExtensionReference[]) extIterator.next();
			for (int i = 0; i < extension.length; i++) {
				configRootElement.appendChild(element = doc.createElement(PROJECT_EXTENSION));
				element.setAttribute(PROJECT_EXTENSION_ATTR_POINT, extension[i].getExtension());
				element.setAttribute(PROJECT_EXTENSION_ATTR_ID, extension[i].getID());
				CExtensionInfo info = (CExtensionInfo) extInfoMap.get(extension[i]);
				if (info != null) {
					Iterator attribIterator = info.getAttributes().entrySet().iterator();
					while (attribIterator.hasNext()) {
						Entry entry = (Entry) attribIterator.next();
						Element extAttributes = doc.createElement(PROJECT_EXTENSION_ATTRIBUTE);
						extAttributes.setAttribute(PROJECT_EXTENSION_ATTRIBUTE_KEY, (String) entry.getKey());
						extAttributes.setAttribute(PROJECT_EXTENSION_ATTRIBUTE_VALUE, (String) entry.getValue());
						element.appendChild(extAttributes);
					}
				}
			}
		}
	}

	String getAsXML() throws IOException, TransformerException, ParserConfigurationException {
		DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
		Document doc = docBuilder.newDocument();
		ProcessingInstruction version = doc.createProcessingInstruction("eclipse-cdt", "version=\"2.0\""); //$NON-NLS-1$ //$NON-NLS-2$
		doc.appendChild(version);
		Element configRootElement = doc.createElement(PROJECT_DESCRIPTION);
		doc.appendChild(configRootElement);
		if (fOwner.getID().length() > 0) {
			configRootElement.setAttribute(PROJECT_OWNER_ID, fOwner.getID());
		}
		encodeProjectExtensions(doc, configRootElement);
		encodeProjectData(doc, configRootElement);
		return serializeDocument(doc);
	}

	protected ICExtension createExtensions(ICExtensionReference ext) throws CoreException {
		InternalCExtension cExtension = null;
		IPluginRegistry pluginRegistry = Platform.getPluginRegistry();
		IExtensionPoint extensionPoint = pluginRegistry.getExtensionPoint(ext.getExtension());
		IExtension extension = extensionPoint.getExtension(ext.getID());
		if (extension == null) {
			throw new CoreException(new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1,
					CCorePlugin.getResourceString("CDescriptor.exception.providerNotFound") + ":" + ext.getID(), null)); //$NON-NLS-1$ //$NON-NLS-2$
		}
		IConfigurationElement element[] = extension.getConfigurationElements();
		for (int i = 0; i < element.length; i++) {
			if (element[i].getName().equalsIgnoreCase("cextension")) { //$NON-NLS-1$
				cExtension = (InternalCExtension) element[i].createExecutableExtension("run"); //$NON-NLS-1$
				cExtension.setExtenionReference(ext);
				cExtension.setProject(fProject);
				break;
			}
		}
		return (ICExtension) cExtension;
	}

	protected IConfigurationElement[] getConfigurationElement(ICExtensionReference ext) throws CoreException {
		IPluginRegistry pluginRegistry = Platform.getPluginRegistry();
		IExtensionPoint extensionPoint = pluginRegistry.getExtensionPoint(ext.getExtension());
		IExtension extension = extensionPoint.getExtension(ext.getID());
		if (extension == null) {
			throw new CoreException(new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, -1,
					CCorePlugin.getResourceString("CDescriptor.exception.providerNotFound"), null)); //$NON-NLS-1$
		}
		IConfigurationElement element[] = extension.getConfigurationElements();
		for (int i = 0; i < element.length; i++) {
			if (element[i].getName().equalsIgnoreCase(CEXTENSION_NAME)) {
				return element[i].getChildren();
			}
		}
		return new IConfigurationElement[0];
	}

	public Element getProjectData(String id) throws CoreException {
		NodeList nodes = getProjectDataDoc().getDocumentElement().getElementsByTagName(PROJECT_DATA_ITEM);
		for (int i = 0; i < nodes.getLength(); ++i) {
			Element element = (Element) nodes.item(i);
			if (element.getAttribute(PROJECT_DATA_ID).equals(id))
				return element;
		}

		// Not found, make a new one
		Element element = dataDoc.createElement(PROJECT_DATA_ITEM);
		element.setAttribute(PROJECT_DATA_ID, id);
		dataDoc.getDocumentElement().appendChild(element);
		return element;
	}

	public void saveProjectData() throws CoreException {
		save();
		fManager.fireEvent(new CDescriptorEvent(this, CDescriptorEvent.CDTPROJECT_CHANGED, 0));
	}

	// The project data allows for the storage of any structured information
	// into the cdtproject file.
	synchronized private Document getProjectDataDoc() throws CoreException {
		if (dataDoc == null) {
			try {
				dataDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
			} catch (ParserConfigurationException e) {
				throw new CoreException(new Status(IStatus.ERROR, CCorePlugin.PLUGIN_ID, IStatus.ERROR,
						CCorePlugin.getResourceString("CDescriptor.extension.internalError"), e)); //$NON-NLS-1$
			}
			Element rootElem = dataDoc.createElement(PROJECT_DATA);
			dataDoc.appendChild(rootElem);
		}
		return dataDoc;
	}

	private void decodeProjectData(Element data) throws CoreException {
		Document doc = getProjectDataDoc();
		doc.replaceChild(doc.importNode(data, true), doc.getDocumentElement());
	}

	private void encodeProjectData(Document doc, Element root) {
		// Don't create or encode the doc if it isn't there already
		if (dataDoc != null) {
			Element dataElements = dataDoc.getDocumentElement();
			NodeList nodes = dataElements.getElementsByTagName(PROJECT_DATA_ITEM);
			for (int i = 0; i < nodes.getLength(); ++i) {
				Element item = (Element) nodes.item(i);
				if (!item.hasChildNodes()) { // remove any empty item tags
					dataElements.removeChild(item);
					i--; //nodeList is live.... removeal changes nodelist
				}
			}
			root.appendChild(doc.importNode(dataDoc.getDocumentElement(), true));
		}
	}

	private String serializeDocument(Document doc) throws IOException, TransformerException {
		ByteArrayOutputStream s = new ByteArrayOutputStream();

		TransformerFactory factory = TransformerFactory.newInstance();
		Transformer transformer = factory.newTransformer();
		transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
		transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$

		DOMSource source = new DOMSource(doc);
		StreamResult outputTarget = new StreamResult(s);
		transformer.transform(source, outputTarget);

		return s.toString("UTF8"); //$NON-NLS-1$			
	}
}

Back to the top