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
.ctf
.core
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Arrays
;
22 import java
.util
.Collection
;
23 import java
.util
.List
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.CompoundDeclaration
;
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
.ICompositeDefinition
;
30 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDeclaration
;
31 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinition
;
32 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
33 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
34 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
35 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.VariantDefinition
;
36 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.types
.ArrayDefinition
;
37 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.types
.ByteArrayDefinition
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
42 * The CTF implementation of the TMF event field model
45 * @author Matthew Khouzam
46 * @author Alexandre Montplaisir
48 public abstract class CtfTmfEventField
extends TmfEventField
{
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
55 * Standard constructor. Only to be used internally, call parseField() to
56 * generate a new field object.
59 * The name of this field
61 * The value of this field. Its type should match the field type.
63 * The children fields. Useful for composite fields
66 protected CtfTmfEventField(String name
, Object value
, ITmfEventField
[] fields
) {
67 super(/* Strip the underscore from the field name if there is one */
68 name
.startsWith("_") ? name
.substring(1) : name
, //$NON-NLS-1$
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
78 * Factory method to instantiate CtfTmfEventField objects.
81 * The CTF Definition of this event field
83 * String The name to assign to this field
84 * @return The resulting CtfTmfEventField object
85 * @deprecated use {@link CtfTmfEventField#parseField(IDefinition, String)}
88 public static CtfTmfEventField
parseField(Definition fieldDef
,
90 return parseField((IDefinition
) fieldDef
, fieldName
);
94 * Factory method to instantiate CtfTmfEventField objects.
97 * The CTF Definition of this event field
99 * String The name to assign to this field
100 * @return The resulting CtfTmfEventField object
103 public static CtfTmfEventField
parseField(IDefinition fieldDef
,
105 CtfTmfEventField field
= null;
107 /* Determine the Definition type */
108 if (fieldDef
instanceof IntegerDefinition
) {
109 IntegerDefinition intDef
= (IntegerDefinition
) fieldDef
;
110 int base
= intDef
.getDeclaration().getBase();
111 field
= new CTFIntegerField(fieldName
, intDef
.getValue(), base
, intDef
.getDeclaration().isSigned());
113 } else if (fieldDef
instanceof EnumDefinition
) {
114 EnumDefinition enumDef
= (EnumDefinition
) fieldDef
;
115 field
= new CTFEnumField(fieldName
, new CtfEnumPair(enumDef
.getValue(), enumDef
.getIntegerValue()));
117 } else if (fieldDef
instanceof StringDefinition
) {
118 field
= new CTFStringField(fieldName
, ((StringDefinition
) fieldDef
).getValue());
120 } else if (fieldDef
instanceof FloatDefinition
) {
121 FloatDefinition floatDef
= (FloatDefinition
) fieldDef
;
122 field
= new CTFFloatField(fieldName
, floatDef
.getValue());
124 } else if (fieldDef
instanceof ArrayDefinition
) {
125 ArrayDefinition arrayDef
= (ArrayDefinition
) fieldDef
;
126 IDeclaration decl
= arrayDef
.getDeclaration();
127 if (!(decl
instanceof CompoundDeclaration
)) {
128 throw new IllegalArgumentException("Array definitions should only come from sequence or array declarations"); //$NON-NLS-1$
130 CompoundDeclaration arrDecl
= (CompoundDeclaration
) decl
;
131 IDeclaration elemType
= null;
132 Collection
<Definition
> definitions
= arrayDef
.getDefinitions();
133 elemType
= arrDecl
.getElementType();
134 if (elemType
instanceof IntegerDeclaration
) {
135 /* Array of integers => CTFIntegerArrayField */
136 IntegerDeclaration elemIntType
= (IntegerDeclaration
) elemType
;
137 long[] values
= new long[arrayDef
.getLength()];
138 for (int i
= 0; i
< arrayDef
.getLength(); i
++) {
139 IDefinition elem
= arrayDef
.getDefinitions().get(i
);
143 values
[i
] = ((IntegerDefinition
) elem
).getValue();
145 field
= new CTFIntegerArrayField(fieldName
, values
,
146 elemIntType
.getBase(),
147 elemIntType
.isSigned());
150 /* Arrays of elements of any other type */
151 CtfTmfEventField
[] elements
= new CtfTmfEventField
[arrayDef
.getLength()];
152 /* Parse the elements of the array. */
154 for (IDefinition definition
: definitions
) {
155 CtfTmfEventField curField
= CtfTmfEventField
.parseField(
156 definition
, fieldName
+ '[' + i
+ ']');
157 elements
[i
] = curField
;
161 field
= new CTFArrayField(fieldName
, elements
);
163 } else if (fieldDef
instanceof ByteArrayDefinition
) {
164 /* This is an array of ascii bytes, a.k.a. a String! */
165 field
= new CTFStringField(fieldName
, fieldDef
.toString());
167 } else if (fieldDef
instanceof ICompositeDefinition
) {
168 ICompositeDefinition strDef
= (ICompositeDefinition
) fieldDef
;
170 List
<ITmfEventField
> list
= new ArrayList
<>();
171 /* Recursively parse the fields */
172 for (String curFieldName
: strDef
.getFieldNames()) {
173 list
.add(CtfTmfEventField
.parseField(strDef
.getDefinition(curFieldName
), curFieldName
));
175 field
= new CTFStructField(fieldName
, list
.toArray(new CtfTmfEventField
[list
.size()]));
177 } else if (fieldDef
instanceof VariantDefinition
) {
178 VariantDefinition varDef
= (VariantDefinition
) fieldDef
;
180 String curFieldName
= varDef
.getCurrentFieldName();
181 IDefinition curFieldDef
= varDef
.getCurrentField();
182 if (curFieldDef
!= null) {
183 CtfTmfEventField subField
= CtfTmfEventField
.parseField(curFieldDef
, curFieldName
);
184 field
= new CTFVariantField(fieldName
, subField
);
186 /* A safe-guard, but curFieldDef should never be null */
187 field
= new CTFStringField(curFieldName
, ""); //$NON-NLS-1$
192 * Safe-guard, to avoid null exceptions later, field is expected not
195 field
= new CTFStringField(fieldName
, Messages
.CtfTmfEventField_UnsupportedType
+ fieldDef
.getClass().toString());
201 public String
toString() {
202 return getName() + '=' + getFormattedValue();
208 * The CTF field implementation for integer fields.
212 final class CTFIntegerField
extends CtfTmfEventField
{
214 private final int fBase
;
215 private final boolean fSigned
;
218 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
219 * Java parser this is interpreted as a long.
222 * The name of this field
224 * The integer value of this field
226 * Is the value signed or not
228 CTFIntegerField(String name
, long longValue
, int base
, boolean signed
) {
229 super(name
, longValue
, null);
235 public Long
getValue() {
236 return (Long
) super.getValue();
240 public String
getFormattedValue() {
241 return IntegerDefinition
.formatNumber(getValue(), fBase
, fSigned
);
247 * The CTF field implementation for string fields
251 final class CTFStringField
extends CtfTmfEventField
{
254 * Constructor for CTFStringField.
257 * The string value of this field
259 * The name of this field
261 CTFStringField(String name
, String strValue
) {
262 super(name
, strValue
, null);
266 public String
getValue() {
267 return (String
) super.getValue();
272 * CTF field implementation for arrays of integers.
276 final class CTFIntegerArrayField
extends CtfTmfEventField
{
278 private final int fBase
;
279 private final boolean fSigned
;
280 private String fFormattedValue
= null;
283 * Constructor for CTFIntegerArrayField.
286 * The name of this field
288 * The array of integers (as longs) that compose this field's
291 * Are the values in the array signed or not
293 CTFIntegerArrayField(String name
, long[] longValues
, int base
, boolean signed
) {
294 super(name
, longValues
, null);
300 public long[] getValue() {
301 return (long[]) super.getValue();
305 public synchronized String
getFormattedValue() {
306 if (fFormattedValue
== null) {
307 List
<String
> strings
= new ArrayList
<>();
308 for (long value
: getValue()) {
309 strings
.add(IntegerDefinition
.formatNumber(value
, fBase
, fSigned
));
311 fFormattedValue
= strings
.toString();
313 return fFormattedValue
;
319 * CTF field implementation for arrays of arbitrary types.
323 final class CTFArrayField
extends CtfTmfEventField
{
325 private String fFormattedValue
= null;
328 * Constructor for CTFArrayField.
331 * The name of this field
333 * The array elements of this field
335 CTFArrayField(String name
, CtfTmfEventField
[] elements
) {
336 super(name
, elements
, elements
);
340 public CtfTmfEventField
[] getValue() {
341 return (CtfTmfEventField
[]) super.getValue();
345 public synchronized String
getFormattedValue() {
346 if (fFormattedValue
== null) {
347 List
<String
> strings
= new ArrayList
<>();
348 for (CtfTmfEventField element
: getValue()) {
349 strings
.add(element
.getFormattedValue());
351 fFormattedValue
= strings
.toString();
353 return fFormattedValue
;
358 * CTF field implementation for floats.
362 final class CTFFloatField
extends CtfTmfEventField
{
365 * Constructor for CTFFloatField.
368 * The float value (actually a double) of this field
370 * The name of this field
372 protected CTFFloatField(String name
, double value
) {
373 super(name
, value
, null);
377 public Double
getValue() {
378 return (Double
) super.getValue();
383 * The CTF field implementation for Enum fields
385 * @author Bernd Hufmann
387 final class CTFEnumField
extends CtfTmfEventField
{
390 * Constructor for CTFEnumField.
393 * The Enum value consisting of a pair of Enum value name and its
396 * The name of this field
398 CTFEnumField(String name
, CtfEnumPair enumValue
) {
399 super(name
, new CtfEnumPair(enumValue
.getFirst(),
400 enumValue
.getSecond()), null);
404 public CtfEnumPair
getValue() {
405 return (CtfEnumPair
) super.getValue();
410 * The CTF field implementation for struct fields with sub-fields
414 final class CTFStructField
extends CtfTmfEventField
{
417 * Constructor for CTFStructField.
420 * The children of this field
422 * The name of this field
424 CTFStructField(String name
, CtfTmfEventField
[] fields
) {
425 super(name
, fields
, fields
);
429 public CtfTmfEventField
[] getValue() {
430 return (CtfTmfEventField
[]) super.getValue();
434 public String
getFormattedValue() {
435 return Arrays
.toString(getValue());
441 * The CTF field implementation for variant fields its child
445 final class CTFVariantField
extends CtfTmfEventField
{
448 * Constructor for CTFVariantField.
451 * The field selected for this variant
453 * The name of this field
455 CTFVariantField(String name
, CtfTmfEventField field
) {
456 super(name
, field
, new CtfTmfEventField
[] { field
});
460 public CtfTmfEventField
getValue() {
461 return (CtfTmfEventField
) super.getValue();
466 /* Implement other possible fields types here... */