Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e8512597290f328eddbd790420ec8bae3cbd1872 (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
/*****************************************************************************
 * Copyright (c) 2013, 2019 CEA LIST.
 *
 *
 * 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
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Bug 545575
 *****************************************************************************/
package org.eclipse.papyrus.infra.ui.converter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EEnumLiteral;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.infra.emf.utils.EMFContants;
import org.eclipse.papyrus.infra.tools.util.BooleanHelper;
import org.eclipse.papyrus.infra.tools.util.TypesConstants;
import org.eclipse.papyrus.infra.ui.Activator;

/**
 * Value solver for EMF
 *
 * WARNING : incomplete implementations
 *
 * @author Vincent Lorenzo
 * @since 1.2
 *
 */

public class EMFStringValueConverter extends AbstractStringValueConverter {



	/**
	 * Context used for the resolution of the string
	 */
	private EObject resolutionContext;

	/**
	 * The separator used for multivalue
	 */
	protected final String multiValueSeparator;

	/**
	 *
	 * Constructor.
	 *
	 * @param resolutionContext
	 *            the context used for the resolution of the string
	 */
	public EMFStringValueConverter(final EObject resolutionContext, final String multiValueSeparator) {
		this.resolutionContext = resolutionContext;
		this.multiValueSeparator = multiValueSeparator;
	}


	/**
	 *
	 * @return
	 * 		the context to use for the resolution
	 */
	public EObject getResolutionContext() {
		return resolutionContext;
	}

	/**
	 *
	 * @see org.eclipse.ui.services.IDisposable#dispose()
	 *
	 */
	@Override
	public void dispose() {
		this.resolutionContext = null;
	}

	/**
	 *
	 * @param resolutionContext
	 *            the table context
	 * @param feature
	 *            the feature
	 * @param valueAsString
	 *            the pasted string for this feature
	 * @return
	 * 		the value for the pasted string or <code>null</code> if not found
	 */
	@Override
	protected ConvertedValueContainer<?> doDeduceValueFromString(final Object feature, final String valueAsString) {
		final EClassifier featureType = getFeatureType(feature);
		if (feature instanceof EStructuralFeature) {
			return deduceValueFromString(feature, featureType, valueAsString);
		}
		return null;
	}

	/**
	 *
	 * @param feature
	 * @param featureType
	 * @param valueAsString
	 * @return
	 */
	protected ConvertedValueContainer<?> deduceValueFromString(final Object feature, final EClassifier featureType, final String valueAsString) {
		ConvertedValueContainer<?> realValue = null;
		final int upperbound = getFeatureUpperBound(feature);
		boolean isMany = (upperbound > 1 || upperbound == -1);
		if (featureType instanceof EDataType) {
			if (featureType instanceof EEnum) {
				realValue = deduceEEnumLiteralValue((EEnum) featureType, isMany, valueAsString);
			}
			final String typeName = featureType.getName();
			if (TypesConstants.STRING.equals(typeName) || EMFContants.ESTRING.equals(typeName)) {
				realValue = deduceStringValue(isMany, valueAsString);
			} else if (EMFContants.EBOOLEAN.equals(typeName) || TypesConstants.BOOLEAN.equals(typeName)) {
				realValue = deduceBooleanValue(isMany, valueAsString);
			} else if (EMFContants.EINT.equals(typeName) || TypesConstants.INTEGER.equals(typeName)) {
				realValue = deduceIntValue(isMany, valueAsString);
			} else if (EMFContants.EDOUBLE.equals(typeName)) {
				realValue = deduceDoubleValue(isMany, valueAsString);
			}
		} else if (featureType instanceof EClass) {
			realValue = deduceEObjectValue(getResolutionContext(), feature, (EClass) featureType, isMany, valueAsString);
		}
		return realValue;
	}

	protected int getFeatureUpperBound(final Object feature) {
		return ((EStructuralFeature) feature).getUpperBound();
	}



	/**
	 *
	 * @param resolutionContext
	 *            the context used for the resolution
	 * @param feature
	 *            the feature
	 * @param featureType
	 *            the type of the feature
	 * @param isMany
	 *            <code>true</code> if the feature isMany
	 * @param valueAsString
	 *            the string value to resolve
	 * @return
	 * 		a value container referencing the eobject represented by the string
	 * @throws StringValueSolverException
	 */
	protected ConvertedValueContainer<?> deduceEObjectValue(EObject resolutionContext, Object feature, EClass featureType, boolean isMany, String valueAsString) {
		if (valueAsString == null || valueAsString.equals("")) {
			return new ConvertedValueContainer<EObject>(null, Status.OK_STATUS);
		}
		final IStatus status = new StringValueConverterStatus(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(NO_X_REPRESENTED_BY_Y_HAVE_BEEN_FOUND, featureType.getName(), valueAsString), Collections.singleton(valueAsString));
		return new ConvertedValueContainer<EObject>(null, status);
	}

	/**
	 *
	 * @param feature
	 *            an object representing a feature
	 * @return
	 * 		the type of the feature
	 */
	protected EClassifier getFeatureType(final Object feature) {
		final EClassifier featureType;
		if (feature instanceof EStructuralFeature) {
			return ((EStructuralFeature) feature).getEType();
		} else {
			featureType = null;
		}
		return featureType;
	}

	/**
	 *
	 * @param eenum
	 *            the enumeration
	 * @param isMany
	 *            <code>true</code> if the feature is many
	 * @param valueAsString
	 *            the value to convert
	 * @return
	 * 		the converted value
	 */
	protected ConvertedValueContainer<?> deduceEEnumLiteralValue(final EEnum eenum, final boolean isMany, final String valueAsString) {
		ConvertedValueContainer<?> returnedValue = null;
		IStatus iStatus = Status.OK_STATUS;
		final Collection<String> unresolvedValues = new ArrayList<String>();
		if (isMany) {
			final Collection<EEnumLiteral> values = new ArrayList<EEnumLiteral>();
			for (final String str : valueAsString.split(this.multiValueSeparator)) {
				final EEnumLiteral literal = eenum.getEEnumLiteral(str);
				if (literal != null) {
					values.add(literal);
				} else {
					unresolvedValues.add(str);
				}
			}
			if (!unresolvedValues.isEmpty()) {
				iStatus = new StringValueConverterStatus(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(SOME_STRING_ARE_NOT_VALID_TO_CREATE_X, EMFContants.EENUM_LITERAL), unresolvedValues);
			}
			returnedValue = new MultiConvertedValueContainer<EEnumLiteral>(values, iStatus);
		} else {
			final EEnumLiteral literal = eenum.getEEnumLiteral(valueAsString);
			if (literal != null) {
				// returnedValue = new ConvertedValueContainer<EEnumLiteral>(literal, iStatus);
				// fix a bug on enumerator
				returnedValue = new ConvertedValueContainer<Enumerator>(literal.getInstance(), iStatus);
			} else {
				unresolvedValues.add(valueAsString);
				iStatus = new StringValueConverterStatus(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(THE_STRING_X_IS_NOT_VALID_TO_CREATE_Y, valueAsString, EMFContants.EENUM_LITERAL), unresolvedValues);
				returnedValue = new ConvertedValueContainer<Boolean>(null, iStatus);
			}
		}
		return returnedValue;
	}

	/**
	 *
	 * @param isMany
	 *            <code>true</code> if the feature isMany
	 * @param valueAsString
	 *            the value to parse
	 * @return
	 * 		the result of the parsing
	 */
	protected ConvertedValueContainer<?> deduceBooleanValue(final boolean isMany, final String valueAsString) {
		ConvertedValueContainer<?> returnedValue = null;
		IStatus iStatus = Status.OK_STATUS;
		final Collection<String> unresolvedValues = new ArrayList<String>();
		if (isMany) {
			final Collection<Boolean> values = new ArrayList<Boolean>();
			for (final String str : valueAsString.split(this.multiValueSeparator)) {
				if (BooleanHelper.isBoolean(str)) {
					values.add(Boolean.valueOf(valueAsString));
				} else {
					unresolvedValues.add(str);
				}
			}
			if (!unresolvedValues.isEmpty()) {
				iStatus = new StringValueConverterStatus(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(SOME_STRING_ARE_NOT_VALID_TO_CREATE_X, TypesConstants.BOOLEAN), unresolvedValues);
			}
			returnedValue = new MultiConvertedValueContainer<Boolean>(values, iStatus);
		} else {
			if (BooleanHelper.isBoolean(valueAsString)) {
				returnedValue = new ConvertedValueContainer<Boolean>(Boolean.valueOf(valueAsString), iStatus);
			} else {
				unresolvedValues.add(valueAsString);
				iStatus = new StringValueConverterStatus(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(THE_STRING_X_IS_NOT_VALID_TO_CREATE_Y, valueAsString, TypesConstants.BOOLEAN), unresolvedValues);
				returnedValue = new ConvertedValueContainer<Boolean>(null, iStatus);
			}
		}
		return returnedValue;
	}

	/**
	 *
	 * @param isMany
	 *            <code>true</code> if the feature isMany
	 * @param valueAsString
	 *            the value to parse
	 * @return
	 * 		the result of the parsing
	 */
	protected ConvertedValueContainer<?> deduceDoubleValue(final boolean isMany, final String valueAsString) {
		ConvertedValueContainer<?> returnedValue = null;
		IStatus iStatus = Status.OK_STATUS;
		final Collection<String> unresolvedValues = new ArrayList<String>();
		if (isMany) {
			final Collection<Double> values = new ArrayList<Double>();
			for (final String str : valueAsString.split(this.multiValueSeparator)) {
				final Double value = Double.valueOf(str);
				if (value != null) {
					values.add(value);
				} else {
					unresolvedValues.add(str);
				}
			}
			if (!unresolvedValues.isEmpty()) {
				iStatus = new StringValueConverterStatus(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(SOME_STRING_ARE_NOT_VALID_TO_CREATE_X, TypesConstants.DOUBLE), unresolvedValues);
			}
			returnedValue = new MultiConvertedValueContainer<Double>(values, iStatus);
		} else {
			try {
				returnedValue = new ConvertedValueContainer<Double>(Double.valueOf(valueAsString), iStatus);
			} catch (final NumberFormatException e) {
				unresolvedValues.add(valueAsString);
				iStatus = new StringValueConverterStatus(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(THE_STRING_X_IS_NOT_VALID_TO_CREATE_Y, valueAsString, TypesConstants.DOUBLE), unresolvedValues);
				returnedValue = new ConvertedValueContainer<Boolean>(null, iStatus);
			}
		}
		return returnedValue;
	}

	/**
	 *
	 * @param isMany
	 *            <code>true</code> if the feature isMany
	 * @param valueAsString
	 *            the value to parse
	 * @return
	 * 		the result of the parsing
	 */
	protected ConvertedValueContainer<?> deduceIntValue(final boolean isMany, final String valueAsString) {
		ConvertedValueContainer<?> returnedValue = null;
		IStatus iStatus = Status.OK_STATUS;
		final Collection<String> unresolvedValues = new ArrayList<String>();
		if (isMany) {
			final Collection<Integer> values = new ArrayList<Integer>();
			for (final String str : valueAsString.split(this.multiValueSeparator)) {
				try {
					values.add(Integer.valueOf(str));
				} catch (final NumberFormatException e) {
					unresolvedValues.add(str);
				}
			}
			if (!unresolvedValues.isEmpty()) {
				iStatus = new StringValueConverterStatus(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(SOME_STRING_ARE_NOT_VALID_TO_CREATE_X, TypesConstants.INTEGER), unresolvedValues);
			}
			returnedValue = new MultiConvertedValueContainer<Integer>(values, iStatus);
		} else {
			try {
				returnedValue = new ConvertedValueContainer<Integer>(Integer.valueOf(valueAsString), iStatus);
			} catch (final NumberFormatException e) {
				unresolvedValues.add(valueAsString);
				iStatus = new StringValueConverterStatus(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(THE_STRING_X_IS_NOT_VALID_TO_CREATE_Y, valueAsString, TypesConstants.INTEGER), unresolvedValues);
				returnedValue = new ConvertedValueContainer<Boolean>(null, iStatus);
			}
		}
		return returnedValue;
	}


	/**
	 *
	 * @param isMany
	 *            <code>true</code> if the feature is many
	 * @param valueAsString
	 *            the value as string
	 * @return
	 * 		the value container with the real value(s)
	 */
	protected ConvertedValueContainer<?> deduceStringValue(final boolean isMany, final String valueAsString) {
		ConvertedValueContainer<?> returnedValue = null;
		final IStatus iStatus = Status.OK_STATUS;
		if (isMany) {
			final Collection<String> values = new ArrayList<String>();
			for (final String str : valueAsString.split(this.multiValueSeparator)) {
				values.add(str);
			}
			returnedValue = new MultiConvertedValueContainer<String>(values, iStatus);
		} else {
			returnedValue = new ConvertedValueContainer<String>(valueAsString, iStatus);
		}
		return returnedValue;
	}

}

Back to the top