ctf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / event / CtfTmfEventField.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 * Alexandre Montplaisir - Initial API and implementation, extend TmfEventField
12 * Bernd Hufmann - Add Enum field handling
13 * Geneviève Bastien - Add Struct and Variant field handling
14 * Jean-Christian Kouame - Correct handling of unsigned integer fields
15 * François Doray - Add generic array field type
16 *******************************************************************************/
17
18 package org.eclipse.tracecompass.tmf.ctf.core.event;
19
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.List;
24
25 import org.eclipse.tracecompass.ctf.core.event.types.AbstractArrayDefinition;
26 import org.eclipse.tracecompass.ctf.core.event.types.CompoundDeclaration;
27 import org.eclipse.tracecompass.ctf.core.event.types.Definition;
28 import org.eclipse.tracecompass.ctf.core.event.types.EnumDefinition;
29 import org.eclipse.tracecompass.ctf.core.event.types.FloatDefinition;
30 import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
31 import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
32 import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
33 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration;
34 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
35 import org.eclipse.tracecompass.ctf.core.event.types.StringDefinition;
36 import org.eclipse.tracecompass.ctf.core.event.types.VariantDefinition;
37 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
38 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
39 import org.eclipse.tracecompass.tmf.ctf.core.CtfEnumPair;
40
41 /**
42 * The CTF implementation of the TMF event field model
43 *
44 * @version 2.0
45 * @author Matthew Khouzam
46 * @author Alexandre Montplaisir
47 */
48 public abstract class CtfTmfEventField extends TmfEventField {
49
50 // ------------------------------------------------------------------------
51 // Constructor
52 // ------------------------------------------------------------------------
53
54 /**
55 * Standard constructor. Only to be used internally, call parseField() to
56 * generate a new field object.
57 *
58 * @param name
59 * The name of this field
60 * @param value
61 * The value of this field. Its type should match the field type.
62 * @param fields
63 * The children fields. Useful for composite fields
64 */
65 protected CtfTmfEventField(String name, Object value, ITmfEventField[] fields) {
66 super(/* Strip the underscore from the field name if there is one */
67 name.startsWith("_") ? name.substring(1) : name, //$NON-NLS-1$
68 value,
69 fields);
70 }
71
72 // ------------------------------------------------------------------------
73 // Operations
74 // ------------------------------------------------------------------------
75
76 /**
77 * Factory method to instantiate CtfTmfEventField objects.
78 *
79 * @param fieldDef
80 * The CTF Definition of this event field
81 * @param fieldName
82 * String The name to assign to this field
83 * @return The resulting CtfTmfEventField object
84 * @deprecated use {@link CtfTmfEventField#parseField(IDefinition, String)}
85 */
86 @Deprecated
87 public static CtfTmfEventField parseField(Definition fieldDef,
88 String fieldName) {
89 return parseField((IDefinition) fieldDef, fieldName);
90 }
91
92 /**
93 * Factory method to instantiate CtfTmfEventField objects.
94 *
95 * @param fieldDef
96 * The CTF Definition of this event field
97 * @param fieldName
98 * String The name to assign to this field
99 * @return The resulting CtfTmfEventField object
100 */
101 public static CtfTmfEventField parseField(IDefinition fieldDef,
102 String fieldName) {
103 CtfTmfEventField field = null;
104
105 /* Determine the Definition type */
106 if (fieldDef instanceof IntegerDefinition) {
107 IntegerDefinition intDef = (IntegerDefinition) fieldDef;
108 int base = intDef.getDeclaration().getBase();
109 field = new CTFIntegerField(fieldName, intDef.getValue(), base, intDef.getDeclaration().isSigned());
110
111 } else if (fieldDef instanceof EnumDefinition) {
112 EnumDefinition enumDef = (EnumDefinition) fieldDef;
113 field = new CTFEnumField(fieldName, new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue()));
114
115 } else if (fieldDef instanceof StringDefinition) {
116 field = new CTFStringField(fieldName, ((StringDefinition) fieldDef).getValue());
117
118 } else if (fieldDef instanceof FloatDefinition) {
119 FloatDefinition floatDef = (FloatDefinition) fieldDef;
120 field = new CTFFloatField(fieldName, floatDef.getValue());
121
122 } else if (fieldDef instanceof AbstractArrayDefinition) {
123 AbstractArrayDefinition arrayDef = (AbstractArrayDefinition) fieldDef;
124 IDeclaration decl = arrayDef.getDeclaration();
125 if (!(decl instanceof CompoundDeclaration)) {
126 throw new IllegalArgumentException("Array definitions should only come from sequence or array declarations"); //$NON-NLS-1$
127 }
128 CompoundDeclaration arrDecl = (CompoundDeclaration) decl;
129 IDeclaration elemType = null;
130 Collection<Definition> definitions = arrayDef.getDefinitions();
131 elemType = arrDecl.getElementType();
132 if (elemType instanceof IntegerDeclaration) {
133 /*
134 * Array of integers => CTFIntegerArrayField, unless it's a
135 * CTFStringField
136 */
137 IntegerDeclaration elemIntType = (IntegerDeclaration) elemType;
138 /* Are the integers characters and encoded? */
139 if (elemIntType.isCharacter()) {
140 /* it's a CTFStringField */
141 field = new CTFStringField(fieldName, arrayDef.toString());
142 } else {
143 /* it's a CTFIntegerArrayField */
144 int size = arrayDef.getDefinitions().size();
145 long[] values = new long[size];
146 for (int i = 0; i < size; i++) {
147 IDefinition elem = arrayDef.getDefinitions().get(i);
148 if (elem == null) {
149 break;
150 }
151 values[i] = ((IntegerDefinition) elem).getValue();
152 }
153 field = new CTFIntegerArrayField(fieldName, values,
154 elemIntType.getBase(),
155 elemIntType.isSigned());
156 }
157 } else {
158 /* Arrays of elements of any other type */
159 CtfTmfEventField[] elements = new CtfTmfEventField[arrayDef.getLength()];
160 /* Parse the elements of the array. */
161 int i = 0;
162 for (IDefinition definition : definitions) {
163 CtfTmfEventField curField = CtfTmfEventField.parseField(
164 definition, fieldName + '[' + i + ']');
165 elements[i] = curField;
166 i++;
167 }
168
169 field = new CTFArrayField(fieldName, elements);
170 }
171 } else if (fieldDef instanceof ICompositeDefinition) {
172 ICompositeDefinition strDef = (ICompositeDefinition) fieldDef;
173
174 List<ITmfEventField> list = new ArrayList<>();
175 /* Recursively parse the fields */
176 for (String curFieldName : strDef.getFieldNames()) {
177 list.add(CtfTmfEventField.parseField(strDef.getDefinition(curFieldName), curFieldName));
178 }
179 field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()]));
180
181 } else if (fieldDef instanceof VariantDefinition) {
182 VariantDefinition varDef = (VariantDefinition) fieldDef;
183
184 String curFieldName = varDef.getCurrentFieldName();
185 IDefinition curFieldDef = varDef.getCurrentField();
186 if (curFieldDef != null) {
187 CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
188 field = new CTFVariantField(fieldName, subField);
189 } else {
190 /* A safe-guard, but curFieldDef should never be null */
191 field = new CTFStringField(curFieldName, ""); //$NON-NLS-1$
192 }
193
194 } else {
195 /*
196 * Safe-guard, to avoid null exceptions later, field is expected not
197 * to be null
198 */
199 field = new CTFStringField(fieldName, Messages.CtfTmfEventField_UnsupportedType + fieldDef.getClass().toString());
200 }
201 return field;
202 }
203
204 @Override
205 public String toString() {
206 return getName() + '=' + getFormattedValue();
207 }
208
209 }
210
211 /**
212 * The CTF field implementation for integer fields.
213 *
214 * @author alexmont
215 */
216 final class CTFIntegerField extends CtfTmfEventField {
217
218 private final int fBase;
219 private final boolean fSigned;
220
221 /**
222 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
223 * Java parser this is interpreted as a long.
224 *
225 * @param name
226 * The name of this field
227 * @param longValue
228 * The integer value of this field
229 * @param signed
230 * Is the value signed or not
231 */
232 CTFIntegerField(String name, long longValue, int base, boolean signed) {
233 super(name, longValue, null);
234 fSigned = signed;
235 fBase = base;
236 }
237
238 @Override
239 public Long getValue() {
240 return (Long) super.getValue();
241 }
242
243 @Override
244 public String getFormattedValue() {
245 return IntegerDefinition.formatNumber(getValue(), fBase, fSigned);
246 }
247
248 }
249
250 /**
251 * The CTF field implementation for string fields
252 *
253 * @author alexmont
254 */
255 final class CTFStringField extends CtfTmfEventField {
256
257 /**
258 * Constructor for CTFStringField.
259 *
260 * @param strValue
261 * The string value of this field
262 * @param name
263 * The name of this field
264 */
265 CTFStringField(String name, String strValue) {
266 super(name, strValue, null);
267 }
268
269 @Override
270 public String getValue() {
271 return (String) super.getValue();
272 }
273 }
274
275 /**
276 * CTF field implementation for arrays of integers.
277 *
278 * @author alexmont
279 */
280 final class CTFIntegerArrayField extends CtfTmfEventField {
281
282 private final int fBase;
283 private final boolean fSigned;
284 private String fFormattedValue = null;
285
286 /**
287 * Constructor for CTFIntegerArrayField.
288 *
289 * @param name
290 * The name of this field
291 * @param longValues
292 * The array of integers (as longs) that compose this field's
293 * value
294 * @param signed
295 * Are the values in the array signed or not
296 */
297 CTFIntegerArrayField(String name, long[] longValues, int base, boolean signed) {
298 super(name, longValues, null);
299 fBase = base;
300 fSigned = signed;
301 }
302
303 @Override
304 public long[] getValue() {
305 return (long[]) super.getValue();
306 }
307
308 @Override
309 public synchronized String getFormattedValue() {
310 if (fFormattedValue == null) {
311 List<String> strings = new ArrayList<>();
312 for (long value : getValue()) {
313 strings.add(IntegerDefinition.formatNumber(value, fBase, fSigned));
314 }
315 fFormattedValue = strings.toString();
316 }
317 return fFormattedValue;
318 }
319
320 }
321
322 /**
323 * CTF field implementation for arrays of arbitrary types.
324 *
325 * @author fdoray
326 */
327 final class CTFArrayField extends CtfTmfEventField {
328
329 private String fFormattedValue = null;
330
331 /**
332 * Constructor for CTFArrayField.
333 *
334 * @param name
335 * The name of this field
336 * @param elements
337 * The array elements of this field
338 */
339 CTFArrayField(String name, CtfTmfEventField[] elements) {
340 super(name, elements, elements);
341 }
342
343 @Override
344 public CtfTmfEventField[] getValue() {
345 return (CtfTmfEventField[]) super.getValue();
346 }
347
348 @Override
349 public synchronized String getFormattedValue() {
350 if (fFormattedValue == null) {
351 List<String> strings = new ArrayList<>();
352 for (CtfTmfEventField element : getValue()) {
353 strings.add(element.getFormattedValue());
354 }
355 fFormattedValue = strings.toString();
356 }
357 return fFormattedValue;
358 }
359 }
360
361 /**
362 * CTF field implementation for floats.
363 *
364 * @author emathko
365 */
366 final class CTFFloatField extends CtfTmfEventField {
367
368 /**
369 * Constructor for CTFFloatField.
370 *
371 * @param value
372 * The float value (actually a double) of this field
373 * @param name
374 * The name of this field
375 */
376 protected CTFFloatField(String name, double value) {
377 super(name, value, null);
378 }
379
380 @Override
381 public Double getValue() {
382 return (Double) super.getValue();
383 }
384 }
385
386 /**
387 * The CTF field implementation for Enum fields
388 *
389 * @author Bernd Hufmann
390 */
391 final class CTFEnumField extends CtfTmfEventField {
392
393 /**
394 * Constructor for CTFEnumField.
395 *
396 * @param enumValue
397 * The Enum value consisting of a pair of Enum value name and its
398 * long value
399 * @param name
400 * The name of this field
401 */
402 CTFEnumField(String name, CtfEnumPair enumValue) {
403 super(name, new CtfEnumPair(enumValue.getFirst(),
404 enumValue.getSecond()), null);
405 }
406
407 @Override
408 public CtfEnumPair getValue() {
409 return (CtfEnumPair) super.getValue();
410 }
411 }
412
413 /**
414 * The CTF field implementation for struct fields with sub-fields
415 *
416 * @author gbastien
417 */
418 final class CTFStructField extends CtfTmfEventField {
419
420 /**
421 * Constructor for CTFStructField.
422 *
423 * @param fields
424 * The children of this field
425 * @param name
426 * The name of this field
427 */
428 CTFStructField(String name, CtfTmfEventField[] fields) {
429 super(name, fields, fields);
430 }
431
432 @Override
433 public CtfTmfEventField[] getValue() {
434 return (CtfTmfEventField[]) super.getValue();
435 }
436
437 @Override
438 public String getFormattedValue() {
439 return Arrays.toString(getValue());
440 }
441
442 }
443
444 /**
445 * The CTF field implementation for variant fields its child
446 *
447 * @author gbastien
448 */
449 final class CTFVariantField extends CtfTmfEventField {
450
451 /**
452 * Constructor for CTFVariantField.
453 *
454 * @param field
455 * The field selected for this variant
456 * @param name
457 * The name of this field
458 */
459 CTFVariantField(String name, CtfTmfEventField field) {
460 super(name, field, new CtfTmfEventField[] { field });
461 }
462
463 @Override
464 public CtfTmfEventField getValue() {
465 return (CtfTmfEventField) super.getValue();
466 }
467
468 }
469
470 /* Implement other possible fields types here... */
This page took 0.040627 seconds and 5 git commands to generate.