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; | |
26 | import org.eclipse.linuxtools.tmf.core.parser.ITmfEventParser; | |
a1440d1f | 27 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
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") |
72f1e62a | 37 | public class TmfEventParserStub implements ITmfEventParser<TmfEvent> { |
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; |
d18dd09b | 45 | |
e31e01e8 | 46 | // ------------------------------------------------------------------------ |
d18dd09b | 47 | // Constructors |
e31e01e8 | 48 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
49 | |
50 | public TmfEventParserStub() { | |
085d898f FC |
51 | fTypes = new TmfEventType[NB_TYPES]; |
52 | for (int i = 0; i < NB_TYPES; i++) { | |
53 | final Vector<String> fields = new Vector<String>(); | |
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)); | |
60 | fTypes[i] = new TmfEventType("UnitTest", "Type-" + i, rootField); | |
61 | } | |
d18dd09b ASL |
62 | } |
63 | ||
e31e01e8 | 64 | // ------------------------------------------------------------------------ |
d18dd09b | 65 | // Operators |
e31e01e8 | 66 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
67 | |
68 | static final String typePrefix = "Type-"; | |
d4011df2 | 69 | @Override |
085d898f FC |
70 | @SuppressWarnings("unchecked") |
71 | public ITmfEvent parseNextEvent(final ITmfTrace<TmfEvent> eventStream, final ITmfContext context) throws IOException { | |
d18dd09b | 72 | |
085d898f | 73 | if (! (eventStream instanceof TmfTraceStub)) |
d18dd09b | 74 | return null; |
d18dd09b | 75 | |
085d898f FC |
76 | // Highly inefficient... |
77 | final RandomAccessFile stream = ((TmfTraceStub) eventStream).getStream(); | |
78 | // String name = eventStream.getName(); | |
79 | // name = name.substring(name.lastIndexOf('/') + 1); | |
80 | ||
81 | // no need to use synchronized since it's already cover by the calling method | |
82 | ||
83 | long location = 0; | |
84 | if (context != null) | |
85 | location = ((TmfLocation<Long>) (context.getLocation())).getLocation(); | |
86 | stream.seek(location); | |
87 | ||
88 | try { | |
89 | final long ts = stream.readLong(); | |
90 | final String source = stream.readUTF(); | |
91 | final String type = stream.readUTF(); | |
92 | final Integer reference = stream.readInt(); | |
93 | final int typeIndex = Integer.parseInt(type.substring(typePrefix.length())); | |
94 | final String[] fields = new String[typeIndex]; | |
95 | for (int i = 0; i < typeIndex; i++) | |
96 | fields[i] = stream.readUTF(); | |
97 | ||
98 | final StringBuffer content = new StringBuffer("["); | |
99 | if (typeIndex > 0) | |
100 | content.append(fields[0]); | |
101 | for (int i = 1; i < typeIndex; i++) | |
102 | content.append(", ").append(fields[i]); | |
103 | content.append("]"); | |
104 | ||
105 | final TmfEventField root = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, content.toString()); | |
106 | final ITmfEvent event = new TmfEvent(eventStream, | |
107 | new TmfTimestamp(ts, -3, 0), // millisecs | |
108 | source, fTypes[typeIndex], root, reference.toString()); | |
109 | return event; | |
110 | } catch (final EOFException e) { | |
111 | } | |
d18dd09b ASL |
112 | return null; |
113 | } | |
114 | ||
e31e01e8 | 115 | } |