| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2013 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 | * Bernd Hufmann - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | package org.eclipse.linuxtools.tmf.ui.tests.uml2sd.trace; |
| 13 | |
| 14 | import java.io.EOFException; |
| 15 | import java.io.IOException; |
| 16 | import java.io.RandomAccessFile; |
| 17 | |
| 18 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 19 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; |
| 20 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
| 21 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
| 22 | import org.eclipse.linuxtools.tmf.core.event.TmfEventType; |
| 23 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; |
| 24 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
| 25 | import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser; |
| 26 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 27 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub; |
| 28 | |
| 29 | /** |
| 30 | * Parser implementation for Uml2SD Test Traces. |
| 31 | * |
| 32 | */ |
| 33 | public class TmfUml2SDTestTrace implements ITmfEventParser { |
| 34 | |
| 35 | ITmfTrace fEventStream; |
| 36 | |
| 37 | /** |
| 38 | * Default Constructor |
| 39 | */ |
| 40 | public TmfUml2SDTestTrace() { |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Constructor |
| 45 | * @param eventStream ITmfTrace implementation |
| 46 | */ |
| 47 | public TmfUml2SDTestTrace(ITmfTrace eventStream) { |
| 48 | fEventStream = eventStream; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @param eventStream ITmfTrace implementation to set |
| 53 | */ |
| 54 | public void setTrace(ITmfTrace eventStream) { |
| 55 | fEventStream = eventStream; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public ITmfEvent parseEvent(ITmfContext context) { |
| 60 | if (! (fEventStream instanceof TmfTraceStub)) { |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | // Highly inefficient... |
| 65 | RandomAccessFile stream = ((TmfTraceStub) fEventStream).getStream(); |
| 66 | |
| 67 | // String name = eventStream.getName(); |
| 68 | // name = name.substring(name.lastIndexOf('/') + 1); |
| 69 | |
| 70 | long location = 0; |
| 71 | if (context != null) { |
| 72 | location = (Long) context.getLocation().getLocationInfo(); |
| 73 | } |
| 74 | |
| 75 | try { |
| 76 | stream.seek(location); |
| 77 | |
| 78 | long ts = stream.readLong(); |
| 79 | String source = stream.readUTF(); |
| 80 | String type = stream.readUTF(); |
| 81 | String reference = stream.readUTF(); |
| 82 | String sender = stream.readUTF(); |
| 83 | String receiver = stream.readUTF(); |
| 84 | String signal = stream.readUTF(); |
| 85 | |
| 86 | String[] labels = {"sender", "receiver", "signal"}; |
| 87 | |
| 88 | TmfEventType tmfEventType = new TmfEventType("UnitTest", type, TmfEventField.makeRoot(labels)); |
| 89 | |
| 90 | String content = "["; |
| 91 | content += sender; |
| 92 | content += "," + receiver; |
| 93 | content += "," + signal; |
| 94 | content += "]"; |
| 95 | |
| 96 | // Pre-parse the content |
| 97 | TmfEventField[] fields = new TmfEventField[3]; |
| 98 | fields[0] = new TmfEventField("sender", sender, null); |
| 99 | fields[1] = new TmfEventField("receiver", receiver, null); |
| 100 | fields[2] = new TmfEventField("signal", signal, null); |
| 101 | |
| 102 | ITmfEventField tmfContent = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, content, fields); |
| 103 | ITmfEvent tmfEvent = new TmfEvent(fEventStream, new TmfTimestamp(ts, -9), source, tmfEventType, tmfContent, reference); |
| 104 | |
| 105 | return tmfEvent; |
| 106 | } catch (final EOFException e) { |
| 107 | } catch (final IOException e) { |
| 108 | } |
| 109 | return null; |
| 110 | } |
| 111 | |
| 112 | } |