Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4febccb91eec6112a60ab8c1998d2a2a986440cb (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
/*****************************************************************************
 * Copyright (c) 2009 ATOS ORIGIN.
 *
 *    
 * 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:
 *  Tristan FAURE (ATOS ORIGIN) tristan.faure@atosorigin.com - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.diagramprofile.utils;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Stereotype;

/**
 * The Class StereotypeUtils.
 */
public class StereotypeUtils {

	/** extension point ID to load profile */
	public static final String extensionPointIdForProfile = "org.eclipse.uml2.uml.generated_package";

	/** URI for profile */
	public static final String uriExtensionPointIdForProfile = "uri";

	/** location for profile */
	public static final String locationExtensionPointIdForProfile = "location";
	
	/** URI for SysML profile */
	public static final String SYSML_URI = "http://www.eclipse.org/papyrus/0.7.0/SysML";

	/**
	 * Get the base Element from a stereotype application.
	 * 
	 * @param e
	 *            , the stereotype application
	 * 
	 * @return the base element
	 */
	public static EObject getBaseElement(EObject e) {
		if (e != null) {
			for (EStructuralFeature f : e.eClass().getEAllStructuralFeatures()) {
				if (f.getName().startsWith("base_")) {
					Object b = e.eGet(f);
					if (b instanceof EObject && b != null) {
						return (EObject) b;
					}
				}
			}
		}
		return null;
	}

	/**
	 * Checks if is profile applied.
	 * 
	 * @param qualifiedName
	 *            the qualified name of the profile
	 * @param e
	 *            the element
	 * 
	 * @return true, if the profile is applied
	 */
	public static boolean isProfileApplied(String qualifiedName, Element e) {
		if (qualifiedName == null || e == null || qualifiedName.length() == 0) {
			return false;
		}
		Package p = e.getNearestPackage();
		for (Profile pr : p.getAllAppliedProfiles()) {
			if (qualifiedName.equals(pr.getQualifiedName())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Checks if the is stereotype applied.
	 * 
	 * @param qualifiedName
	 *            the qualified name of the stereotype
	 * @param e
	 *            the element to check
	 * 
	 * @return true, if the stereotype is applied
	 */
	public static boolean isStereotypeApplied(String qualifiedName, Element e) {
		if (qualifiedName == null || e == null || qualifiedName.length() == 0) {
			return false;
		}
		Stereotype s = e.getAppliedStereotype(qualifiedName);
		return s != null;
	}

	/**
	 * Load a profile from an URI
	 * 
	 * @return the profile
	 */
	public static Profile loadProfile(String uri, ResourceSet set) {
		String locationURI = "";
		Profile profile = null;
		IConfigurationElement[] extensions = Platform.getExtensionRegistry().getConfigurationElementsFor(
				extensionPointIdForProfile);
		for (IConfigurationElement e : extensions) {
			String extensionURI = e.getAttribute(uriExtensionPointIdForProfile);
			if (extensionURI.equals(uri)) {
				locationURI = e.getAttribute(locationExtensionPointIdForProfile);
				URI profileURI = URI.createURI(locationURI);
				if (set != null) {
					Resource resource = set.getResource(profileURI, true);
					for (EObject obj : resource.getContents()) {
						if (obj instanceof Profile) {
							profile = (Profile) obj;
							return profile;
						}
					}
				}
			}
		}
		return profile;
	}

	/**
	 * Apply a profile on a package, from an URI
	 */
	public static void applyProfile(String uri, Package element) {
		Profile profile = loadProfile(uri, element.eResource().getResourceSet());
		if (profile != null) {
			element.applyProfile(profile);
		}
	}
	
	/**
	 * Apply the SysML profile on the specified element
	 */
	public static void applySysMLProfile(Package element) {
		Profile profile = loadProfile(SYSML_URI, element.eResource().getResourceSet());
		if (profile != null) {
			element.applyProfile(profile);
		}
	}
	
	/**
	 * Apply stereotype to element.
	 * 
	 * @param element the element
	 * @param stereotypeName the stereotype qualified name
	 */
	public static void applyStereotypeToElement(Element element, String stereotypeName) {
		for (Stereotype stereotype : element.getApplicableStereotypes()) {
			if (stereotype.getQualifiedName().equals(stereotypeName)) {
				element.applyStereotype(stereotype);
			}
		}
	}

}

Back to the top