1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, École Polytechnique de Montréal
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
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 *******************************************************************************/
18 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Arrays
;
22 import java
.util
.List
;
23 import java
.util
.Map
.Entry
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDefinition
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDefinition
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDeclaration
;
32 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDefinition
;
33 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
34 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
35 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.VariantDefinition
;
36 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Messages
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
41 * The CTF implementation of the TMF event field model
44 * @author Matthew Khouzam
45 * @author Alexandre Montplaisir
47 public abstract class CtfTmfEventField
extends TmfEventField
{
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
54 * Standard constructor. Only to be used internally, call parseField() to
55 * generate a new field object.
58 * The name of this field
60 * The value of this field. Its type should match the field type.
62 * The children fields. Useful for composite fields
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$
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
77 * Factory method to instantiate CtfTmfEventField objects.
80 * The CTF Definition of this event field
82 * String The name to assign to this field
83 * @return The resulting CtfTmfEventField object
85 public static CtfTmfEventField
parseField(Definition fieldDef
,
87 CtfTmfEventField field
= null;
89 /* Determine the Definition type */
90 if (fieldDef
instanceof IntegerDefinition
) {
91 IntegerDefinition intDef
= (IntegerDefinition
) fieldDef
;
92 int base
= intDef
.getDeclaration().getBase();
93 field
= new CTFIntegerField(fieldName
, intDef
.getValue(), base
, intDef
.getDeclaration().isSigned());
95 } else if (fieldDef
instanceof EnumDefinition
) {
96 EnumDefinition enumDef
= (EnumDefinition
) fieldDef
;
97 field
= new CTFEnumField(fieldName
, new CtfEnumPair(enumDef
.getValue(), enumDef
.getIntegerValue()));
99 } else if (fieldDef
instanceof StringDefinition
) {
100 field
= new CTFStringField(fieldName
, ((StringDefinition
) fieldDef
).getValue());
102 } else if (fieldDef
instanceof FloatDefinition
) {
103 FloatDefinition floatDef
= (FloatDefinition
) fieldDef
;
104 field
= new CTFFloatField(fieldName
, floatDef
.getValue());
106 } else if (fieldDef
instanceof ArrayDefinition
) {
107 ArrayDefinition arrayDef
= (ArrayDefinition
) fieldDef
;
109 if (arrayDef
.isString()) {
110 /* This is an array of UTF-8 bytes, a.k.a. a String! */
111 field
= new CTFStringField(fieldName
, fieldDef
.toString());
114 /* Arrays of elements of any other type */
115 Definition
[] definitions
= arrayDef
.getDefinitions();
116 CtfTmfEventField
[] elements
= new CtfTmfEventField
[definitions
.length
];
118 /* Parse the elements of the array. */
119 for (int i
= 0; i
< definitions
.length
; i
++) {
120 CtfTmfEventField curField
= CtfTmfEventField
.parseField(
121 definitions
[i
], fieldName
+ '[' + i
+ ']');
122 elements
[i
] = curField
;
125 field
= new CTFArrayField(fieldName
, elements
);
127 } else if (fieldDef
instanceof SequenceDefinition
) {
128 SequenceDefinition seqDef
= (SequenceDefinition
) fieldDef
;
129 SequenceDeclaration seqDecl
= seqDef
.getDeclaration();
131 if (seqDef
.getLength() == 0) {
132 /* Some sequences have length = 0. Simply use an empty string */
133 field
= new CTFStringField(fieldName
, ""); //$NON-NLS-1$
134 } else if (seqDef
.isString()) {
135 /* Interpret this sequence as a String */
136 field
= new CTFStringField(fieldName
, seqDef
.toString());
137 } else if (seqDecl
.getElementType() instanceof IntegerDeclaration
) {
138 /* Sequence of integers => CTFIntegerArrayField */
139 long[] values
= new long[seqDef
.getLength()];
140 for (int i
= 0; i
< seqDef
.getLength(); i
++) {
141 values
[i
] = ((IntegerDefinition
) seqDef
.getElem(i
)).getValue();
143 field
= new CTFIntegerArrayField(fieldName
, values
,
144 ((IntegerDeclaration
) seqDecl
.getElementType()).getBase(),
145 ((IntegerDeclaration
) seqDecl
.getElementType()).isSigned());
148 /* Add other Sequence types here */
150 } else if (fieldDef
instanceof StructDefinition
) {
151 StructDefinition strDef
= (StructDefinition
) fieldDef
;
153 String curFieldName
= null;
154 Definition curFieldDef
;
155 CtfTmfEventField curField
;
156 List
<ITmfEventField
> list
= new ArrayList
<>();
157 /* Recursively parse the fields */
158 for (Entry
<String
, Definition
> entry
: strDef
.getDefinitions().entrySet()) {
159 curFieldName
= entry
.getKey();
160 curFieldDef
= entry
.getValue();
161 curField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
164 field
= new CTFStructField(fieldName
, list
.toArray(new CtfTmfEventField
[list
.size()]));
166 } else if (fieldDef
instanceof VariantDefinition
) {
167 VariantDefinition varDef
= (VariantDefinition
) fieldDef
;
169 String curFieldName
= varDef
.getCurrentFieldName();
170 Definition curFieldDef
= varDef
.getDefinitions().get(curFieldName
);
171 if (curFieldDef
!= null) {
172 CtfTmfEventField subField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
173 field
= new CTFVariantField(fieldName
, subField
);
175 /* A safe-guard, but curFieldDef should never be null */
176 field
= new CTFStringField(curFieldName
, ""); //$NON-NLS-1$
181 * Safe-guard, to avoid null exceptions later, field is expected not
184 field
= new CTFStringField(fieldName
, Messages
.TmfEventField_UnsupportedType
+ fieldDef
.getClass().toString());
190 public String
toString() {
191 return getName() + '=' + getFormattedValue();
197 * The CTF field implementation for integer fields.
201 final class CTFIntegerField
extends CtfTmfEventField
{
203 private final int fBase
;
204 private final boolean fSigned
;
207 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
208 * Java parser this is interpreted as a long.
211 * The name of this field
213 * The integer value of this field
215 * Is the value signed or not
217 CTFIntegerField(String name
, long longValue
, int base
, boolean signed
) {
218 super(name
, longValue
, null);
224 public Long
getValue() {
225 return (Long
) super.getValue();
229 public String
getFormattedValue() {
230 return IntegerDefinition
.formatNumber(getValue(), fBase
, fSigned
);
236 * The CTF field implementation for string fields
240 final class CTFStringField
extends CtfTmfEventField
{
243 * Constructor for CTFStringField.
246 * The string value of this field
248 * The name of this field
250 CTFStringField(String name
, String strValue
) {
251 super(name
, strValue
, null);
255 public String
getValue() {
256 return (String
) super.getValue();
261 * CTF field implementation for arrays of integers.
265 final class CTFIntegerArrayField
extends CtfTmfEventField
{
267 private final int fBase
;
268 private final boolean fSigned
;
269 private String fFormattedValue
= null;
272 * Constructor for CTFIntegerArrayField.
275 * The name of this field
277 * The array of integers (as longs) that compose this field's
280 * Are the values in the array signed or not
282 CTFIntegerArrayField(String name
, long[] longValues
, int base
, boolean signed
) {
283 super(name
, longValues
, null);
289 public long[] getValue() {
290 return (long[]) super.getValue();
294 public synchronized String
getFormattedValue() {
295 if (fFormattedValue
== null) {
296 List
<String
> strings
= new ArrayList
<>();
297 for (long value
: getValue()) {
298 strings
.add(IntegerDefinition
.formatNumber(value
, fBase
, fSigned
));
300 fFormattedValue
= strings
.toString();
302 return fFormattedValue
;
308 * CTF field implementation for arrays of arbitrary types.
312 final class CTFArrayField
extends CtfTmfEventField
{
314 private String fFormattedValue
= null;
317 * Constructor for CTFArrayField.
320 * The name of this field
322 * The array elements of this field
324 CTFArrayField(String name
, CtfTmfEventField
[] elements
) {
325 super(name
, elements
, elements
);
329 public CtfTmfEventField
[] getValue() {
330 return (CtfTmfEventField
[]) super.getValue();
334 public synchronized String
getFormattedValue() {
335 if (fFormattedValue
== null) {
336 List
<String
> strings
= new ArrayList
<>();
337 for (CtfTmfEventField element
: getValue()) {
338 strings
.add(element
.getFormattedValue());
340 fFormattedValue
= strings
.toString();
342 return fFormattedValue
;
347 * CTF field implementation for floats.
351 final class CTFFloatField
extends CtfTmfEventField
{
354 * Constructor for CTFFloatField.
357 * The float value (actually a double) of this field
359 * The name of this field
361 protected CTFFloatField(String name
, double value
) {
362 super(name
, value
, null);
366 public Double
getValue() {
367 return (Double
) super.getValue();
372 * The CTF field implementation for Enum fields
374 * @author Bernd Hufmann
376 final class CTFEnumField
extends CtfTmfEventField
{
379 * Constructor for CTFEnumField.
382 * The Enum value consisting of a pair of Enum value name and its
385 * The name of this field
387 CTFEnumField(String name
, CtfEnumPair enumValue
) {
388 super(name
, new CtfEnumPair(enumValue
.getFirst(),
389 enumValue
.getSecond()), null);
393 public CtfEnumPair
getValue() {
394 return (CtfEnumPair
) super.getValue();
399 * The CTF field implementation for struct fields with sub-fields
403 final class CTFStructField
extends CtfTmfEventField
{
406 * Constructor for CTFStructField.
409 * The children of this field
411 * The name of this field
413 CTFStructField(String name
, CtfTmfEventField
[] fields
) {
414 super(name
, fields
, fields
);
418 public CtfTmfEventField
[] getValue() {
419 return (CtfTmfEventField
[]) super.getValue();
423 public String
getFormattedValue() {
424 return Arrays
.toString(getValue());
430 * The CTF field implementation for variant fields its child
434 final class CTFVariantField
extends CtfTmfEventField
{
437 * Constructor for CTFVariantField.
440 * The field selected for this variant
442 * The name of this field
444 CTFVariantField(String name
, CtfTmfEventField field
) {
445 super(name
, field
, new CtfTmfEventField
[] { field
});
449 public CtfTmfEventField
getValue() {
450 return (CtfTmfEventField
) super.getValue();
455 /* Implement other possible fields types here... */