General improvements:
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.tests / stubs / org / eclipse / linuxtools / tmf / 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.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.event.TmfEvent;
21 import org.eclipse.linuxtools.tmf.event.TmfEventContent;
22 import org.eclipse.linuxtools.tmf.event.TmfEventFormat;
23 import org.eclipse.linuxtools.tmf.event.TmfEventReference;
24 import org.eclipse.linuxtools.tmf.event.TmfEventSource;
25 import org.eclipse.linuxtools.tmf.event.TmfEventType;
26 import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
27
28 /**
29 * <b><u>TmfEventParserStub</u></b>
30 * <p>
31 * TODO: Implement me. Please.
32 */
33 public class TmfEventParserStub implements ITmfEventParser {
34
35 // ========================================================================
36 // Attributes
37 // ========================================================================
38
39 private final int NB_FORMATS = 10;
40 private final TmfEventFormat[] fFormats;
41
42 // ========================================================================
43 // Constructors
44 // ========================================================================
45
46 public TmfEventParserStub() {
47 fFormats = new TmfEventFormat[NB_FORMATS];
48 for (int i = 0; i < NB_FORMATS; i++) {
49 Vector<String> format = new Vector<String>();
50 for (int j = 1; j <= i; j++) {
51 format.add(new String("Fmt-" + i + "-Fld-" + j));
52 }
53 String[] fields = new String[i];
54 fFormats[i] = new TmfEventFormat(format.toArray(fields));
55 }
56 }
57
58 // ========================================================================
59 // Operators
60 // ========================================================================
61
62 /* (non-Javadoc)
63 * @see org.eclipse.linuxtools.tmf.eventlog.ITmfEventParser#parseNextEvent()
64 */
65 static final String typePrefix = "Type-";
66 public TmfEvent parseNextEvent(ITmfTrace eventStream, TmfTraceContext context) throws IOException {
67
68 if (! (eventStream instanceof TmfTraceStub)) {
69 return null;
70 }
71
72 // Highly inefficient...
73 RandomAccessFile stream = ((TmfTraceStub) eventStream).getStream();
74 String name = eventStream.getName();
75 name = name.substring(name.lastIndexOf('/') + 1);
76
77 synchronized(stream) {
78 long location = 0;
79 if (context != null)
80 location = (Long) (context.getLocation());
81 stream.seek(location);
82
83 try {
84 long ts = stream.readLong();
85 String source = stream.readUTF();
86 String type = stream.readUTF();
87 @SuppressWarnings("unused")
88 int reference = stream.readInt();
89 int typeIndex = Integer.parseInt(type.substring(typePrefix.length()));
90 String[] fields = new String[typeIndex];
91 for (int i = 0; i < typeIndex; i++) {
92 fields[i] = stream.readUTF();
93 }
94
95 // Update the context
96 context.setLocation(stream.getFilePointer());
97 context.incrIndex();
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 new TmfEventType(type, fFormats[typeIndex]),
112 new TmfEventContent(content, fFormats[typeIndex]),
113 new TmfEventReference(name));
114 return event;
115 } catch (EOFException e) {
116 }
117 }
118 return null;
119 }
120
121 }
This page took 0.03991 seconds and 6 git commands to generate.