Implement the new TMF Event Model
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / stubs / org / eclipse / linuxtools / tmf / stubs / trace / TmfEventParserStub.java
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
13 package org.eclipse.linuxtools.tmf.stubs.trace;
14
15 import java.io.EOFException;
16 import java.io.IOException;
17 import java.io.RandomAccessFile;
18 import java.util.Vector;
19
20 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
21 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
22 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
23 import org.eclipse.linuxtools.tmf.core.event.TmfEventType;
24 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
25 import org.eclipse.linuxtools.tmf.core.parser.ITmfEventParser;
26 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
27 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
28 import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
29
30 /**
31 * <b><u>TmfEventParserStub</u></b>
32 * <p>
33 * TODO: Implement me. Please.
34 */
35 @SuppressWarnings("nls")
36 public class TmfEventParserStub implements ITmfEventParser {
37
38 // ------------------------------------------------------------------------
39 // Attributes
40 // ------------------------------------------------------------------------
41
42 private final int NB_TYPES = 10;
43 private final TmfEventType[] fTypes;
44
45 // ------------------------------------------------------------------------
46 // Constructors
47 // ------------------------------------------------------------------------
48
49 public TmfEventParserStub() {
50 fTypes = new TmfEventType[NB_TYPES];
51 for (int i = 0; i < NB_TYPES; i++) {
52 Vector<String> fields = new Vector<String>();
53 for (int j = 1; j <= i; j++) {
54 String field = "Fmt-" + i + "-Fld-" + j;
55 fields.add(field);
56 }
57 String[] fieldArray = new String[i];
58 ITmfEventField rootField = TmfEventField.makeRoot(fields.toArray(fieldArray));
59 fTypes[i] = new TmfEventType("UnitTest", "Type-" + i, rootField);
60 }
61 }
62
63 // ------------------------------------------------------------------------
64 // Operators
65 // ------------------------------------------------------------------------
66
67 static final String typePrefix = "Type-";
68 @Override
69 @SuppressWarnings("unchecked")
70 public TmfEvent parseNextEvent(ITmfTrace<?> eventStream, TmfContext context) throws IOException {
71
72 if (! (eventStream instanceof TmfTraceStub)) {
73 return null;
74 }
75
76 // Highly inefficient...
77 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 long ts = stream.readLong();
90 String source = stream.readUTF();
91 String type = stream.readUTF();
92 @SuppressWarnings("unused")
93 int reference = stream.readInt();
94 int typeIndex = Integer.parseInt(type.substring(typePrefix.length()));
95 String[] fields = new String[typeIndex];
96 for (int i = 0; i < typeIndex; i++) {
97 fields[i] = stream.readUTF();
98 }
99
100 String content = "[";
101 if (typeIndex > 0) {
102 content += fields[0];
103 }
104 for (int i = 1; i < typeIndex; i++) {
105 content += ", " + fields[i];
106 }
107 content += "]";
108
109 TmfEventField root = new TmfEventField(ITmfEventField.ROOT_ID, content);
110 TmfEvent event = new TmfEvent(eventStream,
111 new TmfTimestamp(ts, (byte) -3, 0), // millisecs
112 source, fTypes[typeIndex], root, name);
113 event.setContent(root);
114 return event;
115 } catch (EOFException e) {
116 }
117 return null;
118 }
119
120 }
This page took 0.044263 seconds and 6 git commands to generate.