1 /*******************************************************************************
2 * Copyright (c) 2014 É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 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
;
16 import java
.io
.IOException
;
17 import java
.io
.InputStream
;
19 import java
.util
.Collection
;
21 import javax
.xml
.XMLConstants
;
22 import javax
.xml
.transform
.Source
;
23 import javax
.xml
.transform
.stream
.StreamSource
;
24 import javax
.xml
.validation
.Schema
;
25 import javax
.xml
.validation
.SchemaFactory
;
26 import javax
.xml
.validation
.Validator
;
28 import org
.eclipse
.core
.resources
.IProject
;
29 import org
.eclipse
.core
.resources
.IResource
;
30 import org
.eclipse
.core
.runtime
.IStatus
;
31 import org
.eclipse
.core
.runtime
.Status
;
32 import org
.eclipse
.jdt
.annotation
.NonNull
;
33 import org
.eclipse
.jdt
.annotation
.Nullable
;
34 import org
.eclipse
.osgi
.util
.NLS
;
35 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventType
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfCpuAspect
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfEventFieldAspect
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomEventContent
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlEvent
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTrace
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
.CustomXmlTraceDefinition
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
56 import org
.xml
.sax
.SAXException
;
58 import com
.google
.common
.collect
.ImmutableList
;
59 import com
.google
.common
.primitives
.Ints
;
62 * An XML development trace using a custom XML trace definition and schema.
64 * This class will typically be used to build custom traces to unit test more
65 * complex functionalities like analyzes or to develop and test data-driven
68 * This class wraps a custom XML trace and rewrites the returned events in the
69 * getNext() method so that event's fields are the ones defined in <field ... />
70 * elements instead of those defined in the custom XML parser. This way, each
71 * event can have a different set of fields. This class can, for example, mimic
74 * @author Geneviève Bastien
76 public class TmfXmlTraceStub
extends TmfTrace
{
78 private static final String DEVELOPMENT_TRACE_PARSER_PATH
= "TmfXmlDevelopmentTrace.xml"; //$NON-NLS-1$
79 private static final String DEVELOPMENT_TRACE_XSD
= "TmfXmlDevelopmentTrace.xsd"; //$NON-NLS-1$
80 private static final String EMPTY
= ""; //$NON-NLS-1$
82 /* XML elements and attributes names */
83 private static final String EVENT_NAME_FIELD
= "Message"; //$NON-NLS-1$
84 private static final String FIELD_NAMES_FIELD
= "fields"; //$NON-NLS-1$
85 private static final String VALUES_FIELD
= "values"; //$NON-NLS-1$
86 private static final String TYPES_FIELD
= "type"; //$NON-NLS-1$
87 private static final String VALUES_SEPARATOR
= " \\| "; //$NON-NLS-1$
88 private static final String TYPE_INTEGER
= "int"; //$NON-NLS-1$
89 private static final String TYPE_LONG
= "long"; //$NON-NLS-1$
90 private static final String ASPECT_SPECIAL_EVENT
= "set_aspects";
91 private static final String ASPECT_CPU
= "cpu";
93 private final CustomXmlTrace fTrace
;
95 private Collection
<ITmfEventAspect
> fAspects
;
98 * Constructor. Constructs the custom XML trace with the appropriate
101 public TmfXmlTraceStub() {
103 /* Load custom XML definition */
104 try (InputStream in
= TmfXmlTraceStub
.class.getResourceAsStream(DEVELOPMENT_TRACE_PARSER_PATH
);) {
105 CustomXmlTraceDefinition
[] definitions
= CustomXmlTraceDefinition
.loadAll(in
);
106 if (definitions
.length
== 0) {
107 throw new IllegalStateException("The custom trace definition does not exist"); //$NON-NLS-1$
109 fTrace
= new CustomXmlTrace(definitions
[0]);
110 /* Deregister the custom XML trace */
111 TmfSignalManager
.deregister(fTrace
);
112 this.setParser(fTrace
);
114 Collection
<ITmfEventAspect
> aspects
= TmfTrace
.BASE_ASPECTS
;
116 } catch (IOException e
) {
117 throw new IllegalStateException("Cannot open the trace parser for development traces"); //$NON-NLS-1$
123 public void initTrace(@Nullable IResource resource
, @Nullable String path
, @Nullable Class
<?
extends ITmfEvent
> type
) throws TmfTraceException
{
124 super.initTrace(resource
, path
, type
);
125 fTrace
.initTrace(resource
, path
, type
);
127 /* Set the start and (current) end times for this trace */
132 ITmfEvent event
= getNext(ctx
);
134 final ITmfTimestamp curTime
= event
.getTimestamp();
135 this.setStartTime(curTime
);
136 this.setEndTime(curTime
);
141 public @Nullable ITmfLocation
getCurrentLocation() {
142 return fTrace
.getCurrentLocation();
146 public double getLocationRatio(@Nullable ITmfLocation location
) {
147 return fTrace
.getLocationRatio(location
);
151 public @Nullable ITmfContext
seekEvent(@Nullable ITmfLocation location
) {
152 return fTrace
.seekEvent(location
);
156 public @Nullable ITmfContext
seekEvent(double ratio
) {
157 return fTrace
.seekEvent(ratio
);
161 public IStatus
validate(@Nullable IProject project
, @Nullable String path
) {
162 File xmlFile
= new File(path
);
163 if (!xmlFile
.exists() || !xmlFile
.isFile() || !xmlFile
.canRead()) {
164 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_FileNotFound
, path
));
166 /* Does the XML file validate with the XSD */
167 SchemaFactory schemaFactory
= SchemaFactory
.newInstance(XMLConstants
.W3C_XML_SCHEMA_NS_URI
);
168 Source xmlSource
= new StreamSource(xmlFile
);
171 URL url
= TmfXmlTraceStub
.class.getResource(DEVELOPMENT_TRACE_XSD
);
172 Schema schema
= schemaFactory
.newSchema(url
);
174 Validator validator
= schema
.newValidator();
175 validator
.validate(xmlSource
);
176 } catch (SAXException e
) {
177 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_ValidationError
, path
), e
);
178 } catch (IOException e
) {
179 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, NLS
.bind(org
.eclipse
.tracecompass
.tmf
.tests
.stubs
.trace
.xml
.Messages
.TmfDevelopmentTrace_IoError
, path
), e
);
181 @SuppressWarnings("null")
182 @NonNull IStatus status
= Status
.OK_STATUS
;
186 private static String
getStringValue(ITmfEventField content
, String fieldName
) {
187 ITmfEventField field
= content
.getField(fieldName
);
191 Object val
= field
.getValue();
192 if (!(val
instanceof String
)) {
199 public synchronized @Nullable ITmfEvent
getNext(@Nullable ITmfContext context
) {
200 if (context
== null) {
203 final ITmfContext savedContext
= new TmfContext(context
.getLocation(), context
.getRank());
204 CustomXmlEvent event
= fTrace
.getNext(context
);
209 /* Translate the content of the event */
210 /* The "fields" field contains a | separated list of field names */
211 /* The "values" field contains a | separated list of field values */
212 /* the "type" field contains a | separated list of field types */
213 ITmfEventField content
= event
.getContent();
214 if (content
== null) {
218 String fieldString
= getStringValue(content
, FIELD_NAMES_FIELD
);
219 String valueString
= getStringValue(content
, VALUES_FIELD
);
220 String typeString
= getStringValue(content
, TYPES_FIELD
);
222 String
[] fields
= fieldString
.split(VALUES_SEPARATOR
);
223 String
[] values
= valueString
.split(VALUES_SEPARATOR
);
224 String
[] types
= typeString
.split(VALUES_SEPARATOR
);
225 ITmfEventField
[] fieldsArray
= new TmfEventField
[fields
.length
];
227 for (int i
= 0; i
< fields
.length
; i
++) {
228 String value
= EMPTY
;
229 if (values
.length
> i
) {
233 if (types
.length
> i
) {
241 val
= Integer
.valueOf(value
);
242 } catch (NumberFormatException e
) {
243 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to integer", value
), e
); //$NON-NLS-1$
250 val
= Long
.valueOf(value
);
251 } catch (NumberFormatException e
) {
252 Activator
.logError(String
.format("Get next XML event: cannot cast value %s to long", value
), e
); //$NON-NLS-1$
261 fieldsArray
[i
] = new TmfEventField(fields
[i
], val
, null);
264 /* Generate the aspects for this trace if it is the aspects special event */
265 String eventName
= getStringValue(content
, EVENT_NAME_FIELD
);
266 if (eventName
.equals(ASPECT_SPECIAL_EVENT
)) {
267 generateAspects(fieldsArray
);
268 return getNext(context
);
271 /* Create a new event with new fields and name */
272 ITmfEventType customEventType
= event
.getType();
273 TmfEventType eventType
= new TmfEventType(eventName
, customEventType
.getRootField());
274 ITmfEventField eventFields
= new CustomEventContent(content
.getName(), content
.getValue(), fieldsArray
);
275 // FIXME We used to use getSource() to get the CPU. Now this will have
276 // to be done differently.
277 TmfEvent newEvent
= new TmfEvent(this, ITmfContext
.UNKNOWN_RANK
, event
.getTimestamp(), eventType
, eventFields
);
278 updateAttributes(savedContext
, event
.getTimestamp());
279 context
.increaseRank();
284 private static final class XmlStubCpuAspect
extends TmfCpuAspect
{
286 private final TmfEventFieldAspect fAspect
;
288 public XmlStubCpuAspect(TmfEventFieldAspect aspect
) {
293 public @Nullable String
getFilterId() {
298 public Integer
resolve(ITmfEvent event
) {
299 Integer cpu
= Ints
.tryParse(fAspect
.resolve(event
));
301 return TmfCpuAspect
.CPU_UNAVAILABLE
;
308 private void generateAspects(ITmfEventField
[] fieldsArray
) {
309 ImmutableList
.Builder
<ITmfEventAspect
> builder
= new ImmutableList
.Builder
<>();
311 /* Initialize the first default trace aspects */
312 builder
.add(ITmfEventAspect
.BaseAspects
.TIMESTAMP
);
313 builder
.add(ITmfEventAspect
.BaseAspects
.EVENT_TYPE
);
315 /* Add custom aspects in between */
316 for (ITmfEventField field
: fieldsArray
) {
317 String name
= field
.getName();
321 ITmfEventAspect aspect
= new TmfEventFieldAspect(name
, name
);
322 if (name
.equals(ASPECT_CPU
)) {
323 aspect
= new XmlStubCpuAspect((TmfEventFieldAspect
) aspect
);
328 /* Add the big content aspect */
329 builder
.add(ITmfEventAspect
.BaseAspects
.CONTENTS
);
331 @SuppressWarnings("null")
332 @NonNull Collection
<ITmfEventAspect
> aspectList
= builder
.build();
333 fAspects
= aspectList
;
337 public Iterable
<ITmfEventAspect
> getEventAspects() {