Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 33a2169ca221b274d796816eda9728573d4fec43 (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
/*****************************************************************************
 * Copyright (c) 2009 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:
 *  Yann TANGUY (CEA LIST) yann.tanguy@cea.fr - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.uml.tools.utils;

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

import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Property;

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

	/**
	 * Get all properties that can be subset by this {@link Property} checks the type and the
	 * multiplicity.
	 *
	 * @param property
	 *        property for which the list of subsettable properties are made
	 * @param noCheck
	 *        set this parameter to <code>true</code> if multiplicity and type check should be
	 *        made for the computation
	 * @return all properties that can be subset
	 */
	public static List<Property> getSubsettablesProperties(Property property, boolean noCheck) {
		List<Property> list = new ArrayList<Property>();

		// subset properties:
		Iterator<NamedElement> it = property.getClass_().getMembers().iterator();
		while(it.hasNext()) {

			NamedElement element = it.next();
			if(element instanceof Property) {
				boolean isValid = true;
				Property subsettableProperty = (Property)element;

				// check it is not itself....
				if(subsettableProperty.equals(property)) {
					isValid = false;
				}

				// check types conformity
				if(!noCheck) {
					if(property.getType() != null && subsettableProperty.getType() != null) {
						if(!property.getType().conformsTo(subsettableProperty.getType())) {
							isValid = false;
						}
					} else {
						isValid = false;
					}

					// check multiplicity (only upper bound has an OCL rule)
					if((subsettableProperty.getUpper() != -1) && (property.getUpper() > subsettableProperty.getUpper())) {
						isValid = false;
					}
				}

				if(isValid) {
					list.add(subsettableProperty);
				}
			}
		}
		return list;
	}

	/**
	 * Find a subsetted property given its name and a context to find it.
	 *
	 * @param name
	 *        the name of the property
	 * @return the property found or <code>null</code> if the element was not found.
	 */
	// @unused
	public static Property findSusbsettedPropertyByName(String propertyName, Property property, boolean noCheck) {
		Iterator<Property> it = PropertyUtil.getSubsettablesProperties(property, true).iterator();
		while(it.hasNext()) {
			Property tmpProperty = it.next();
			String tmpPropertyName = tmpProperty.getName();
			if(tmpPropertyName != null && propertyName.equals(tmpPropertyName.trim())) {
				return tmpProperty;
			}
		}
		return null;
	}

	/**
	 * Get all properties that can be redefined by this {@link Property}.
	 *
	 * @return all properties that can be redefined
	 */
	public static List<Property> getRedefinableProperties(Property property) {
		List<Property> list = new ArrayList<Property>();

		// redefine-able properties:
		Iterator<NamedElement> it = property.getClass_().getInheritedMembers().iterator();
		while(it.hasNext()) {
			NamedElement element = it.next();
			if(element instanceof Property) {
				list.add((Property)element);
			}
		}

		// adds also already redefined members. In fact, when properties are
		// redefined, they
		// disappear from the inherited members list
		Iterator<Property> it2 = property.getRedefinedProperties().iterator();
		while(it2.hasNext()) {
			Property element = it2.next();
			list.add(element);
		}
		return list;
	}

	/**
	 * Find a redefined property given its name and a context to find it.
	 *
	 * @param name
	 *        the name of the property
	 * @return the property found or <code>null</code> if the element was not found.
	 */
	public static Property findRedefinedPropertyByName(String propertyName, Property property) {
		Iterator<Property> it = PropertyUtil.getRedefinableProperties(property).iterator();
		while(it.hasNext()) {
			Property tmpProperty = it.next();
			String tmpPropertyName = tmpProperty.getName();
			if(tmpPropertyName != null && propertyName.equals(tmpPropertyName.trim())) {
				return tmpProperty;
			}
		}
		return null;
	}

	/**
	 * Get the displayed string for the derived attribute of the property.
	 *
	 * @param property
	 *        the property
	 * @return If the property is derived, return "/". Otherwise return an empty String
	 */
	public static String getDerived(Property property) {
		return property.isDerived() ? "/" : "";
	}

	/**
	 * return the full label of the property, given UML2 specification.
	 *
	 * @return the string corresponding to the label of the property
	 */
	public static String getLabel(Property property) {
		StringBuffer buffer = new StringBuffer();
		// visibility
		buffer.append(" ");
		buffer.append(NamedElementUtil.getVisibilityAsSign(property));

		// derived property
		buffer.append(getDerived(property));

		// name
		buffer.append(" ");
		buffer.append(getName(property));

		// type
		if(property.getType() != null) {
			buffer.append(" : " + property.getType().getName());
		} else {
			buffer.append(" : " + TypeUtil.UNDEFINED_TYPE_NAME);
		}

		// multiplicity -> do not display [1]
		String multiplicity = MultiplicityElementUtil.getMultiplicityAsString(property);
		if(!multiplicity.trim().equals("[1]")) {
			buffer.append(multiplicity);
		}

		// default value
		if(property.getDefault() != null) {
			buffer.append(" = ");
			buffer.append(property.getDefault());
		}

		// property modifiers
		buffer.append(PropertyUtil.getModifiersAsString(property, false));

		return buffer.toString();
	}

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

	/**
	 * return the custom label of the property, given UML2 specification and a custom style.
	 *
	 * @param style
	 *        the collection of label fragments to display
	 *
	 * @return the string corresponding to the label of the property
	 */
	public static String getCustomLabel(Property property, Collection<String> style) {
		StringBuffer buffer = new StringBuffer();
		// visibility

		buffer.append(" ");
		if(style.contains(ICustomAppearence.DISP_VISIBILITY)) {
			buffer.append(NamedElementUtil.getVisibilityAsSign(property));
		}

		// derived property
		if(style.contains(ICustomAppearence.DISP_DERIVE)) {
			if(property.isDerived()) {
				buffer.append("/");
			}
		}
		// name
		if(style.contains(ICustomAppearence.DISP_NAME)) {
			buffer.append(" ");
			buffer.append(property.getName());
		}

		if(style.contains(ICustomAppearence.DISP_TYPE)) {
			// type
			if(property.getType() != null) {
				buffer.append(": " + property.getType().getName());
			} else {
				buffer.append(": " + TypeUtil.UNDEFINED_TYPE_NAME);
			}
		}

		if(style.contains(ICustomAppearence.DISP_MULTIPLICITY)) {
			// multiplicity -> do not display [1]
			String multiplicity = MultiplicityElementUtil.getMultiplicityAsString(property);
			buffer.append(multiplicity);
		}

		if(style.contains(ICustomAppearence.DISP_DEFAULT_VALUE)) {
			// default value
			if(property.getDefault() != null) {
				buffer.append(" = ");
				buffer.append(property.getDefault());
			}
		}

		if(style.contains(ICustomAppearence.DISP_MODIFIERS)) {
			boolean multiLine = style.contains(ICustomAppearence.DISP_MULTI_LINE);
			// property modifiers
			String modifiers = PropertyUtil.getModifiersAsString(property, multiLine);
			if(!modifiers.equals("")) {
				if(multiLine) {
					buffer.append("\n");
				}

				if(!buffer.toString().endsWith(" ")) {
					buffer.append(" ");
				}

				buffer.append(modifiers);
			}
		}
		return buffer.toString();
	}

	/**
	 * Returns the modifier of the property, separated by a comma, as as single line if <code>multiline</code> is <code>false</code> or as a multiline
	 * string if <code>multiline</code> is <code>false</code>.
	 *
	 * @param multiLine
	 *        boolean that indicates if the string should have several lines when set to <code>true</code> or only one line when set to
	 *        <code>false</code>.
	 *
	 * @return a string giving all modifiers for the property
	 */
	public static String getModifiersAsString(Property property, boolean multiLine) {
		StringBuffer buffer = new StringBuffer();
		boolean needsComma = false;
		String NL = (multiLine) ? "\n" : " ";

		// Return property modifiers
		if(property.isReadOnly()) {
			buffer.append("readOnly");
			needsComma = true;
		}
		if(property.isDerivedUnion()) {
			needsComma = updateModifiersString(buffer, needsComma, NL, "union");
		}
		if(property.isOrdered()) {
			needsComma = updateModifiersString(buffer, needsComma, NL, "ordered");;
		}
		if(property.isUnique()) {
			needsComma = updateModifiersString(buffer, needsComma, NL, "unique");
		}

		// is the property redefining another property ?
		for(Property current : property.getRedefinedProperties()) {
			needsComma = updateModifiersString(buffer, needsComma, NL, "redefines ");
			buffer.append(current.getName());
		}

		// is the property subsetting another property ?
		for(Property current : property.getSubsettedProperties()) {
			needsComma = updateModifiersString(buffer, needsComma, NL, "subsets ");
			buffer.append(current.getName());
		}

		if(!buffer.toString().equals("")) {
			buffer.insert(0, "{");
			buffer.append("}");
		}

		return buffer.toString();
	}

	/**
	 * Update the modifiers string
	 *
	 * @param buffer
	 *        the existing bufferString to append
	 * @param needsComma
	 *        if it needs coma
	 * @param NL
	 *        if it is multiline
	 * @param message
	 *        the message top
	 * @return true because the modifier string is no more empty
	 */
	private static boolean updateModifiersString(StringBuffer buffer, boolean needsComma, String NL, String message) {
		if(needsComma) {
			buffer.append(",");
			buffer.append(NL);
		}
		buffer.append(message);
		return true;
	}
}

Back to the top