Re-structure LTTng sub-project as per the Linux Tools guidelines
[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.TmfEvent;
21 import org.eclipse.linuxtools.tmf.core.event.TmfEventContent;
22 import org.eclipse.linuxtools.tmf.core.event.TmfEventReference;
23 import org.eclipse.linuxtools.tmf.core.event.TmfEventSource;
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;
27 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
28 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
29 import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
30
31 /**
32 * <b><u>TmfEventParserStub</u></b>
33 * <p>
34 * TODO: Implement me. Please.
35 */
36 @SuppressWarnings("nls")
37 public class TmfEventParserStub implements ITmfEventParser {
38
39 // ------------------------------------------------------------------------
40 // Attributes
41 // ------------------------------------------------------------------------
42
43 private final int NB_TYPES = 10;
44 private final TmfEventType[] fTypes;
45
46 // ------------------------------------------------------------------------
47 // Constructors
48 // ------------------------------------------------------------------------
49
50 public TmfEventParserStub() {
51 fTypes = new TmfEventType[NB_TYPES];
52 for (int i = 0; i < NB_TYPES; i++) {
53 Vector<String> format = new Vector<String>();
54 for (int j = 1; j <= i; j++) {
55 format.add(new String("Fmt-" + i + "-Fld-" + j));
56 }
57 String[] fields = new String[i];
58 fTypes[i] = new TmfEventType("Type-" + i, format.toArray(fields));
59 }
60 }
61
62 // ------------------------------------------------------------------------
63 // Operators
64 // ------------------------------------------------------------------------
65
66 static final String typePrefix = "Type-";
67 @Override
68 @SuppressWarnings("unchecked")
69 public TmfEvent parseNextEvent(ITmfTrace eventStream, TmfContext context) throws IOException {
70
71 if (! (eventStream instanceof TmfTraceStub)) {
72 return null;
73 }
74
75 // Highly inefficient...
76 RandomAccessFile stream = ((TmfTraceStub) eventStream).getStream();
77 String name = eventStream.getName();
78 name = name.substring(name.lastIndexOf('/') + 1);
79
80 // no need to use synchronized since it's already cover by the calling method
81
82 long location = 0;
83 if (context != null)
84 location = ((TmfLocation<Long>) (context.getLocation())).getLocation();
85 stream.seek(location);
86
87 try {
88 long ts = stream.readLong();
89 String source = stream.readUTF();
90 String type = stream.readUTF();
91 @SuppressWarnings("unused")
92 int reference = stream.readInt();
93 int typeIndex = Integer.parseInt(type.substring(typePrefix.length()));
94 String[] fields = new String[typeIndex];
95 for (int i = 0; i < typeIndex; i++) {
96 fields[i] = stream.readUTF();
97 }
98
99 String content = "[";
100 if (typeIndex > 0) {
101 content += fields[0];
102 }
103 for (int i = 1; i < typeIndex; i++) {
104 content += ", " + fields[i];
105 }
106 content += "]";
107
108 TmfEvent event = new TmfEvent(
109 new TmfTimestamp(ts, (byte) -3, 0), // millisecs
110 new TmfEventSource(source),
111 fTypes[typeIndex],
112 new TmfEventReference(name));
113 TmfEventContent cnt = new TmfEventContent(event, content);
114 event.setContent(cnt);
115 return event;
116 } catch (EOFException e) {
117 }
118 return null;
119 }
120
121 }
This page took 0.033309 seconds and 5 git commands to generate.