1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial implementation
11 * Patrick Tasse - Dispose wrapped trace
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
19 import java
.io
.IOException
;
20 import java
.io
.InputStream
;
22 import java
.util
.Collection
;
24 import javax
.xml
.XMLConstants
;
25 import javax
.xml
.transform
.Source
;
26 import javax
.xml
.transform
.stream
.StreamSource
;
27 import javax
.xml
.validation
.Schema
;
28 import javax
.xml
.validation
.SchemaFactory
;
29 import javax
.xml
.validation
.Validator
;
31 import org
.eclipse
.core
.resources
.IProject
;
32 import org
.eclipse
.core
.resources
.IResource
;
33 import org
.eclipse
.core
.runtime
.IStatus
;
34 import org
.eclipse
.core
.runtime
.Status
;
35 import org
.eclipse
.jdt
.annotation
.NonNull
;
36 import org
.eclipse
.jdt
.annotation
.Nullable
;
37 import org
.eclipse
.osgi
.util
.NLS
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventType
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfContentFieldAspect
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfCpuAspect
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomEventContent
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlEvent
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTrace
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTraceDefinition
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
59 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
60 import org
.xml
.sax
.SAXException
;
62 import com
.google
.common
.collect
.ImmutableList
;
65 * An XML development trace using a custom XML trace definition and schema.
67 * This class will typically be used to build custom traces to unit test more
68 * complex functionalities like analyzes or to develop and test data-driven
71 * This class wraps a custom XML trace and rewrites the returned events in the
72 * getNext() method so that event's fields are the ones defined in <field ... />
73 * elements instead of those defined in the custom XML parser. This way, each
74 * event can have a different set of fields. This class can, for example, mimic
77 * @author Geneviève Bastien
79 public class TmfXmlTraceStub
extends TmfTrace
{
81 private static final String DEVELOPMENT_TRACE_PARSER_PATH
= "TmfXmlDevelopmentTrace.xml"; //$NON-NLS-1$
82 private static final String DEVELOPMENT_TRACE_XSD
= "TmfXmlDevelopmentTrace.xsd"; //$NON-NLS-1$
83 private static final String EMPTY
= ""; //$NON-NLS-1$
85 /* XML elements and attributes names */
86 private static final String EVENT_NAME_FIELD
= "Message"; //$NON-NLS-1$
87 private static final String FIELD_NAMES_FIELD
= "fields"; //$NON-NLS-1$
88 private static final String VALUES_FIELD
= "values"; //$NON-NLS-1$
89 private static final String TYPES_FIELD
= "type"; //$NON-NLS-1$
90 private static final String VALUES_SEPARATOR
= " \\| "; //$NON-NLS-1$
91 private static final String TYPE_INTEGER
= "int"; //$NON-NLS-1$
92 private static final String TYPE_LONG
= "long"; //$NON-NLS-1$
93 private static final String ASPECT_SPECIAL_EVENT
= "set_aspects";
94 private static final String ASPECT_CPU
= "cpu";
96 private static final Long SECONDS_TO_NS
= 1000000000L;
98 private final CustomXmlTrace fTrace
;
100 private Collection
<ITmfEventAspect
> fAspects
;
103 * Constructor. Constructs the custom XML trace with the appropriate
106 public TmfXmlTraceStub() {
108 /* Load custom XML definition */
109 try (InputStream in
= TmfXmlTraceStub
.class.getResourceAsStream(DEVELOPMENT_TRACE_PARSER_PATH
);) {
110 CustomXmlTraceDefinition
[] definitions
= CustomXmlTraceDefinition
.loadAll(in
);
111 if (definitions
.length
== 0) {
112 throw new IllegalStateException("The custom trace definition does not exist"); //$NON-NLS-1$
114 fTrace
= new CustomXmlTrace(definitions
[0]);
115 /* Deregister the custom XML trace */
116 TmfSignalManager
.deregister(fTrace
);
118 Collection
<ITmfEventAspect
> aspects
= TmfTrace
.BASE_ASPECTS
;
120 } catch (IOException e
) {
121 throw new IllegalStateException("Cannot open the trace parser for development traces"); //$NON-NLS-1$
127 public void initTrace(@Nullable IResource resource
, @Nullable String path
, @Nullable Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
128 super.initTrace(resource
, path
, type
);
129 fTrace
.initTrace(resource
, path
, type
);
131 /* Set the start and (current) end times for this trace */
136 ITmfEvent event
= getNext(ctx
);
138 final ITmfTimestamp curTime
= event
.getTimestamp();
139 this.setStartTime(curTime
);
140 this.setEndTime(curTime
);
145 public synchronized void dispose() {
151 public @Nullable ITmfEvent
parseEvent(@Nullable ITmfContext context
) {
152 return fTrace
.parseEvent(context
);
156 public @Nullable ITmfLocation
getCurrentLocation() {
157 return fTrace
.getCurrentLocation();
161 public double getLocationRatio(@Nullable ITmfLocation location
) {
162 return fTrace
.getLocationRatio(location
);
166 public @Nullable ITmfContext
seekEvent(@Nullable ITmfLocation location
) {
167 return fTrace
.seekEvent(location
);
171 public @Nullable ITmfContext
seekEvent(double ratio
) {
172 return fTrace
.seekEvent(ratio
);
176 public IStatus
validate(@Nullable IProject project
, @Nullable String path
) {
177 File xmlFile
= new File(path
);
178 if (!xmlFile
.exists() || !xmlFile
.isFile() || !xmlFile
.canRead()) {
179 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_FileNotFound
, path
));
181 /* Does the XML file validate with the XSD */
182 SchemaFactory schemaFactory
= SchemaFactory
.newInstance(XMLConstants
.W3C_XML_SCHEMA_NS_URI
);
183 Source xmlSource
= new StreamSource(xmlFile
);
186 URL url
= TmfXmlTraceStub
.class.getResource(DEVELOPMENT_TRACE_XSD
);
187 Schema schema
= schemaFactory
.newSchema(url
);
189 Validator validator
= schema
.newValidator();
190 validator
.validate(xmlSource
);
191 } catch (SAXException e
) {
192 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_ValidationError
, path
), e
);
193 } catch (IOException e
) {
194 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_IoError
, path
), e
);
196 @SuppressWarnings("null")
198 IStatus status
= Status
.OK_STATUS
;
202 private static String
getStringValue(ITmfEventField content
, String fieldName
) {
203 ITmfEventField field
= content
.getField(fieldName
);
207 Object val
= field
.getValue();
208 if (!(val
instanceof String
)) {
215 public synchronized @Nullable ITmfEvent
getNext(@Nullable ITmfContext context
) {
216 if (context
== null) {
219 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
220 CustomXmlEvent event
= fTrace
.getNext(context
);
225 /* Translate the content of the event */
226 /* The "fields" field contains a | separated list of field names */
227 /* The "values" field contains a | separated list of field values */
228 /* the "type" field contains a | separated list of field types */
229 ITmfEventField content
= event
.getContent();
230 if (content
== null) {
234 String fieldString
= getStringValue(content
, FIELD_NAMES_FIELD
);
235 String valueString
= getStringValue(content
, VALUES_FIELD
);
236 String typeString
= getStringValue(content
, TYPES_FIELD
);
238 String
[] fields
= fieldString
.split(VALUES_SEPARATOR
);
239 String
[] values
= valueString
.split(VALUES_SEPARATOR
);
240 String
[] types
= typeString
.split(VALUES_SEPARATOR
);
241 ITmfEventField
[] fieldsArray
= new TmfEventField
[fields
.length
];
243 for (int i
= 0; i
< fields
.length
; i
++) {
244 String value
= EMPTY
;
245 if (values
.length
> i
) {
249 if (types
.length
> i
) {
257 val
= Integer
.valueOf(value
);
258 } catch (NumberFormatException e
) {
259 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to integer", value
), e
); //$NON-NLS-1$
266 val
= Long
.valueOf(value
);
267 } catch (NumberFormatException e
) {
268 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to long", value
), e
); //$NON-NLS-1$
277 fieldsArray
[i
] = new TmfEventField(checkNotNull(fields
[i
]), val
, null);
280 /* Generate the aspects for this trace if it is the aspects special event */
281 String eventName
= getStringValue(content
, EVENT_NAME_FIELD
);
282 if (eventName
.equals(ASPECT_SPECIAL_EVENT
)) {
283 generateAspects(fieldsArray
);
284 return getNext(context
);
287 /* Create a new event with new fields and name */
288 ITmfEventType customEventType
= event
.getType();
289 TmfEventType eventType
= new TmfEventType(eventName
, customEventType
.getRootField());
290 ITmfEventField eventFields
= new CustomEventContent(content
.getName(), content
.getValue(), fieldsArray
);
292 * TODO: Timestamps for these traces are in nanos, but since the
293 * CustomXmlTrace does not support this format, the timestamp of the
294 * original is in second and we need to convert it. We should do that at
295 * the source when it is supported
297 ITmfTimestamp timestamp
= new TmfNanoTimestamp(event
.getTimestamp().getValue() / SECONDS_TO_NS
);
298 TmfEvent newEvent
= new TmfEvent(this, ITmfContext
.UNKNOWN_RANK
, timestamp
, eventType
, eventFields
);
299 updateAttributes(savedContext
, event
);
300 context
.increaseRank();
305 private void generateAspects(ITmfEventField
[] fieldsArray
) {
306 ImmutableList
.Builder
<ITmfEventAspect
> builder
= new ImmutableList
.Builder
<>();
308 /* Initialize the first default trace aspects */
309 builder
.add(ITmfEventAspect
.BaseAspects
.TIMESTAMP
);
310 builder
.add(ITmfEventAspect
.BaseAspects
.EVENT_TYPE
);
312 /* Add custom aspects in between */
313 for (ITmfEventField field
: fieldsArray
) {
314 String name
= field
.getName();
315 final ITmfEventAspect aspect
= new TmfContentFieldAspect(name
, name
);
316 if (name
.equals(ASPECT_CPU
)) {
317 builder
.add(new TmfCpuAspect() {
319 public @Nullable Integer
resolve(ITmfEvent event
) {
320 Object result
= aspect
.resolve(event
);
321 if (result
instanceof Number
) {
322 return ((Number
) result
).intValue();
332 /* Add the big content aspect */
333 builder
.add(ITmfEventAspect
.BaseAspects
.CONTENTS
);
334 fAspects
= builder
.build();
338 public Iterable
<ITmfEventAspect
> getEventAspects() {