Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 147bab875fdd83af62e51cfbc280efdf45c7c32a (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
/*****************************************************************************
 * Copyright (c) 2008 CEA LIST.
 *
 * 
 * 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:
 *  Chokri Mraidha (CEA LIST) Chokri.Mraidha@cea.fr - Initial API and implementation
 *  Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - modification
 *  Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - add getNearestProfileApplication
 *****************************************************************************/
package org.eclipse.papyrus.uml.profile.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.papyrus.uml.profile.Activator;
import org.eclipse.papyrus.uml.profile.Message;
import org.eclipse.papyrus.uml.profile.definition.IPapyrusVersionConstants;
import org.eclipse.papyrus.uml.profile.definition.PapyrusDefinitionAnnotation;
import org.eclipse.papyrus.uml.profile.definition.Version;
import org.eclipse.uml2.uml.Comment;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.EnumerationLiteral;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.LiteralBoolean;
import org.eclipse.uml2.uml.LiteralInteger;
import org.eclipse.uml2.uml.LiteralString;
import org.eclipse.uml2.uml.LiteralUnlimitedNatural;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageImport;
import org.eclipse.uml2.uml.PrimitiveType;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.ProfileApplication;
import org.eclipse.uml2.uml.Slot;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.StructuralFeature;
import org.eclipse.uml2.uml.TemplateBinding;
import org.eclipse.uml2.uml.TemplateSignature;
import org.eclipse.uml2.uml.TemplateableElement;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.ValueSpecification;
import org.eclipse.uml2.uml.util.UMLUtil;


/**
 * Some utils extracted from com.cea.utils classes (package and element)
 * TODO: not all are used, cleanup with respect to types
 */
public class Util {

	/**
	 * Return a usable string label for passed object.
	 * 
	 * @param object
	 *        the object
	 * 
	 * @return the label
	 */
	public static String getLabel(Object object, boolean shortLabel) {
		String label = "";

		if(object == null) {
			return "undefined";
		}

		if(object instanceof ValueSpecification) {
			label = getLabel((ValueSpecification)object);

		} else if(object instanceof Element) {
			Element cE = (Element)object;
			String cName = null;
			String suffix = "";
			String cComLabel = "";

			NamedElement cNE = null;
			if(object instanceof NamedElement) {
				cNE = (NamedElement)object;

			} else if(object instanceof PackageImport) {
				PackageImport cPI = (PackageImport)object;
				suffix = " (PackageImport)";
				cNE = cPI.getImportedPackage();

			} else if(object instanceof ElementImport) {
				ElementImport cEI = (ElementImport)object;
				suffix = " (ElementImport)";
				cNE = cEI.getImportedElement();

			} else if(object instanceof ProfileApplication) {
				ProfileApplication cPA = (ProfileApplication)object;
				suffix = " (ProfileApplication)";
				cNE = cPA.getAppliedProfile();

			} else if(object instanceof Comment) {
				Comment cCom = (Comment)object;
				suffix = " (Comment)";
				String cComBody = cCom.getBody();
				if(cComBody.length() >= 10) {
					cComLabel = cComBody.substring(0, 9) + "...";
				} else {
					cComLabel = cComBody;
				}
			} else if(object instanceof TemplateSignature) {
				TemplateableElement te = ((TemplateSignature)object).getTemplate();
				suffix = " (TemplateSignature owner)";
				if(te instanceof NamedElement) {
					cNE = (NamedElement)te;
				}
			} else if(object instanceof TemplateBinding) {
				TemplateableElement te = ((TemplateBinding)object).getBoundElement();
				suffix = " (TemplateBinding bound-element)";
				if(te instanceof NamedElement) {
					cNE = (NamedElement)te;
				}
			}
			if(shortLabel) {
				if(object instanceof Comment) {
					cName = cComLabel;
				} else if(cNE != null) {
					cName = cNE.getName();
				}
			} else {
				if(object instanceof Comment) {
					cName = cComLabel + suffix;
				} else if(cNE != null) {
					cName = cNE.getQualifiedName() + suffix;
				}
			}

			if(cName != null) {
				label = cName;
			} else {
				label = cE.toString();
			}
		}

		return label;
	}


	/**
	 * Check if a type is a metaclass.
	 * 
	 * @param type
	 *        to check
	 * 
	 * @return true if type is metaclass, else false
	 */
	public static boolean isMetaclass(Type type) {
		boolean isMetaclass = false;

		if((type instanceof org.eclipse.uml2.uml.Class) && (type.getAppliedStereotypes() != null) && (type.getAppliedStereotypes().size() > 0)) {

			Stereotype firstStereotype = type.getAppliedStereotypes().get(0);

			if(firstStereotype.getName().equals("Metaclass")) {
				isMetaclass = true;
			}
		}
		return isMetaclass;
	}

	/**
	 * Convert the list of element in a list of string after some basic checks.
	 * 
	 * @param elements
	 *        the elements
	 * 
	 * @return the string array from list
	 */
	public static String[] getStringArrayFromList(List<Element> elements) {

		ArrayList<String> tmp = new ArrayList<String>();

		// if no possible selection : abort
		if(elements.size() == 0) {
			return null;
		}

		// First create tmp list
		Iterator<Element> it = elements.iterator();
		while(it.hasNext()) {
			Object current = it.next();

			String label = getLabel(current, false);
			tmp.add(label);
		}

		// if no possible selection : abort
		if(tmp.size() == 0) {
			return null;
		}

		// Put this in array
		String[] elementsNames = new String[tmp.size()];
		for(int i = 0; i < tmp.size(); i++) {
			elementsNames[i] = tmp.get(i);
		}

		// PostCondition
		if(elementsNames.length != elements.size()) {
			Message.error("Number of elements in name list does not match number or elements.");
			elementsNames = null;
		}
		return elementsNames;
	}

	/**
	 * Treat a string provided by the user for a primitiveType and return
	 * the corresponding object.
	 * 
	 * @param dialogValue
	 *        user input string
	 * @param type
	 *        expected
	 * 
	 * @return object
	 */
	public static Object getValueObjectFromString(String dialogValue, Type type) {
		Object newValue = null;
		String typeName = type.getQualifiedName();

		try {
			if(type instanceof PrimitiveType || ((type instanceof DataType) && !(type instanceof Enumeration))) {
				if(typeName.equals("UMLPrimitiveTypes::Integer")) {
					newValue = Integer.valueOf(dialogValue);

				} else if(typeName.equals("UMLPrimitiveTypes::UnlimitedNatural")) {
					if(Integer.signum(Integer.parseInt(dialogValue)) != -1) {
						newValue = Integer.valueOf(dialogValue);

					} else {
						Message.warning("UnlimitedNatural must be positive or null.");
					}

				} else if(typeName.equals("UMLPrimitiveTypes::String")) {
					newValue = dialogValue;

				} else if(typeName.equals("UMLPrimitiveTypes::Boolean")) {
					newValue = Boolean.valueOf(dialogValue);

				} else { // user defined primitive type
					newValue = dialogValue;
				}

			} else if(type instanceof Enumeration) {
				Enumeration enumeration = (Enumeration)type;
				EnumerationLiteral literal = enumeration.getOwnedLiteral(dialogValue);

				if(literal != null) {
					newValue = literal;
				}
			}
		} catch (NumberFormatException e) {
			Message.warning("Bad format number.");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return newValue;
	}

	/**
	 * Helper function used by getInstancesFilteredByType
	 */
	private static void checkAndAddElement(EObject currentEObj, @SuppressWarnings("rawtypes") Class metaType, Stereotype appliedStereotype, ArrayList<Element> filteredElements) {
		if(currentEObj instanceof Element) {
			Element piCurrentElt = (Element)currentEObj;
			if(appliedStereotype != null) {

				// It is not sufficient to call getAppliedStereotypes, since we also want to
				// retrieve elements that apply a sub-stereotype
				if(piCurrentElt.getAppliedSubstereotype(appliedStereotype, null) != null) {
					filteredElements.add(piCurrentElt);
				}
				Iterator<Stereotype> appStIter = piCurrentElt.getAppliedStereotypes().iterator();
				while(appStIter.hasNext()) {
					Stereotype currentSt = appStIter.next();

					if(currentSt.conformsTo(appliedStereotype)) {
						filteredElements.add(piCurrentElt);
					}
				}

			} else { // if (appliedStereotype == null)
				if(metaType.isInstance(piCurrentElt)) {
					filteredElements.add(piCurrentElt);
				}

				/** add imported meta elements */
				else if(piCurrentElt instanceof ElementImport) {
					Iterator<EObject> eIter = ((ElementImport)piCurrentElt).getImportedElement().eAllContents();
					while(eIter.hasNext()) {
						EObject currentEIelt = eIter.next();
						if((currentEIelt instanceof Element) && (metaType.isInstance(currentEIelt))) {
							filteredElements.add((Element)currentEIelt);
						}
					}
				}
			}
		}
	}

	/**
	 * Retrieve an ArrayList of all instances in the model that
	 * are instances of the java.lang.Class metaType or with a
	 * stereotype applied
	 * 
	 * @param metaType
	 *        selected classes
	 * @param model
	 *        to check
	 * @param appliedStereotype
	 *        may be null, metatype is ignored if not null
	 * @return an arraylist containing the selected instances
	 */
	public static ArrayList<Element> getInstancesFilteredByType(Element element, @SuppressWarnings("rawtypes") Class metaType, Stereotype appliedStereotype) {
		// retrieve parent element
		Package topPackage = Util.topPackage(element);
		Assert.isNotNull(topPackage, "Top package should not be null for element " + element);
		Iterator<EObject> iter = topPackage.eAllContents();
		ArrayList<Element> filteredElements = new ArrayList<Element>();

		while(iter.hasNext()) {
			EObject currentEObj = iter.next();

			// If currentElt is an ElementImport, it is replaced by the imported
			// Element.
			if(currentEObj instanceof ElementImport) {
				ElementImport elementImport = (ElementImport)currentEObj;
				currentEObj = elementImport.getImportedElement();
			}

			/* package imports treatment */
			else if(currentEObj instanceof PackageImport) {
				Package importedPkg = ((PackageImport)currentEObj).getImportedPackage();
				if(importedPkg != null) {
					Iterator<EObject> piIter = importedPkg.eAllContents();
					while(piIter.hasNext()) {
						EObject piCurrentEObj = piIter.next();
						checkAndAddElement(piCurrentEObj, metaType, appliedStereotype, filteredElements);
					}
				}
			}

			// Filtering elements
			checkAndAddElement(currentEObj, metaType, appliedStereotype, filteredElements);
		}

		return filteredElements;
	}

	/**
	 * Reorder stereotype applications.
	 * 
	 * @param element
	 *        the element
	 * @param stereotypes
	 *        the stereotypes
	 */
	public static void reorderStereotypeApplications(Element element, EList<Stereotype> stereotypes) {
		for(Iterator<Stereotype> s = stereotypes.iterator(); s.hasNext();) {
			EObject stereotypeApplication = element.getStereotypeApplication(s.next());
			if(stereotypeApplication != null) {
				UMLUtil.setBaseElement(stereotypeApplication, null);
				UMLUtil.setBaseElement(stereotypeApplication, element);
				Resource eResource = stereotypeApplication.eResource();
				if(eResource != null) {
					EList<EObject> contents = eResource.getContents();
					contents.move(contents.size() - 1, stereotypeApplication);
				}
			}
		}
	}

	/**
	 * Return the labe for a value specification
	 * 
	 * @param object
	 * @return
	 */
	public static String getLabel(ValueSpecification value) {
		String label = "";

		if(value instanceof LiteralBoolean) {
			label = "<LiteralBoolean> ";
		} else if(value instanceof LiteralInteger) {
			label = "<LiteralInteger> ";
		} else if(value instanceof LiteralString) {
			label = "<LiteralString> ";
		} else if(value instanceof LiteralUnlimitedNatural) {
			label = "<LiteralUnlimitedNatural> ";
		} else {
			label = "<ValueSpecification> ";
		}

		// Add the value
		label = label + value.stringValue();

		return label;
	}

	/**
	 * Return the label for a value specification
	 * 
	 * @param object
	 * @return
	 */
	public static String getOriginLabel(ValueSpecification value) {
		String label = getLabel(value);

		Element owner = value.getOwner();
		// Add parent qualified name when possible
		if(owner instanceof NamedElement) {
			NamedElement ownerNE = (NamedElement)value.getOwner();
			if(ownerNE.isSetName()) {
				label = label + " (" + ownerNE.getQualifiedName() + ")";
			}
		} else if(owner instanceof Slot) {
			Slot ownerSlot = (Slot)owner;
			InstanceSpecification ownerISpec = ownerSlot.getOwningInstance();
			StructuralFeature definingFeature = ownerSlot.getDefiningFeature();
			label = label + " (" + ownerISpec.getQualifiedName() + "::" + definingFeature.getLabel() + ")";

		} // else nothing

		return label;
	}

	/**
	 * This method is used to look for the top package that contains this element.
	 * 
	 * @param element
	 *        the element for which top package must be returned
	 * @return the top package
	 */
	public static Package topPackage(Element element) {
		if(element.getOwner() == null) {
			return (Package)element;
		} else {
			return topPackage(element.getOwner());
		}
	}

	/**
	 * Returns every PapyrusDefinitionAnnotation corresponding to the version of profile definitions
	 * 
	 * @param profile
	 *        the profile to study
	 * @return every eAnnotations associated to the profile definitions or an empty list if no
	 *         eAnnotation exists for given profile
	 */
	public static List<PapyrusDefinitionAnnotation> getAllPapyrusDefinitionAnnotation(Profile profile) {
		List<PapyrusDefinitionAnnotation> definitions = new ArrayList<PapyrusDefinitionAnnotation>();
		Iterator<EAnnotation> it = getAllPapyrusVersionAnnotation(profile).iterator();
		while(it.hasNext()) {
			definitions.add(PapyrusDefinitionAnnotation.parseEAnnotation(it.next()));
		}
		return definitions;
	}

	/**
	 * Returns every eAnnotations associated to the profile definitions
	 * 
	 * @param profile
	 *        the profile to study
	 * @return every eAnnotations associated to the profile definitions or an empty list if no
	 *         eAnnotation exists for given profile
	 */
	public static List<EAnnotation> getAllPapyrusVersionAnnotation(Profile profile) {
		List<EAnnotation> annotations = new ArrayList<EAnnotation>();
		EAnnotation definitions = profile.getEAnnotation("http://www.eclipse.org/uml2/2.0.0/UML");
		if(definitions == null) {
			return annotations;
		}

		Iterator<EObject> it = definitions.getContents().iterator();
		while(it.hasNext()) {
			// content are EPackage
			EPackage object = (EPackage)it.next();
			EAnnotation annotation = object.getEAnnotation(IPapyrusVersionConstants.PAPYRUS_EANNOTATION_SOURCE);
			if(annotation != null) {
				annotations.add(annotation);
			}
		}
		return annotations;
	}

	/**
	 * Returns the eAnnotation associated to the current profile definition
	 * 
	 * @return the eAnnotation associated to the current profile definition or <code>null</code> if no
	 *         eAnnotation exists for given profile
	 */
	public static EAnnotation getPapyrusVersionAnnotation(Profile profile) {
		if(profile.getDefinition() != null) {
			return profile.getDefinition().getEAnnotation(IPapyrusVersionConstants.PAPYRUS_EANNOTATION_SOURCE);
		}
		return null;
	}

	/**
	 * Returns the version of the current profile definition
	 * 
	 * @param profile
	 *        the profile to check
	 * @return the version of the current profile definition
	 */
	public static Version getProfileDefinitionVersion(Profile profile) {
		EAnnotation annotation = getPapyrusVersionAnnotation(profile);
		if(annotation == null) {
			return Version.emptyVersion;
		} else {
			// retrieve the version from the annotation
			Version version;
			String value = "";
			try {
				value = annotation.getDetails().get(IPapyrusVersionConstants.PAPYRUS_VERSION_KEY);
				version = new Version((value != null) ? value : "");
			} catch (Exception e) {
				Activator.logWarning("impossible to parse the version value: " + value);
				version = Version.emptyVersion;
			}
			return version;
		}
	}

	/**
	 * Returns the author of the current profile definition
	 * 
	 * @param profile
	 *        the profile to check
	 * @return the author of the current profile definition or the empty string if none was given
	 */
	public static String getProfileDefinitionAuthor(Profile profile) {
		EAnnotation annotation = getPapyrusVersionAnnotation(profile);
		if(annotation == null) {
			return "";
		} else {
			// retrieve the version from the annotation
			final String value = annotation.getDetails().get(IPapyrusVersionConstants.PAPYRUS_AUTHOR_KEY);
			return (value != null) ? value : "";
		}
	}

	/**
	 * Returns the date of the current profile definition
	 * 
	 * @param profile
	 *        the profile to check
	 * @return the date of the current profile definition or the empty string if none was given
	 */
	public static String getProfileDefinitionDate(Profile profile) {
		EAnnotation annotation = getPapyrusVersionAnnotation(profile);
		if(annotation == null) {
			return "";
		} else {
			// retrieve the version from the annotation
			final String value = annotation.getDetails().get(IPapyrusVersionConstants.PAPYRUS_DATE_KEY);
			return (value != null) ? value : "";
		}
	}

	/**
	 * Returns the comment of the current profile definition
	 * 
	 * @param profile
	 *        the profile to check
	 * @return the comment of the current profile definition or the empty string if none was given
	 */
	public static String getProfileDefinitionComment(Profile profile) {
		EAnnotation annotation = getPapyrusVersionAnnotation(profile);
		if(annotation == null) {
			return "";
		} else {
			// retrieve the version from the annotation
			final String value = annotation.getDetails().get(IPapyrusVersionConstants.PAPYRUS_COMMENT_KEY);
			return (value != null) ? value : "";
		}
	}

	/**
	 * Returns the copyright of the current profile definition
	 * 
	 * @param profile
	 *        the profile to check
	 * @return the copyright of the current profile definition or the empty string if none was given
	 */
	public static String getProfileDefinitionCopyright(Profile profile) {
		EAnnotation annotation = getPapyrusVersionAnnotation(profile);
		if(annotation == null) {
			return "";
		} else {
			// retrieve the version from the annotation
			final String value = annotation.getDetails().get(IPapyrusVersionConstants.PAPYRUS_COPYRIGHT_KEY);
			return (value != null) ? value : "";
		}
	}

	/**
	 * We look for the nearest profile application from the Element, owning the stereotype;
	 * 
	 * @param stereotype
	 * @return
	 *         the nearest profile application owning the stereotype
	 */
	public static ProfileApplication getNearestProfileApplicationFor(final Element element, final Stereotype stereotype) {
		ProfileApplication profileApplication = null;
		Profile profile = stereotype.getProfile();
		EObject container = element;
		while(profileApplication == null && container != null) {
			if(container instanceof Package) {
				profileApplication = ((Package)container).getProfileApplication(profile);
			}
			container = container.eContainer();
		}
		return profileApplication;
	}
}

Back to the top