Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cea5bcfd521b053d64df506103ed9815b8518a96 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 Obeo.
 * 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:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.m2m.atl.adt.ui.editor.formatter.objects;

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

import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.text.BadLocationException;

/**
 * A formatted object.
 * 
 * @author <a href="mailto:william.piers@obeo.fr">William Piers</a>
 */
public class FormattedObject {

	public static final int TYPE_UNKNOWN = -1;

	public static final int TYPE_MODULE = 0;

	public static final int TYPE_OCL_MODEL = 1;

	public static final int TYPE_HELPER = 2;

	public static final int TYPE_MATCHED_RULE = 3;

	public static final int TYPE_OPERATOR_CALL_EXP = 4;

	public static final int TYPE_PARAMETER = 5;

	public static final int TYPE_BINDING = 6;

	public static final int TYPE_IN_PATTERN = 7;

	public static final int TYPE_OUT_PATTERN = 8;

	public static final int TYPE_COLLECTION_TYPE = 9;

	public static final int TYPE_OPERATION = 10;

	public static final int TYPE_ATTRIBUTE = 11;

	public static final int TYPE_TUPLE_EXP = 12;

	public static final int TYPE_COLLECTION_EXP = 13;

	public static final int TYPE_COLLECTION_OPERATION_CALL_EXP = 14;

	public static final int TYPE_NAVIGATION_OR_ATTRIBUTE_CALL_EXP = 15;

	public static final int TYPE_STRING_EXP = 16;

	public static final int TYPE_LIBRARY = 17;

	public static final int TYPE_QUERY = 18;

	public static final int TYPE_LAZY_MATCHED_RULE = 19;

	public static final int TYPE_ABSTRACT_MATCHED_RULE = 20;

	public static final int TYPE_ITERATOR_EXP = 21;

	public static final int TYPE_ITERATE_EXP = 22;

	public static final int TYPE_CALLED_RULE = 23;

	public static final int TYPE_RULE_VARIABLE_DECLARATION = 24;

	public static final int TYPE_OPERATION_CALL_EXP = 25;

	public static final int TYPE_IF_EXP = 26;

	public static final int TYPE_FOR_EXP = 27;

	public static final int TYPE_ACTION_BLOCK = 28;

	public static final int TYPE_OCL_MODEL_ELEMENT = 29;

	public static final int TYPE_SIMPLE_IN_PATTERN = 30;

	public static final int TYPE_LONE_OBJECT = 31;

	public static final int TYPE_LET_EXP = 32;

	public static final int TYPE_SIMPLE_OUT_PATTERN_ELEMENT = 33;

	public static final int TYPE_FOR_EACH_OUT_PATTERN_ELEMENT = 34;

	public static final int TYPE_OCL_UNDEFINED = 35;

	public static final int LINE_TYPE_NORMAL = 0;

	public static final int LINE_TYPE_HELPER_COMMENT = 1;

	public static final int LINE_TYPE_COMMENT = 2;

	public static final int LINE_TYPE_STRING = 3;

	public static final String REPLACEMENT = "OBJECT"; //$NON-NLS-1$

	public static final String RPLCMT_RGX = "(?:\\(\\s*)?" + REPLACEMENT + "\\{\\d+\\}(?:\\s*\\))?"; //$NON-NLS-1$ //$NON-NLS-2$

	public static final String RPLCMT_RGX_GRPNM = "(?:\\(\\s*)?" + REPLACEMENT + "\\{(\\d+)\\}(?:\\s*\\))?"; //$NON-NLS-1$ //$NON-NLS-2$

	private static int count = 0;

	protected String text;

	protected EObject eObject;

	protected int indentationLevel;

	protected int type;

	protected int number;

	protected List<FormattedObject> children;

	public FormattedObject(EObject eObject, String text) throws BadLocationException {
		this.eObject = eObject;
		this.text = text;
		indentationLevel = 0;
		type = typeOf(eObject);
		number = count++;
		children = new ArrayList<FormattedObject>();
	}

