Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7cd797d16b75ceb8197205c2189795c6beb8d313 (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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*****************************************************************************
 * Copyright (c) 2011, 2016 CEA LIST, Christian W. Damus, and others.
 *
 * All rights reserved. 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *  Christian W. Damus - bug 485220
 *
 *****************************************************************************/
package org.eclipse.papyrus.eclipse.project.editors.project;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;

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.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.papyrus.eclipse.project.editors.Activator;
import org.eclipse.papyrus.eclipse.project.editors.interfaces.IFeatureProjectEditor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class FeatureProjectEditor extends ProjectEditor implements IFeatureProjectEditor {

	/**
	 * the name of the file feature.xml
	 * 
	 * @since 2.0
	 */
	public static final String FEATURE_XML_FILE = "feature.xml"; //$NON-NLS-1$

	private static final String FEATURE_NATURE = "org.eclipse.pde.FeatureNature"; //$NON-NLS-1$
	private static final String FEATURE_BUILDER = "org.eclipse.pde.FeatureBuilder"; //$NON-NLS-1$

	private static final String ID = "id"; //$NON-NLS-1$
	private static final String LABEL = "label"; //$NON-NLS-1$
	private static final String VERSION = "version"; //$NON-NLS-1$
	private static final String PROVIDER = "provider-name"; //$NON-NLS-1$

	private static final String URL = "url"; //$NON-NLS-1$
	private static final String COPYRIGHT = "copyright"; //$NON-NLS-1$
	private static final String LICENSE = "license"; //$NON-NLS-1$
	private static final String DESCRIPTION = "description"; //$NON-NLS-1$

	private static final String OS = "os"; //$NON-NLS-1$
	private static final String WS = "ws"; //$NON-NLS-1$
	private static final String NL = "nl"; //$NON-NLS-1$
	private static final String ARCH = "arch"; //$NON-NLS-1$
	private static final String UPDATE = "update"; //$NON-NLS-1$

	private static final String PLUGIN = "plugin"; //$NON-NLS-1$
	private static final String IMPORT = "import"; //$NON-NLS-1$
	private static final String INCLUDES = "includes"; //$NON-NLS-1$
	private static final String REQUIRES = "requires"; //$NON-NLS-1$
	private static final String FEATURE = "feature"; //$NON-NLS-1$

	// TODO pour l'externalization : utiliser l'�diteur de Properties! dans java Utils

	private Document featureXML;

	private IFile featureFile;

	private Element featureRoot;

	/**
	 * Constructor.
	 *
	 * @param project
	 *            the eclipse project
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 * @throws CoreException
	 */
	public FeatureProjectEditor(final IProject project) throws ParserConfigurationException, SAXException, IOException, CoreException {
		super(project);
	}

	@Override
	public void init() {
		featureFile = getFeature();
		if ((featureFile != null) && featureFile.exists()) {
			final DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
			try {
				DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
				featureXML = documentBuilder.parse(featureFile.getLocation().toOSString());
				featureRoot = featureXML.getDocumentElement();
			} catch (final ParserConfigurationException e) {
				Activator.log.error(e);
			} catch (final SAXException e) {
				Activator.log.error(e);
			} catch (final IOException e) {
				Activator.log.error(e);
			}
		}
	}

	@Override
	public void createFiles(final Set<String> files) {
		if (files.contains(FEATURE_XML_FILE)) {
			featureFile = getProject().getFile(FEATURE_XML_FILE);
			if (!featureFile.exists()) {
				InputStream content = getInputStream("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<feature>\n</feature>\n\n"); //$NON-NLS-1$

				try {
					featureFile.create(content, true, null);
				} catch (CoreException e) {
					Activator.log.error(e);
				}
			}
		}
	}

	@Override
	public boolean exists() {
		return getFeature().exists() && super.exists();
	}

	private void setAttribute(final String attributeName, final String attributeValue) {
		setAttribute(featureRoot, attributeName, attributeValue);
	}

	private void setAttribute(final Element element, final String attributeName, final String attributeValue) {
		if (!Objects.equals(element.getAttribute(attributeName), attributeValue)) {
			touch();
			element.setAttribute(attributeName, attributeValue);
		}
	}

	private void setTextContent(Element element, String text) {
		if (!Objects.equals(element.getTextContent(), text)) {
			touch();
			element.setTextContent(text);
		}
	}

	/**
	 * @return the feature.xml file if it exists
	 */
	private IFile getFeature() {
		final IFile result = getProject().getFile(FEATURE_XML_FILE);

		if (result.exists()) {
			return result;
		}

		return null;
	}

	/**
	 * @since 2.0
	 */
	@Override
	protected void doSave() {
		if ((featureFile != null) && featureFile.exists()) {
			try {
				final TransformerFactory transformerFactory = TransformerFactory.newInstance();
				final Transformer transformer = transformerFactory.newTransformer();
				transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
				final StreamResult result = new StreamResult(new StringWriter());
				final DOMSource source = new DOMSource(featureXML);
				transformer.transform(source, result);

				final InputStream inputStream = getInputStream(result.getWriter().toString());
				featureFile.setContents(inputStream, true, true, null);
			} catch (final TransformerException ex) {
				Activator.log.error(ex);
			} catch (final CoreException ex) {
				Activator.log.error(ex);
			}
		}
		super.doSave();
	}

	@Override
	public Set<String> getMissingNature() {
		Set<String> result = super.getMissingNature();
		if (!hasNature(FEATURE_NATURE)) {
			result.add(FEATURE_NATURE);
		}
		return result;
	}

	@Override
	public Set<String> getMissingFiles() {
		Set<String> result = super.getMissingFiles();
		IFile feature = getProject().getFile(FEATURE_XML_FILE);
		if (!feature.exists()) {
			result.add(FEATURE_XML_FILE);
		}
		return result;
	}

	@Override
	public Set<String> getMissingBuildCommand() {
		Set<String> result = super.getMissingBuildCommand();
		if (!hasBuildCommand(FEATURE_BUILDER)) {
			result.add(FEATURE_BUILDER);
		}
		return result;
	}

	@Override
	public Document getDocument() {
		return featureXML;
	}

	private String getAttribute(String name) {
		return getAttribute(featureRoot, name);
	}

	private String getAttribute(Element element, String name) {
		return element.getAttribute(name);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public String getId() {
		return getAttribute(ID);
	}

	@Override
	public String getLabel() {
		return getAttribute(LABEL);
	}

	@Override
	public String getVersion() {
		return getAttribute(VERSION);
	}

	@Override
	public String getProviderName() {
		return getAttribute(PROVIDER);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public String getDescriptionText() {
		Element description = getNodeChild(DESCRIPTION, featureRoot);
		return (description == null) ? null : description.getTextContent().trim();
	}

	/**
	 * @since 2.0
	 */
	@Override
	public String getDescriptionURL() {
		Element description = getNodeChild(DESCRIPTION, featureRoot);
		return (description == null) || !description.hasAttribute(URL)
				? null
				: description.getAttribute(URL);
	}

	@Override
	public String getCopyrightURL() {
		final Element copyrightNode = getNode(COPYRIGHT);
		if (copyrightNode != null) {
			final String value = copyrightNode.getAttribute("url");
			if (value != null && value.startsWith("%")) {
				final IFile file = getProject().getFile("feature.properties");
				final Properties prop = new Properties(); // TODO create a method to use Properties for others fields too
				try {
					prop.load(file.getContents());
				} catch (final IOException e) {
					Activator.log.error(e);
				} catch (final CoreException e) {
					Activator.log.error(e);
				}
				final Object val = prop.get("url");
				if (val != null) {
					return (String) val;
				}
			}
			return copyrightNode.getAttribute("url");
		}
		return null;
	}

	@Override
	public String getCopyrightText() {
		final Element copyrightNode = getNode(COPYRIGHT);

		return copyrightNode != null ? copyrightNode.getTextContent() : null;
	}

	/**
	 * @since 2.0
	 */
	@Override
	public String getLicenseText() {
		Element license = getNodeChild(LICENSE, featureRoot);
		return (license == null)
				? null
				: license.getTextContent().trim();
	}

	/**
	 * @since 2.0
	 */
	@Override
	public String getLicenseURL() {
		Element license = getNodeChild(LICENSE, featureRoot);
		return (license == null) || !license.hasAttribute(URL)
				? null
				: license.getAttribute(URL);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public String getOS() {
		return getAttribute(OS);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public String getWS() {
		return getAttribute(WS);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public String getNL() {
		return getAttribute(NL);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public String getArch() {
		return getAttribute(ARCH);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public void setId(final String id) {
		setAttribute(ID, id);
	}

	@Override
	public void setLabel(final String label) {
		setAttribute(LABEL, label);
	}

	@Override
	public void setVersion(final String version) {
		setAttribute(VERSION, version);
	}

	@Override
	public void setProviderName(final String providerName) {
		setAttribute(PROVIDER, providerName);
	}

	private Element forceElement(String name) {
		return forceElement(featureRoot, name);
	}

	private Element forceElement(Element parent, String name) {
		Element result = getNodeChild(name, parent);
		if (result == null) {
			touch();
			result = createElement(name);
			parent.appendChild(result);
		}
		return result;
	}

	@Override
	public void setDescription(final String descriptionURL, final String description) {
		if (featureRoot != null) {
			Element extension = forceElement(DESCRIPTION);
			setAttribute(extension, URL, descriptionURL);
			setTextContent(extension, description);
		}
	}

	@Override
	public void setCopyright(final String copyrightURL, final String copyrightDesc) {
		setURLNode(COPYRIGHT, copyrightURL, copyrightDesc);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public void setLicense(final String licenseURL, final String licenseDesc) {
		setURLNode(LICENSE, licenseURL, licenseDesc);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public void setOS(final String os) {
		setAttribute(OS, os);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public void setWS(final String ws) {
		setAttribute(WS, ws);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public void setNL(final String nl) {
		setAttribute(NL, nl);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public void setArch(final String architecture) {
		setAttribute(ARCH, architecture);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public void setUpdateURL(final String urlLabel, final String url) {
		Element urlNode = forceElement(URL);
		Element updateNode = forceElement(urlNode, UPDATE);

		setAttribute(updateNode, LABEL, urlLabel);
		setAttribute(updateNode, URL, url);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public void addPlugin(final String pluginName) {
		// Get the plug-in element or create it if it does not exist
		Element plugin = forcePlugin(pluginName);
		if (!plugin.hasAttribute(VERSION)) {
			plugin.setAttribute(VERSION, "0.0.0"); //$NON-NLS-1$
		}
		if (!plugin.hasAttribute("download-size")) {
			plugin.setAttribute("download-size", "0"); //$NON-NLS-1$
		}
		if (!plugin.hasAttribute("install-size")) {
			plugin.setAttribute("install-size", "0"); //$NON-NLS-1$
		}
		if (!plugin.hasAttribute("unpack")) {
			plugin.setAttribute("unpack", "false"); //$NON-NLS-1$
		}
	}

	/**
	 * @since 2.0
	 */
	@Override
	public void addRequiredFeature(final String featureName, final String version) {
		// Get or create the required feature element
		Element feature = forceRequiredFeature(featureName);

		// Set the element values
		feature.setAttribute(VERSION, version);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public void addRequiredPlugin(final String pluginName) {
		// Get or create the plug-in element
		forceRequiredPlugin(pluginName);
	}

	/**
	 * @since 2.0
	 */
	@Override
	public void addInclude(final String featureName, final String version) {
		Element includeNode = forceInclude(featureName);
		setAttribute(includeNode, VERSION, (version == null) ? "0.0.0" : version);
	}

	/**
	 * Creates an element and returns it.
	 *
	 * @param elementName
	 *            the name of the element to create
	 * @return the created element
	 */
	private Element createElement(String elementName) {
		return featureXML.createElement(elementName);
	}

	protected void setURLNode(final String nodeName, final String url, final String description) {
		if (featureRoot != null) {
			Element urlNode = forceElement(nodeName);
			if (url != null) {
				setAttribute(urlNode, URL, url);
			}
			setTextContent(urlNode, description);
		}
	}

	/**
	 * Gets an element inside a parent element.
	 *
	 * @param parentElement
	 * @param nodeName
	 *            the node name of the element
	 * @param attributeValue
	 *            the value of the element's attribute to retrieve
	 * @return the element or null if it does not exist
	 */
	private Element getElement(final Element parentElement, final String nodeName, final String attributeName, final String attributeValue) {
		NodeList childNodes = parentElement.getChildNodes();

		for (int i = 0; i < childNodes.getLength(); i++) {
			Node item = childNodes.item(i);

			if (nodeName.equals(item.getNodeName())) {
				if (attributeValue.equals(getNodeAttribute(item, attributeName))) {
					if (item instanceof Element) {
						return (Element) item;
					}
				}
			}
		}

		return null;
	}

	private Element getNodeChild(final String childName, final Element node) {
		NodeList childNodes = node.getChildNodes();

		if (childNodes == null) {
			return null;
		}

		for (int i = 0; i < childNodes.getLength(); i++) {
			Node item = childNodes.item(i);

			if (item.getNodeName().equals(childName)) {
				if (item instanceof Element) {
					return (Element) item;
				}
			}
		}

		return null;
	}

	/**
	 * Gets a node element inside the root element.
	 *
	 * @param nodeName
	 *            the node name
	 * @return the node element or null if it does not exist.
	 */
	private Element getNode(final String nodeName) {
		if (featureRoot != null) {
			final NodeList nodes = featureRoot.getChildNodes();
			for (int i = 0; i < nodes.getLength(); i++) {
				final Node item = nodes.item(i);
				if (item instanceof NodeList) {
					final String name = item.getNodeName();
					if (name.equals(nodeName)) {
						if (item instanceof Element) {
							return (Element) item;
						}
					}
				}
			}
		}

		return null;
	}

	private Element getPlugin(String pluginName) {
		return getElement(featureRoot, PLUGIN, ID, pluginName);
	}

	private Element forcePlugin(String pluginName) {
		Element result = getPlugin(pluginName);
		if (result == null) {
			result = createElement(PLUGIN);
			featureRoot.appendChild(result);
			setAttribute(result, ID, pluginName);
		}
		return result;
	}

	private Element getInclude(String featureName) {
		return getElement(featureRoot, INCLUDES, ID, featureName);
	}

	private Element forceInclude(String featureName) {
		Element result = getInclude(featureName);
		if (result == null) {
			result = createElement(INCLUDES);
			featureRoot.appendChild(result);
			setAttribute(result, ID, featureName);
		}
		return result;
	}

	/**
	 * @param pluginName
	 * @return
	 */
	private Element getRequiredPlugin(String pluginName) {
		Element requires = getNode(REQUIRES);

		if (requires != null) {
			return getElement(requires, IMPORT, PLUGIN, pluginName);
		}

		return null;
	}

	private Element forceRequiredPlugin(String pluginName) {
		Element result = getRequiredPlugin(pluginName);

		if (result == null) {
			result = createElement(IMPORT);
			forceElement(REQUIRES).appendChild(result);
			setAttribute(result, PLUGIN, pluginName);
		}

		return result;
	}

	private String getNodeAttribute(Node node, String name) {
		Node attribute = node.getAttributes().getNamedItem(name);

		return attribute != null ? attribute.getNodeValue() : null;
	}

	private Element getRequiredFeature(String featureName) {
		Element requires = getNode(REQUIRES);

		if (requires != null) {
			return getElement(requires, IMPORT, FEATURE, featureName);
		}

		return null;
	}

	private Element forceRequiredFeature(String featureName) {
		Element result = getRequiredFeature(featureName);

		if (result == null) {
			result = createElement(IMPORT);
			forceElement(REQUIRES).appendChild(result);
			setAttribute(result, FEATURE, featureName);
		}

		return result;
	}

}

Back to the top