Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eafad62be207149d3f5c9de9d3a2b95d1eb1ee00 (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
/*****************************************************************************
 * Copyright (c) 2009, 2014 CEA LIST 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:
 *  Yann TANGUY (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - bug 440263
 *  
 *****************************************************************************/
package org.eclipse.papyrus.uml.tools.utils;

import java.util.Collection;

import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil;
import org.eclipse.osgi.util.NLS;
import org.eclipse.uml2.uml.ActivityEdge;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.AssociationClass;
import org.eclipse.uml2.uml.GeneralOrdering;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Pseudostate;
import org.eclipse.uml2.uml.Relationship;
import org.eclipse.uml2.uml.Transition;
import org.eclipse.uml2.uml.util.UMLSwitch;

import com.google.common.base.Optional;
import com.google.common.base.Strings;

/**
 * Utility class for <code>org.eclipse.uml2.uml.NamedElement</code><BR>
 */
public class NamedElementUtil {

	public static final String COPY_OF = "CopyOf";

	public static final String QUALIFIED_NAME_SEPARATOR = "::";

	private final static String PUBLIC_STRING = "+";

	private final static String PROTECTED_STRING = "#";

	private final static String PRIVATE_STRING = "-";

	private final static String PACKAGE_STRING = "~";

	private static final UMLSwitch<Boolean> IS_AUTONAMED = getIsAutoNamedSwitch();

	/**
	 * A helper method to calculate the max depth of an element
	 * 
	 * @param the
	 *            named element
	 * @return the maximum depth found in qualified name
	 */
	public static int getQualifiedNameMaxDepth(NamedElement namedElement) {
		int d = 0;
		String s = namedElement.getQualifiedName();
		if (s == null) {
			return 0;
		}
		int n = 0;
		while ((n = s.indexOf(QUALIFIED_NAME_SEPARATOR, n)) != -1) {
			n += 2;
			d++;
		}
		return d;
	}

	/**
	 * generate a default name for the eobject in parameter the format is :
	 * "eclassName"+"max(elementOfTheSameName in the container)" + 1
	 * 
	 * @param newElement
	 * @return
	 */
	public static String getDefaultNameWithIncrement(EObject newElement) {
		if (newElement.eContainer() != null) {
			return getDefaultNameWithIncrement(newElement, newElement.eContainer().eContents());
		}
		return null;
	}

	/**
	 * generate a default name for the eobject in parameter the format is :
	 * "eclassName"+"max(elementOfTheSameName in the container)" + 1 the method checks already
	 * existing element in contents parameter
	 * 
	 * @param newElement
	 */
	public static String getDefaultNameWithIncrement(EObject newElement, Collection<?> contents) {
		return getDefaultNameWithIncrement("", newElement, contents);
	}

	public static String getDefaultCopyNameWithIncrement(NamedElement namedElement, Collection<?> contents) {
		// A NamedElement with empty string for a name is logically unnamed, so any copy of it should also be unnamed
		String rootName = Strings.emptyToNull(namedElement.getName());
		if (rootName != null) {
			for (Object o : contents) {
				if (o instanceof EObject) {
					String name = EMFCoreUtil.getName((EObject) o);
					if (rootName.equals(name)) {
						String newName = NLS.bind(COPY_OF + "_{0}_", rootName);
						return NamedElementUtil.getDefaultNameWithIncrementFromBase(newName, contents);
					}
				}
			}
		}
		return rootName;
	}

	public static String getDefaultNameWithIncrement(String prefix, EObject newElement, Collection<?> contents) {
		if (prefix == null) {
			prefix = "";
		}
		return getDefaultNameWithIncrementFromBase(prefix + newElement.eClass().getName(), contents, newElement, "");
	}

	public static String getDefaultNameWithIncrementFromBase(String base, Collection<?> contents) {
		return getDefaultNameWithIncrementFromBase(base, contents, null, ""); //$NON-NLS-1$
	}

	public static String getDefaultNameWithIncrementFromBase(String base, Collection<?> contents, EObject elementToRename, String separator) {
		return (elementToRename != null) ? //
		getDefaultNameSwitch(base, contents, separator).doSwitch(elementToRename).orNull(): //
		computeDefaultNameWithIncrementFromBase(base, contents, elementToRename, separator);
	}

	private static UMLSwitch<Optional<String>> getDefaultNameSwitch(final String base, final Collection<?> contents, final String separator) {
		return new UMLSwitch<Optional<String>>() {

			@Override
			public Optional<String> defaultCase(EObject object) {
				return Optional.fromNullable(computeDefaultNameWithIncrementFromBase(base, contents, object, separator));
			}

			@Override
			public Optional<String> casePseudostate(Pseudostate object) {
				String base = object.getKind().getLiteral();
				base = base.substring(0, 1).toUpperCase() + base.substring(1);

				return Optional.fromNullable(computeDefaultNameWithIncrementFromBase(base, contents, object, separator));
			}

			@Override
			public Optional<String> caseRelationship(Relationship object) {
				return Optional.absent();
			}

			@Override
			public Optional<String> caseAssociation(Association object) {
				return Optional.absent();
			}

			public Optional<String> caseAssociationClass(AssociationClass object) {
				return defaultCase(object);
			}

			@Override
			public Optional<String> caseActivityEdge(ActivityEdge object) {
				return Optional.absent();
			}

			@Override
			public Optional<String> caseTransition(Transition object) {
				return Optional.absent();
			}

			@Override
			public Optional<String> caseGeneralOrdering(GeneralOrdering object) {
				return Optional.absent();
			}
		};
	}

	private static UMLSwitch<Boolean> getIsAutoNamedSwitch() {
		return new UMLSwitch<Boolean>() {

			@Override
			public Boolean defaultCase(EObject object) {
				return Boolean.TRUE;
			}

			@Override
			public Boolean caseRelationship(Relationship object) {
				return Boolean.FALSE;
			}

			@Override
			public Boolean caseAssociation(Association object) {
				return Boolean.FALSE;
			}

			public Boolean caseAssociationClass(AssociationClass object) {
				return Boolean.TRUE;
			}

			@Override
			public Boolean caseActivityEdge(ActivityEdge object) {
				return Boolean.FALSE;
			}

			@Override
			public Boolean caseTransition(Transition object) {
				return Boolean.FALSE;
			}

			@Override
			public Boolean caseGeneralOrdering(GeneralOrdering object) {
				return Boolean.FALSE;
			}
		};
	}

	public static boolean isAutoNamed(EObject element) {
		return IS_AUTONAMED.doSwitch(element);
	}

	static String computeDefaultNameWithIncrementFromBase(String base, Collection<?> contents, EObject elementToRename, String separator) {
		if (elementToRename != null) {
			// Is this even an element that we should auto-name?
			if (!isAutoNamed(elementToRename)) {
				return null;
			}

			// Do not change the name if it's already present in the contents collection and already has a name
			if (contents.contains(elementToRename)) {
				if (elementToRename instanceof ENamedElement) {
					ENamedElement eNamedElement = (ENamedElement) elementToRename;
					if (eNamedElement.getName() != null) {
						return eNamedElement.getName();
					}
				}
				// UML specific
				if (elementToRename instanceof NamedElement) {
					NamedElement namedElement = (NamedElement) elementToRename;
					if (namedElement.getName() != null) {
						return namedElement.getName();
					}
				}
			}
		}

		int nextNumber = 1;
		
		// specific value for properties. default name is attribute
		if ("property".equalsIgnoreCase(base)) {
			base = "attribute";
		}
		
		for (Object o : contents) {
			if (o instanceof EObject && o != elementToRename) {
				String name = EMFCoreUtil.getName((EObject) o);
				if (name != null && name.startsWith(base)) {
					String end = name.substring(base.length());
					int nextNumberTmp = 1;

					try {
						nextNumberTmp = Integer.parseInt(end) + 1;
					} catch (NumberFormatException ex) {
					}

					if (nextNumberTmp > nextNumber) {
						nextNumber = nextNumberTmp;
					}
				}
			}
		}

		return base + separator + nextNumber;
	}

	/**
	 * Give the visibility of the {@link NamedElement} as a string, as defined in the UML2 standard.
	 * 
	 * @return A String representing the visibility of the {@link NamedElement}. Possible values:
	 *         <ul>
	 *         <li>public: <code>"+"</code>
	 *         <li>private: <code>"-"</code>
	 *         <li>protected: <code>"#"</code>
	 *         <li>package: <code>"~"</code>
	 *         </ul>
	 */
	public static String getVisibilityAsSign(NamedElement element) {
		String vKindValue = "";

		switch (element.getVisibility().getValue()) {
		case org.eclipse.uml2.uml.VisibilityKind.PUBLIC:
			vKindValue = PUBLIC_STRING;
			break;
		case org.eclipse.uml2.uml.VisibilityKind.PRIVATE:
			vKindValue = PRIVATE_STRING;
			break;
		case org.eclipse.uml2.uml.VisibilityKind.PACKAGE:
			vKindValue = PACKAGE_STRING;
			break;
		case org.eclipse.uml2.uml.VisibilityKind.PROTECTED:
			vKindValue = PROTECTED_STRING;
			break;
		}
		return vKindValue;
	}

	/**
	 * Returns the name of an element, given its qualified name
	 * 
	 * @param qualifiedName
	 *            the qualified name of the element
	 * @return the name of the element. It shall never be <code>null</code>.
	 */
	public static String getNameFromQualifiedName(String qualifiedName) {
		String name = qualifiedName.substring(qualifiedName.lastIndexOf(NamedElement.SEPARATOR) + NamedElement.SEPARATOR.length());
		return (name != null) ? name : "";
	}

	/**
	 * 
	 * @param childQualifiedName
	 *            the qualifiedName of an element
	 * @return
	 *         the qualified name of its parent
	 */
	public static String getParentQualifiedName(final String childQualifiedName) {
		final String childName = getNameFromQualifiedName(childQualifiedName);
		final String parentQualifiedName = childQualifiedName.substring(0, childQualifiedName.length() - (NamedElement.SEPARATOR.length() + childName.length()));
		return (parentQualifiedName != null) ? parentQualifiedName : "";
	}


	public static String getName(NamedElement element) {
		if (element.getName() != null) {
			return element.getName();
		} else {
			return (NamedElementUtil.getDefaultNameWithIncrement(element));
		}
	}
}

Back to the top