	public FormattedObject(EObject eObject, String text, int indentationLevel) throws BadLocationException {
		this(eObject, text);
		this.indentationLevel = indentationLevel;
	}

	public FormattedObject getChild(int i) throws BadLocationException {
		EObject child = eObject.eContents().get(i);
		return new FormattedObject(child, ""); //$NON-NLS-1$
	}

	public void addChildren(FormattedObject... fo) {
		for (FormattedObject child : fo) {
			children.add(child);
		}
	}

	/**
	 * Gives the type of the given EObject.
	 * 
	 * @param obj
	 *            the object we want to know the type.
	 * @return an integer value of the type of the element.
	 * @throws BadLocationException
	 */
	public static int typeOf(EObject obj) throws BadLocationException {
		if (oclIsTypeOf(obj, "Module")) //$NON-NLS-1$
			return TYPE_MODULE;
		else if (oclIsTypeOf(obj, "OclModel")) //$NON-NLS-1$
			return TYPE_OCL_MODEL;
		else if (oclIsTypeOf(obj, "Helper")) //$NON-NLS-1$
			return TYPE_HELPER;
		else if (oclIsTypeOf(obj, "MatchedRule")) //$NON-NLS-1$
			return TYPE_MATCHED_RULE;
		else if (oclIsTypeOf(obj, "LazyMatchedRule")) //$NON-NLS-1$
			return TYPE_LAZY_MATCHED_RULE;
		else if (oclIsTypeOf(obj, "AbstractMatchedRule")) //$NON-NLS-1$
			return TYPE_ABSTRACT_MATCHED_RULE;
		else if (oclIsTypeOf(obj, "CalledRule")) //$NON-NLS-1$
			return TYPE_CALLED_RULE;
		else if (oclIsTypeOf(obj, "OperatorCallExp")) //$NON-NLS-1$
			return TYPE_OPERATOR_CALL_EXP;
		else if (oclIsTypeOf(obj, "Parameter") || //$NON-NLS-1$
				oclIsTypeOf(obj, "TupleTypeAttribute")) //$NON-NLS-1$
			return TYPE_PARAMETER;
		else if (oclIsTypeOf(obj, "Binding")) //$NON-NLS-1$
			return TYPE_BINDING;
		else if (oclIsTypeOf(obj, "InPattern")) //$NON-NLS-1$
			return TYPE_IN_PATTERN;
		else if (oclIsTypeOf(obj, "OutPattern")) //$NON-NLS-1$
			return TYPE_OUT_PATTERN;
		else if (oclIsTypeOf(obj, "SequenceType") || //$NON-NLS-1$
				oclIsTypeOf(obj, "TupleType") || //$NON-NLS-1$
				oclIsTypeOf(obj, "SetType") || //$NON-NLS-1$
				oclIsTypeOf(obj, "BagType") || //$NON-NLS-1$
				oclIsTypeOf(obj, "OrderedSetType") || //$NON-NLS-1$
				oclIsTypeOf(obj, "MapType") || //$NON-NLS-1$
				oclIsTypeOf(obj, "CollectionType")) //$NON-NLS-1$
			return TYPE_COLLECTION_TYPE;
		else if (oclIsTypeOf(obj, "Operation")) //$NON-NLS-1$
			return TYPE_OPERATION;
		else if (oclIsTypeOf(obj, "Attribute")) //$NON-NLS-1$
			return TYPE_ATTRIBUTE;
		else if (oclIsTypeOf(obj, "TupleExp")) //$NON-NLS-1$
			return TYPE_TUPLE_EXP;
		else if (oclIsTypeOf(obj, "SequenceExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "TupleExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "SetExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "BagExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "OrderedSetExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "MapExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "CollectionExp")) //$NON-NLS-1$
			return TYPE_COLLECTION_EXP;
		else if (oclIsTypeOf(obj, "CollectionOperationCallExp")) //$NON-NLS-1$
			return TYPE_COLLECTION_OPERATION_CALL_EXP;
		else if (oclIsTypeOf(obj, "OperationCallExp")) //$NON-NLS-1$
			return TYPE_OPERATION_CALL_EXP;
		else if (oclIsTypeOf(obj, "NavigationOrAttributeCallExp")) //$NON-NLS-1$
			return TYPE_NAVIGATION_OR_ATTRIBUTE_CALL_EXP;
		else if (oclIsTypeOf(obj, "StringExp")) //$NON-NLS-1$
			return TYPE_STRING_EXP;
		else if (oclIsTypeOf(obj, "Library")) //$NON-NLS-1$
			return TYPE_LIBRARY;
		else if (oclIsTypeOf(obj, "Query")) //$NON-NLS-1$
			return TYPE_QUERY;
		else if (oclIsTypeOf(obj, "IteratorExp")) //$NON-NLS-1$
			return TYPE_ITERATOR_EXP;
		else if (oclIsTypeOf(obj, "IterateExp")) //$NON-NLS-1$
			return TYPE_ITERATE_EXP;
		else if (oclIsTypeOf(obj, "RuleVariableDeclaration")) //$NON-NLS-1$
			return TYPE_RULE_VARIABLE_DECLARATION;
		else if (oclIsTypeOf(obj, "IfExp")) //$NON-NLS-1$
			return TYPE_IF_EXP;
		else if (oclIsTypeOf(obj, "ForStat")) //$NON-NLS-1$
			return TYPE_FOR_EXP;
		else if (oclIsTypeOf(obj, "ActionBlock")) //$NON-NLS-1$
			return TYPE_ACTION_BLOCK;
		else if (oclIsTypeOf(obj, "OclModelElement")) //$NON-NLS-1$
			return TYPE_OCL_MODEL_ELEMENT;
		else if (oclIsTypeOf(obj, "SimpleInPatternElement")) //$NON-NLS-1$
			return TYPE_SIMPLE_IN_PATTERN;
		else if (oclIsTypeOf(obj, "BooleanExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "VariableExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "IntegerExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "RealExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "SetExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "BagExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "MapExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "OrderedSetExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "CollectionExp") || //$NON-NLS-1$
				oclIsTypeOf(obj, "StringType") || //$NON-NLS-1$
				oclIsTypeOf(obj, "IntegerType") || //$NON-NLS-1$
				oclIsTypeOf(obj, "RealType") || //$NON-NLS-1$
				oclIsTypeOf(obj, "BooleanType") || //$NON-NLS-1$
				oclIsTypeOf(obj, "OclAnyType") || //$NON-NLS-1$
				oclIsTypeOf(obj, "Iterator")) //$NON-NLS-1$
			return TYPE_LONE_OBJECT; // None formatted objects, but wrapped anyway
		else if (oclIsTypeOf(obj, "LetExp")) //$NON-NLS-1$
			return TYPE_LET_EXP;
		else if (oclIsTypeOf(obj, "SimpleOutPatternElement")) //$NON-NLS-1$
			return TYPE_SIMPLE_OUT_PATTERN_ELEMENT;
		else if (oclIsTypeOf(obj, "ForEachOutPatternElement")) //$NON-NLS-1$
			return TYPE_FOR_EACH_OUT_PATTERN_ELEMENT;
		else if (oclIsTypeOf(obj, "OclUndefinedExp")) //$NON-NLS-1$
			return TYPE_OCL_UNDEFINED;
		return TYPE_UNKNOWN;
	}

	private static boolean oclIsTypeOf(EObject obj, String type) {
		return obj.eClass().getName().equals(type);
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public EObject getEObject() {
		return eObject;
	}

	public void setEObject(EObject eObject) {
		this.eObject = eObject;
	}

	public int getIndentationLevel() {
		return indentationLevel;
	}

	public void setIndentationLevel(int indentationLevel) {
		this.indentationLevel = indentationLevel;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public List<FormattedObject> getChildren() {
		return children;
	}

	public int getNumber() {
		return number;
	}

	public String getReplacement() {
		return REPLACEMENT + "{" + number + "}"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	public static void resetCounter() {
		count = 0;
	}

	public String toString() {
		return text;
	}

}

Back to the top