Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2009, 2014 Ericsson |
54a7a54c | 3 | * |
d18dd09b ASL |
4 | * All rights reserved. This program and the accompanying materials are |
5 | * made 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 | |
54a7a54c | 8 | * |
d18dd09b ASL |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.tests.stubs.trace; |
d18dd09b ASL |
14 | |
15 | import java.io.EOFException; | |
16 | import java.io.IOException; | |
17 | import java.io.RandomAccessFile; | |
18 | import java.util.Vector; | |
19 | ||
2bdf0193 AM |
20 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
21 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; | |
22 | import org.eclipse.tracecompass.tmf.core.event.TmfEvent; | |
23 | import org.eclipse.tracecompass.tmf.core.event.TmfEventField; | |
24 | import org.eclipse.tracecompass.tmf.core.event.TmfEventType; | |
2bdf0193 AM |
25 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; |
26 | import org.eclipse.tracecompass.tmf.core.trace.ITmfEventParser; | |
27 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
d18dd09b ASL |
28 | |
29 | /** | |
30 | * <b><u>TmfEventParserStub</u></b> | |
31 | * <p> | |
32 | * TODO: Implement me. Please. | |
33 | */ | |
cad06250 | 34 | @SuppressWarnings("javadoc") |
6256d8ad | 35 | public class TmfEventParserStub implements ITmfEventParser { |
d18dd09b | 36 | |
e31e01e8 | 37 | // ------------------------------------------------------------------------ |
d18dd09b | 38 | // Attributes |
e31e01e8 | 39 | // ------------------------------------------------------------------------ |
d18dd09b | 40 | |
085d898f | 41 | private static final int NB_TYPES = 10; |
28b94d61 | 42 | private final TmfEventType[] fTypes; |
6256d8ad | 43 | private final ITmfTrace fEventStream; |
d18dd09b | 44 | |
e31e01e8 | 45 | // ------------------------------------------------------------------------ |
d18dd09b | 46 | // Constructors |
e31e01e8 | 47 | // ------------------------------------------------------------------------ |
d18dd09b | 48 | |
6256d8ad | 49 | public TmfEventParserStub(final ITmfTrace eventStream) { |
7e6347b0 | 50 | fEventStream = eventStream; |
085d898f FC |
51 | fTypes = new TmfEventType[NB_TYPES]; |
52 | for (int i = 0; i < NB_TYPES; i++) { | |
ccf2bbb4 | 53 | final Vector<String> fields = new Vector<>(); |
085d898f FC |
54 | for (int j = 1; j <= i; j++) { |
55 | final String field = "Fmt-" + i + "-Fld-" + j; | |
56 | fields.add(field); | |
57 | } | |
58 | final String[] fieldArray = new String[i]; | |
59 | final ITmfEventField rootField = TmfEventField.makeRoot(fields.toArray(fieldArray)); | |
e600c338 | 60 | fTypes[i] = new TmfEventType("Type-" + i, rootField); |
085d898f | 61 | } |
d18dd09b ASL |
62 | } |
63 | ||
e31e01e8 | 64 | // ------------------------------------------------------------------------ |
d18dd09b | 65 | // Operators |
e31e01e8 | 66 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
67 | |
68 | static final String typePrefix = "Type-"; | |
d4011df2 | 69 | @Override |
2771b032 | 70 | public ITmfEvent parseEvent(final ITmfContext context) { |
d18dd09b | 71 | |
54a7a54c | 72 | if (! (fEventStream instanceof TmfTraceStub)) { |
d18dd09b | 73 | return null; |
54a7a54c | 74 | } |
d18dd09b | 75 | |
085d898f | 76 | // Highly inefficient... |
7e6347b0 | 77 | final RandomAccessFile stream = ((TmfTraceStub) fEventStream).getStream(); |
04add3c4 BH |
78 | if (stream == null) { |
79 | return null; | |
80 | } | |
81 | ||
d5efe032 AF |
82 | // String name = eventStream.getName(); |
83 | // name = name.substring(name.lastIndexOf('/') + 1); | |
085d898f FC |
84 | |
85 | // no need to use synchronized since it's already cover by the calling method | |
86 | ||
87 | long location = 0; | |
0316808c | 88 | if (context != null && context.getLocation() != null) { |
5976d44a | 89 | location = (Long) context.getLocation().getLocationInfo(); |
0316808c FC |
90 | try { |
91 | stream.seek(location); | |
92 | ||
93 | final long ts = stream.readLong(); | |
e1de2fd4 | 94 | stream.readUTF(); /* Previously source, now unused */ |
0316808c | 95 | final String type = stream.readUTF(); |
e1de2fd4 | 96 | stream.readInt(); /* Previously reference, now unused */ |
0316808c FC |
97 | final int typeIndex = Integer.parseInt(type.substring(typePrefix.length())); |
98 | final String[] fields = new String[typeIndex]; | |
54a7a54c | 99 | for (int i = 0; i < typeIndex; i++) { |
0316808c | 100 | fields[i] = stream.readUTF(); |
54a7a54c | 101 | } |
0316808c FC |
102 | |
103 | final StringBuffer content = new StringBuffer("["); | |
54a7a54c | 104 | if (typeIndex > 0) { |
0316808c | 105 | content.append(fields[0]); |
54a7a54c FC |
106 | } |
107 | for (int i = 1; i < typeIndex; i++) { | |
0316808c | 108 | content.append(", ").append(fields[i]); |
54a7a54c | 109 | } |
0316808c FC |
110 | content.append("]"); |
111 | ||
214cc822 | 112 | final TmfEventField root = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, content.toString(), null); |
2771b032 | 113 | final ITmfEvent event = new TmfEvent(fEventStream, |
0c7471fb | 114 | ITmfContext.UNKNOWN_RANK, |
0f19a90f | 115 | fEventStream.createTimestamp(ts * 1000000L), |
e1de2fd4 | 116 | fTypes[typeIndex], root); |
0316808c FC |
117 | return event; |
118 | } catch (final EOFException e) { | |
119 | } catch (final IOException e) { | |
120 | } | |
085d898f | 121 | } |
d18dd09b ASL |
122 | return null; |
123 | } | |
124 | ||
6256d8ad | 125 | } |