Commit | Line | Data |
---|---|---|
d18dd09b ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
4918b8f2 | 13 | package org.eclipse.linuxtools.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 | ||
72f1e62a | 20 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
4c564a2d | 21 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
6c13869b | 22 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
4c564a2d | 23 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
6c13869b FC |
24 | import org.eclipse.linuxtools.tmf.core.event.TmfEventType; |
25 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; | |
a1440d1f | 26 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
7e6347b0 | 27 | import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser; |
6c13869b | 28 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
6c13869b | 29 | import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; |
d18dd09b ASL |
30 | |
31 | /** | |
32 | * <b><u>TmfEventParserStub</u></b> | |
33 | * <p> | |
34 | * TODO: Implement me. Please. | |
35 | */ | |
3b38ea61 | 36 | @SuppressWarnings("nls") |
7e6347b0 | 37 | public class TmfEventParserStub implements ITmfEventParser<ITmfEvent> { |
d18dd09b | 38 | |
e31e01e8 | 39 | // ------------------------------------------------------------------------ |
d18dd09b | 40 | // Attributes |
e31e01e8 | 41 | // ------------------------------------------------------------------------ |
d18dd09b | 42 | |
085d898f | 43 | private static final int NB_TYPES = 10; |
28b94d61 | 44 | private final TmfEventType[] fTypes; |
7e6347b0 | 45 | private ITmfTrace<TmfEvent> fEventStream; |
d18dd09b | 46 | |
e31e01e8 | 47 | // ------------------------------------------------------------------------ |
d18dd09b | 48 | // Constructors |
e31e01e8 | 49 | // ------------------------------------------------------------------------ |
d18dd09b | 50 | |
7e6347b0 FC |
51 | public TmfEventParserStub(final ITmfTrace<TmfEvent> eventStream) { |
52 | fEventStream = eventStream; | |
085d898f FC |
53 | fTypes = new TmfEventType[NB_TYPES]; |
54 | for (int i = 0; i < NB_TYPES; i++) { | |
55 | final Vector<String> fields = new Vector<String>(); | |
56 | for (int j = 1; j <= i; j++) { | |
57 | final String field = "Fmt-" + i + "-Fld-" + j; | |
58 | fields.add(field); | |
59 | } | |
60 | final String[] fieldArray = new String[i]; | |
61 | final ITmfEventField rootField = TmfEventField.makeRoot(fields.toArray(fieldArray)); | |
62 | fTypes[i] = new TmfEventType("UnitTest", "Type-" + i, rootField); | |
63 | } | |
d18dd09b ASL |
64 | } |
65 | ||
e31e01e8 | 66 | // ------------------------------------------------------------------------ |
d18dd09b | 67 | // Operators |
e31e01e8 | 68 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
69 | |
70 | static final String typePrefix = "Type-"; | |
d4011df2 | 71 | @Override |
085d898f | 72 | @SuppressWarnings("unchecked") |
7e6347b0 | 73 | public ITmfEvent parseEvent(final ITmfContext context) { |
d18dd09b | 74 | |
7e6347b0 | 75 | if (! (fEventStream instanceof TmfTraceStub)) |
d18dd09b | 76 | return null; |
d18dd09b | 77 | |
085d898f | 78 | // Highly inefficient... |
7e6347b0 | 79 | final RandomAccessFile stream = ((TmfTraceStub) fEventStream).getStream(); |
085d898f FC |
80 | // String name = eventStream.getName(); |
81 | // name = name.substring(name.lastIndexOf('/') + 1); | |
82 | ||
83 | // no need to use synchronized since it's already cover by the calling method | |
84 | ||
85 | long location = 0; | |
86 | if (context != null) | |
87 | location = ((TmfLocation<Long>) (context.getLocation())).getLocation(); | |
085d898f FC |
88 | |
89 | try { | |
7e6347b0 FC |
90 | stream.seek(location); |
91 | ||
085d898f FC |
92 | final long ts = stream.readLong(); |
93 | final String source = stream.readUTF(); | |
94 | final String type = stream.readUTF(); | |
95 | final Integer reference = stream.readInt(); | |
96 | final int typeIndex = Integer.parseInt(type.substring(typePrefix.length())); | |
97 | final String[] fields = new String[typeIndex]; | |
98 | for (int i = 0; i < typeIndex; i++) | |
99 | fields[i] = stream.readUTF(); | |
100 | ||
101 | final StringBuffer content = new StringBuffer("["); | |
102 | if (typeIndex > 0) | |
103 | content.append(fields[0]); | |
104 | for (int i = 1; i < typeIndex; i++) | |
105 | content.append(", ").append(fields[i]); | |
106 | content.append("]"); | |
107 | ||
108 | final TmfEventField root = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, content.toString()); | |
7e6347b0 | 109 | final ITmfEvent event = new TmfEvent(fEventStream, |
085d898f FC |
110 | new TmfTimestamp(ts, -3, 0), // millisecs |
111 | source, fTypes[typeIndex], root, reference.toString()); | |
112 | return event; | |
113 | } catch (final EOFException e) { | |
7e6347b0 | 114 | } catch (final IOException e) { |
085d898f | 115 | } |
d18dd09b ASL |
116 | return null; |
117 | } | |
118 | ||
e31e01e8 | 119 | } |