1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Alexendre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
15 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDeclaration
;
16 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDeclaration
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDefinition
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
26 * <b><u>CTFEventField</u></b>
28 public abstract class CtfTmfEventField
implements ITmfEventField
{
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
34 protected final String name
;
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
40 protected CtfTmfEventField(String name
) {
41 /* Strip the damn underscores, screw you CTF */
42 if ( name
.startsWith("_") ) { //$NON-NLS-1$
43 this.name
= name
.substring(1);
49 // ------------------------------------------------------------------------
50 // Getters/Setters/Predicates
51 // ------------------------------------------------------------------------
54 public String
getName() {
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 public static CtfTmfEventField
parseField(Definition fieldDef
,
64 CtfTmfEventField field
= null;
66 /* Determine the Definition type */
67 if (fieldDef
instanceof IntegerDefinition
) {
68 field
= new CTFIntegerField(
69 ((IntegerDefinition
) fieldDef
).getValue(), fieldName
);
71 } else if (fieldDef
instanceof StringDefinition
) {
72 field
= new CTFStringField(
73 ((StringDefinition
) fieldDef
).getValue(), fieldName
);
75 } else if (fieldDef
instanceof ArrayDefinition
) {
76 ArrayDefinition arrayDef
= (ArrayDefinition
) fieldDef
;
77 ArrayDeclaration arrayDecl
= arrayDef
.getDeclaration();
79 if (arrayDef
.isString()) {
80 /* This is an array of UTF-8 bytes, a.k.a. a String! */
81 field
= new CTFStringField(fieldDef
.toString(), fieldName
);
83 } else if (arrayDecl
.getElementType() instanceof IntegerDeclaration
) {
84 /* This is a an array of CTF Integers */
85 long[] values
= new long[arrayDecl
.getLength()];
86 for (int i
= 0; i
< arrayDecl
.getLength(); i
++) {
87 values
[i
] = ((IntegerDefinition
) arrayDef
.getElem(i
)).getValue();
89 field
= new CTFIntegerArrayField(values
, fieldName
);
91 /* Add other types of arrays here */
93 } else if (fieldDef
instanceof SequenceDefinition
) {
94 SequenceDefinition seqDef
= (SequenceDefinition
) fieldDef
;
95 SequenceDeclaration seqDecl
= seqDef
.getDeclaration();
97 if (seqDef
.getLength() == 0) {
98 /* Some sequences have length = 0. Simply use an empty string */
99 field
= new CTFStringField("", fieldName
); //$NON-NLS-1$
100 } else if (seqDef
.isString()) {
101 /* Interpret this sequence as a String */
102 field
= new CTFStringField(seqDef
.toString(), fieldName
);
103 } else if (seqDecl
.getElementType() instanceof IntegerDeclaration
) {
104 /* Sequence of integers => CTFIntegerArrayField */
105 long[] values
= new long[seqDef
.getLength()];
106 for (int i
= 0; i
< seqDef
.getLength(); i
++) {
107 values
[i
] = ((IntegerDefinition
) seqDef
.getElem(i
)).getValue();
109 field
= new CTFIntegerArrayField(values
, fieldName
);
111 /* Add other Sequence types here */
113 /* Add other field types here */
118 public static CtfTmfEventField
copyFrom(CtfTmfEventField other
) {
119 switch (other
.getFieldType()) {
121 return new CTFIntegerField(((CTFIntegerField
) other
).getValue(),
124 return new CTFStringField(((CTFStringField
) other
).getValue(),
127 return new CTFIntegerArrayField(
128 ((CTFIntegerArrayField
) other
).getValue(), other
.name
);
135 public CtfTmfEventField
clone() {
136 return CtfTmfEventField
.copyFrom(this);
140 * Return the int representing this field's value type
142 * @return the field type
144 public abstract int getFieldType();
147 * Return this field's value. You can cast it to the correct type depending
148 * on what getFieldType says.
150 * @return the field value
153 public abstract Object
getValue();
156 * Other methods defined by ITmfEventField, but not used here: the CTF
157 * fields do not have sub-fields (yet!)
159 * @return the field names
162 public String
[] getFieldNames() {
167 public String
getFieldName(int index
) {
172 public ITmfEventField
[] getFields() {
177 public ITmfEventField
getField(String fieldName
) {
182 public ITmfEventField
getField(int index
) {
188 * <b><u>CTFIntegerField</u></b>
190 final class CTFIntegerField
extends CtfTmfEventField
{
192 private final long longValue
;
195 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
196 * Java parser this is interpreted as a long.
198 CTFIntegerField(long longValue
, String name
) {
200 this.longValue
= longValue
;
204 public int getFieldType() {
209 public Long
getValue() {
210 return this.longValue
;
216 * @see java.lang.Object#toString()
219 public String
toString() {
220 return name
+ '=' + longValue
;
225 * <b><u>CTFStringField</u></b>
227 final class CTFStringField
extends CtfTmfEventField
{
229 private final String strValue
;
231 CTFStringField(String strValue
, String name
) {
233 this.strValue
= strValue
;
237 public int getFieldType() {
242 public String
getValue() {
243 return this.strValue
;
249 * @see java.lang.Object#toString()
252 public String
toString() {
253 return name
+ '=' + strValue
;
258 * <b><u>CTFIntegerArrayField</u></b>
260 final class CTFIntegerArrayField
extends CtfTmfEventField
{
262 private final long[] longValues
;
264 CTFIntegerArrayField(long[] longValues
, String name
) {
266 this.longValues
= longValues
;
270 public int getFieldType() {
275 public long[] getValue() {
276 return this.longValues
;
280 public String
toString() {
281 StringBuffer buffer
= new StringBuffer();
282 buffer
.append("{ "); //$NON-NLS-1$
284 buffer
.append(longValues
[0]);
285 for (int i
= 1; i
< longValues
.length
; i
++) {
286 buffer
.append(", " + longValues
[i
]); //$NON-NLS-1$
289 return name
+ '=' + buffer
.toString();
293 /* Implement other possible fields types here... */