| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011 Ericsson |
| 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 | * Alexendre Montplaisir - Initial API and implementation |
| 12 | * Bernd Hufmann - Add Enum field handling |
| 13 | * |
| 14 | *******************************************************************************/ |
| 15 | |
| 16 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; |
| 17 | |
| 18 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; |
| 19 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; |
| 20 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
| 21 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; |
| 22 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition; |
| 23 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
| 24 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
| 25 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; |
| 26 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; |
| 27 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; |
| 28 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
| 29 | |
| 30 | /** |
| 31 | * The CTF implementation of the TMF event field model |
| 32 | * |
| 33 | * @version 2.0 |
| 34 | * @author Matthew Khouzam |
| 35 | * @author Alexandre Montplaisir |
| 36 | */ |
| 37 | public abstract class CtfTmfEventField implements ITmfEventField { |
| 38 | |
| 39 | // ------------------------------------------------------------------------ |
| 40 | // Class attributes |
| 41 | // ------------------------------------------------------------------------ |
| 42 | |
| 43 | /** @since 2.0 */ |
| 44 | protected static final int FIELDTYPE_INTEGER = 0; |
| 45 | |
| 46 | /** @since 2.0 */ |
| 47 | protected static final int FIELDTYPE_STRING = 1; |
| 48 | |
| 49 | /** @since 2.0 */ |
| 50 | protected static final int FIELDTYPE_INTEGER_ARRAY = 2; |
| 51 | |
| 52 | /** @since 2.0 */ |
| 53 | protected static final int FIELDTYPE_FLOAT = 3; |
| 54 | |
| 55 | /** @since 2.0 */ |
| 56 | protected static final int FIELDTYPE_ENUM = 4; |
| 57 | |
| 58 | // ------------------------------------------------------------------------ |
| 59 | // Attributes |
| 60 | // ------------------------------------------------------------------------ |
| 61 | |
| 62 | protected final String name; |
| 63 | |
| 64 | // ------------------------------------------------------------------------ |
| 65 | // Constructor |
| 66 | // ------------------------------------------------------------------------ |
| 67 | |
| 68 | /** |
| 69 | * Standard constructor. Only to be used internally, call parseField() to |
| 70 | * generate a new field object. |
| 71 | * |
| 72 | * @param name |
| 73 | * The name of this field |
| 74 | */ |
| 75 | protected CtfTmfEventField(String name) { |
| 76 | /* Strip the underscore */ |
| 77 | if (name.startsWith("_")) { //$NON-NLS-1$ |
| 78 | this.name = name.substring(1); |
| 79 | } else { |
| 80 | this.name = name; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // ------------------------------------------------------------------------ |
| 85 | // Getters/Setters/Predicates |
| 86 | // ------------------------------------------------------------------------ |
| 87 | |
| 88 | @Override |
| 89 | public String getName() { |
| 90 | return this.name; |
| 91 | } |
| 92 | |
| 93 | // ------------------------------------------------------------------------ |
| 94 | // Operations |
| 95 | // ------------------------------------------------------------------------ |
| 96 | |
| 97 | /** |
| 98 | * Factory method to instantiate CtfTmfEventField objects. |
| 99 | * |
| 100 | * @param fieldDef |
| 101 | * The CTF Definition of this event field |
| 102 | * @param fieldName |
| 103 | * String The name to assign to this field |
| 104 | * @return The resulting CtfTmfEventField object |
| 105 | */ |
| 106 | public static CtfTmfEventField parseField(Definition fieldDef, |
| 107 | String fieldName) { |
| 108 | CtfTmfEventField field = null; |
| 109 | |
| 110 | /* Determine the Definition type */ |
| 111 | if (fieldDef instanceof IntegerDefinition) { |
| 112 | IntegerDefinition intDef = (IntegerDefinition) fieldDef; |
| 113 | int base = intDef.getDeclaration().getBase(); |
| 114 | field = new CTFIntegerField(intDef.getValue(), fieldName, base); |
| 115 | |
| 116 | } else if (fieldDef instanceof EnumDefinition) { |
| 117 | EnumDefinition enumDef = (EnumDefinition) fieldDef; |
| 118 | field = new CTFEnumField(new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue()), fieldName); |
| 119 | |
| 120 | } else if (fieldDef instanceof StringDefinition) { |
| 121 | field = new CTFStringField( |
| 122 | ((StringDefinition) fieldDef).getValue(), fieldName); |
| 123 | |
| 124 | } else if (fieldDef instanceof ArrayDefinition) { |
| 125 | ArrayDefinition arrayDef = (ArrayDefinition) fieldDef; |
| 126 | ArrayDeclaration arrayDecl = arrayDef.getDeclaration(); |
| 127 | |
| 128 | if (arrayDef.isString()) { |
| 129 | /* This is an array of UTF-8 bytes, a.k.a. a String! */ |
| 130 | field = new CTFStringField(fieldDef.toString(), fieldName); |
| 131 | |
| 132 | } else if (arrayDecl.getElementType() instanceof IntegerDeclaration) { |
| 133 | /* This is a an array of CTF Integers */ |
| 134 | long[] values = new long[arrayDecl.getLength()]; |
| 135 | for (int i = 0; i < arrayDecl.getLength(); i++) { |
| 136 | values[i] = ((IntegerDefinition) arrayDef.getElem(i)) |
| 137 | .getValue(); |
| 138 | } |
| 139 | field = new CTFIntegerArrayField(values, fieldName); |
| 140 | } |
| 141 | /* Add other types of arrays here */ |
| 142 | |
| 143 | } else if (fieldDef instanceof SequenceDefinition) { |
| 144 | SequenceDefinition seqDef = (SequenceDefinition) fieldDef; |
| 145 | SequenceDeclaration seqDecl = seqDef.getDeclaration(); |
| 146 | |
| 147 | if (seqDef.getLength() == 0) { |
| 148 | /* Some sequences have length = 0. Simply use an empty string */ |
| 149 | field = new CTFStringField("", fieldName); //$NON-NLS-1$ |
| 150 | } else if (seqDef.isString()) { |
| 151 | /* Interpret this sequence as a String */ |
| 152 | field = new CTFStringField(seqDef.toString(), fieldName); |
| 153 | } else if (seqDecl.getElementType() instanceof IntegerDeclaration) { |
| 154 | /* Sequence of integers => CTFIntegerArrayField */ |
| 155 | long[] values = new long[seqDef.getLength()]; |
| 156 | for (int i = 0; i < seqDef.getLength(); i++) { |
| 157 | values[i] = ((IntegerDefinition) seqDef.getElem(i)) |
| 158 | .getValue(); |
| 159 | } |
| 160 | field = new CTFIntegerArrayField(values, fieldName); |
| 161 | } |
| 162 | /* Add other Sequence types here */ |
| 163 | |
| 164 | } else if (fieldDef instanceof FloatDefinition) { |
| 165 | FloatDefinition floatDef = (FloatDefinition) fieldDef; |
| 166 | field = new CTFFloatField(floatDef.getValue(), fieldName); |
| 167 | } |
| 168 | |
| 169 | return field; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Copy factory. Create a new field by (deep-) copying the information in an |
| 174 | * existing one. |
| 175 | * |
| 176 | * @param other |
| 177 | * The other CtfTmfEventField to copy |
| 178 | * @return The new CtfTmfEventField |
| 179 | */ |
| 180 | public static CtfTmfEventField copyFrom(CtfTmfEventField other) { |
| 181 | switch (other.getFieldType()) { |
| 182 | case FIELDTYPE_INTEGER: |
| 183 | CTFIntegerField intOther = (CTFIntegerField) other; |
| 184 | return new CTFIntegerField(intOther.getValue(), intOther.name, |
| 185 | intOther.getBase()); |
| 186 | case FIELDTYPE_STRING: |
| 187 | return new CTFStringField(((CTFStringField) other).getValue(), |
| 188 | other.name); |
| 189 | case FIELDTYPE_INTEGER_ARRAY: |
| 190 | return new CTFIntegerArrayField( |
| 191 | ((CTFIntegerArrayField) other).getValue(), other.name); |
| 192 | case FIELDTYPE_FLOAT: |
| 193 | return new CTFFloatField(((CTFFloatField) other).getValue(), |
| 194 | other.name); |
| 195 | case FIELDTYPE_ENUM: |
| 196 | return new CTFEnumField(((CTFEnumField) other).getValue(), other.name); |
| 197 | default: |
| 198 | return null; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public CtfTmfEventField clone() { |
| 204 | return CtfTmfEventField.copyFrom(this); |
| 205 | } |
| 206 | |
| 207 | // ------------------------------------------------------------------------ |
| 208 | // Abstract methods (to be implemented by each specific field type) |
| 209 | // ------------------------------------------------------------------------ |
| 210 | |
| 211 | /** |
| 212 | * Return the int representing this field's value type |
| 213 | * |
| 214 | * @return The field type |
| 215 | */ |
| 216 | public abstract int getFieldType(); |
| 217 | |
| 218 | /** |
| 219 | * Return this field's value. You can cast it to the correct type depending |
| 220 | * on what getFieldType says. |
| 221 | * |
| 222 | * @return The field's value |
| 223 | */ |
| 224 | @Override |
| 225 | public abstract Object getValue(); |
| 226 | |
| 227 | // ------------------------------------------------------------------------ |
| 228 | // Other methods defined by ITmfEventField, but not used here. |
| 229 | // CTF fields do not have sub-fields (yet!) |
| 230 | // ------------------------------------------------------------------------ |
| 231 | |
| 232 | @Override |
| 233 | public String[] getFieldNames() { |
| 234 | return null; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public String getFieldName(int index) { |
| 239 | return null; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public ITmfEventField[] getFields() { |
| 244 | return null; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public ITmfEventField getField(String fieldName) { |
| 249 | return null; |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public ITmfEventField getField(int index) { |
| 254 | return null; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * The CTF field implementation for integer fields. |
| 260 | * |
| 261 | * @author alexmont |
| 262 | */ |
| 263 | final class CTFIntegerField extends CtfTmfEventField { |
| 264 | |
| 265 | private final long longValue; |
| 266 | private final int base; |
| 267 | |
| 268 | /** |
| 269 | * A CTF "IntegerDefinition" can be an integer of any byte size, so in the |
| 270 | * Java parser this is interpreted as a long. |
| 271 | * |
| 272 | * @param longValue |
| 273 | * The integer value of this field |
| 274 | * @param name |
| 275 | * The name of this field |
| 276 | */ |
| 277 | CTFIntegerField(long longValue, String name, int base) { |
| 278 | super(name); |
| 279 | this.longValue = longValue; |
| 280 | this.base = base; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Return the integer's base. (Not made public until it's needed.) |
| 285 | * |
| 286 | * @return The base, usually 10 or 16. |
| 287 | */ |
| 288 | int getBase() { |
| 289 | return base; |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public int getFieldType() { |
| 294 | return FIELDTYPE_INTEGER; |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public Long getValue() { |
| 299 | return this.longValue; |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public String toString() { |
| 304 | StringBuilder sb = new StringBuilder(name); |
| 305 | sb.append('='); |
| 306 | |
| 307 | /* Format the number correctly according to the integer's base */ |
| 308 | switch (base) { |
| 309 | case 2: |
| 310 | sb.append("0b"); //$NON-NLS-1$ |
| 311 | sb.append(Long.toBinaryString(longValue)); |
| 312 | break; |
| 313 | case 8: |
| 314 | sb.append('0'); |
| 315 | sb.append(Long.toOctalString(longValue)); |
| 316 | break; |
| 317 | case 10: |
| 318 | sb.append(longValue); |
| 319 | break; |
| 320 | case 16: |
| 321 | sb.append("0x"); //$NON-NLS-1$ |
| 322 | sb.append(Long.toHexString(longValue)); |
| 323 | break; |
| 324 | default: |
| 325 | /* Non-standard base, we'll just print it as a decimal number */ |
| 326 | sb.append(longValue); |
| 327 | break; |
| 328 | } |
| 329 | return sb.toString(); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * The CTF field implementation for string fields |
| 335 | * |
| 336 | * @author alexmont |
| 337 | */ |
| 338 | final class CTFStringField extends CtfTmfEventField { |
| 339 | |
| 340 | private final String strValue; |
| 341 | |
| 342 | /** |
| 343 | * Constructor for CTFStringField. |
| 344 | * |
| 345 | * @param strValue |
| 346 | * The string value of this field |
| 347 | * @param name |
| 348 | * The name of this field |
| 349 | */ |
| 350 | CTFStringField(String strValue, String name) { |
| 351 | super(name); |
| 352 | this.strValue = strValue; |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public int getFieldType() { |
| 357 | return FIELDTYPE_STRING; |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public String getValue() { |
| 362 | return this.strValue; |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public String toString() { |
| 367 | return name + '=' + strValue; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * CTF field implementation for arrays of integers. |
| 373 | * |
| 374 | * @author alexmont |
| 375 | */ |
| 376 | final class CTFIntegerArrayField extends CtfTmfEventField { |
| 377 | |
| 378 | private final long[] longValues; |
| 379 | |
| 380 | /** |
| 381 | * Constructor for CTFIntegerArrayField. |
| 382 | * |
| 383 | * @param longValues |
| 384 | * The array of integers (as longs) that compose this field's |
| 385 | * value |
| 386 | * @param name |
| 387 | * The name of this field |
| 388 | */ |
| 389 | CTFIntegerArrayField(long[] longValues, String name) { |
| 390 | super(name); |
| 391 | this.longValues = longValues; |
| 392 | } |
| 393 | |
| 394 | @Override |
| 395 | public int getFieldType() { |
| 396 | return FIELDTYPE_INTEGER_ARRAY; |
| 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public long[] getValue() { |
| 401 | return this.longValues; |
| 402 | } |
| 403 | |
| 404 | @Override |
| 405 | public String toString() { |
| 406 | StringBuffer buffer = new StringBuffer(); |
| 407 | buffer.append("{ "); //$NON-NLS-1$ |
| 408 | |
| 409 | buffer.append(longValues[0]); |
| 410 | for (int i = 1; i < longValues.length; i++) { |
| 411 | buffer.append(", " + longValues[i]); //$NON-NLS-1$ |
| 412 | } |
| 413 | buffer.append('}'); |
| 414 | return name + '=' + buffer.toString(); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * CTF field implementation for floats. |
| 420 | * |
| 421 | * @author emathko |
| 422 | */ |
| 423 | final class CTFFloatField extends CtfTmfEventField { |
| 424 | |
| 425 | private final Double value; |
| 426 | |
| 427 | /** |
| 428 | * Constructor for CTFFloatField. |
| 429 | * |
| 430 | * @param value |
| 431 | * The float value (actually a double) of this field |
| 432 | * @param name |
| 433 | * The name of this field |
| 434 | */ |
| 435 | protected CTFFloatField(double value, String name) { |
| 436 | super(name); |
| 437 | this.value = value; |
| 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public int getFieldType() { |
| 442 | return FIELDTYPE_FLOAT; |
| 443 | } |
| 444 | |
| 445 | @Override |
| 446 | public Double getValue() { |
| 447 | return this.value; |
| 448 | } |
| 449 | |
| 450 | @Override |
| 451 | public String toString() { |
| 452 | return name + '=' + value; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * The CTF field implementation for Enum fields |
| 458 | * |
| 459 | * @author Bernd Hufmann |
| 460 | */ |
| 461 | final class CTFEnumField extends CtfTmfEventField { |
| 462 | |
| 463 | private final CtfEnumPair value; |
| 464 | |
| 465 | /** |
| 466 | * Constructor for CTFEnumField. |
| 467 | * |
| 468 | * @param enumValue |
| 469 | * The Enum value consisting of a pair of Enum value name and its long value |
| 470 | * @param name |
| 471 | * The name of this field |
| 472 | */ |
| 473 | CTFEnumField(CtfEnumPair enumValue, String name) { |
| 474 | super(name); |
| 475 | this.value = new CtfEnumPair(enumValue.getFirst(), enumValue.getSecond().longValue()); |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public int getFieldType() { |
| 480 | return FIELDTYPE_ENUM; |
| 481 | } |
| 482 | |
| 483 | @Override |
| 484 | public CtfEnumPair getValue() { |
| 485 | return this.value; |
| 486 | } |
| 487 | |
| 488 | @Override |
| 489 | public String toString() { |
| 490 | return name + '=' + value.toString(); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /* Implement other possible fields types here... */ |