tmf: Annotate methods in ITmfEventField
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / stubs / org / eclipse / tracecompass / tmf / tests / stubs / trace / xml / TmfXmlTraceStub.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal
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 * Geneviève Bastien - Initial implementation
11 * Patrick Tasse - Dispose wrapped trace
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.tests.stubs.trace.xml;
15
16 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.URL;
22 import java.util.Collection;
23
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;
30
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;
61
62 import com.google.common.collect.ImmutableList;
63
64 /**
65 * An XML development trace using a custom XML trace definition and schema.
66 *
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
69 * analyzes.
70 *
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
75 * a CTF trace.
76 *
77 * @author Geneviève Bastien
78 */
79 public class TmfXmlTraceStub extends TmfTrace {
80
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$
84
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";
95
96 private static final Long SECONDS_TO_NS = 1000000000L;
97
98 private final CustomXmlTrace fTrace;
99
100 private Collection<ITmfEventAspect> fAspects;
101
102 /**
103 * Constructor. Constructs the custom XML trace with the appropriate
104 * definition.
105 */
106 public TmfXmlTraceStub() {
107
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$
113 }
114 fTrace = new CustomXmlTrace(definitions[0]);
115 /* Deregister the custom XML trace */
116 TmfSignalManager.deregister(fTrace);
117
118 Collection<ITmfEventAspect> aspects = TmfTrace.BASE_ASPECTS;
119 fAspects = aspects;
120 } catch (IOException e) {
121 throw new IllegalStateException("Cannot open the trace parser for development traces"); //$NON-NLS-1$
122 }
123
124 }
125
126 @Override
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);
130 ITmfContext ctx;
131 /* Set the start and (current) end times for this trace */
132 ctx = seekEvent(0L);
133 if (ctx == null) {
134 return;
135 }
136 ITmfEvent event = getNext(ctx);
137 if (event != null) {
138 final ITmfTimestamp curTime = event.getTimestamp();
139 this.setStartTime(curTime);
140 this.setEndTime(curTime);
141 }
142 }
143
144 @Override
145 public synchronized void dispose() {
146 super.dispose();
147 fTrace.dispose();
148 }
149
150 @Override
151 public @Nullable ITmfEvent parseEvent(@Nullable ITmfContext context) {
152 return fTrace.parseEvent(context);
153 }
154
155 @Override
156 public @Nullable ITmfLocation getCurrentLocation() {
157 return fTrace.getCurrentLocation();
158 }
159
160 @Override
161 public double getLocationRatio(@Nullable ITmfLocation location) {
162 return fTrace.getLocationRatio(location);
163 }
164
165 @Override
166 public @Nullable ITmfContext seekEvent(@Nullable ITmfLocation location) {
167 return fTrace.seekEvent(location);
168 }
169
170 @Override
171 public @Nullable ITmfContext seekEvent(double ratio) {
172 return fTrace.seekEvent(ratio);
173 }
174
175 @Override
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));
180 }
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);
184
185 try {
186 URL url = TmfXmlTraceStub.class.getResource(DEVELOPMENT_TRACE_XSD);
187 Schema schema = schemaFactory.newSchema(url);
188
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);
195 }
196 @SuppressWarnings("null")
197 @NonNull
198 IStatus status = Status.OK_STATUS;
199 return status;
200 }
201
202 private static String getStringValue(ITmfEventField content, String fieldName) {
203 ITmfEventField field = content.getField(fieldName);
204 if (field == null) {
205 return EMPTY;
206 }
207 Object val = field.getValue();
208 if (!(val instanceof String)) {
209 return EMPTY;
210 }
211 return (String) val;
212 }
213
214 @Override
215 public synchronized @Nullable ITmfEvent getNext(@Nullable ITmfContext context) {
216 if (context == null) {
217 return null;
218 }
219 final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank());
220 CustomXmlEvent event = fTrace.getNext(context);
221 if (event == null) {
222 return null;
223 }
224
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) {
231 return null;
232 }
233
234 String fieldString = getStringValue(content, FIELD_NAMES_FIELD);
235 String valueString = getStringValue(content, VALUES_FIELD);
236 String typeString = getStringValue(content, TYPES_FIELD);
237
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];
242
243 for (int i = 0; i < fields.length; i++) {
244 String value = EMPTY;
245 if (values.length > i) {
246 value = values[i];
247 }
248 String type = null;
249 if (types.length > i) {
250 type = types[i];
251 }
252 Object val = value;
253 if (type != null) {
254 switch (type) {
255 case TYPE_INTEGER: {
256 try {
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$
260 val = 0;
261 }
262 break;
263 }
264 case TYPE_LONG: {
265 try {
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$
269 val = 0L;
270 }
271 break;
272 }
273 default:
274 break;
275 }
276 }
277 fieldsArray[i] = new TmfEventField(checkNotNull(fields[i]), val, null);
278 }
279
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);
285 }
286
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);
291 /*
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
296 */
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();
301
302 return newEvent;
303 }
304
305 private void generateAspects(ITmfEventField[] fieldsArray) {
306 ImmutableList.Builder<ITmfEventAspect> builder = new ImmutableList.Builder<>();
307
308 /* Initialize the first default trace aspects */
309 builder.add(ITmfEventAspect.BaseAspects.TIMESTAMP);
310 builder.add(ITmfEventAspect.BaseAspects.EVENT_TYPE);
311
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() {
318 @Override
319 public @Nullable Integer resolve(ITmfEvent event) {
320 Object result = aspect.resolve(event);
321 if (result instanceof Number) {
322 return ((Number) result).intValue();
323 }
324 return null;
325 }
326 });
327 } else {
328 builder.add(aspect);
329 }
330 }
331
332 /* Add the big content aspect */
333 builder.add(ITmfEventAspect.BaseAspects.CONTENTS);
334
335 @SuppressWarnings("null")
336 @NonNull Collection<ITmfEventAspect> aspectList = builder.build();
337 fAspects = aspectList;
338 }
339
340 @Override
341 public Iterable<ITmfEventAspect> getEventAspects() {
342 return fAspects;
343 }
344
345 }
This page took 0.040906 seconds and 6 git commands to generate.