Commit | Line | Data |
---|---|---|
1d8ab692 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2014, 2015 École Polytechnique de Montréal |
1d8ab692 GB |
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 | |
a62b0a0d | 11 | * Patrick Tasse - Dispose wrapped trace |
1d8ab692 GB |
12 | *******************************************************************************/ |
13 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.tests.stubs.trace.xml; |
1d8ab692 | 15 | |
fafdd006 AM |
16 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
17 | ||
1d8ab692 GB |
18 | import java.io.File; |
19 | import java.io.IOException; | |
20 | import java.io.InputStream; | |
21 | import java.net.URL; | |
69ff4f26 | 22 | import java.util.Collection; |
1d8ab692 GB |
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; | |
56e9f830 | 35 | import org.eclipse.jdt.annotation.NonNull; |
69ff4f26 | 36 | import org.eclipse.jdt.annotation.Nullable; |
1d8ab692 | 37 | import org.eclipse.osgi.util.NLS; |
2bdf0193 AM |
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; | |
69ff4f26 | 45 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; |
40dfafb3 | 46 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfContentFieldAspect; |
69ff4f26 | 47 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect; |
2bdf0193 AM |
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; | |
12ca2c10 | 55 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp; |
2bdf0193 AM |
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; | |
1d8ab692 GB |
60 | import org.xml.sax.SAXException; |
61 | ||
69ff4f26 | 62 | import com.google.common.collect.ImmutableList; |
69ff4f26 | 63 | |
1d8ab692 GB |
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$ | |
1d8ab692 GB |
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$ | |
69ff4f26 GB |
93 | private static final String ASPECT_SPECIAL_EVENT = "set_aspects"; |
94 | private static final String ASPECT_CPU = "cpu"; | |
1d8ab692 | 95 | |
12ca2c10 GB |
96 | private static final Long SECONDS_TO_NS = 1000000000L; |
97 | ||
1d8ab692 GB |
98 | private final CustomXmlTrace fTrace; |
99 | ||
69ff4f26 GB |
100 | private Collection<ITmfEventAspect> fAspects; |
101 | ||
1d8ab692 GB |
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); | |
69ff4f26 GB |
117 | |
118 | Collection<ITmfEventAspect> aspects = TmfTrace.BASE_ASPECTS; | |
119 | fAspects = aspects; | |
1d8ab692 GB |
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 | |
69ff4f26 | 127 | public void initTrace(@Nullable IResource resource, @Nullable String path, @Nullable Class<? extends ITmfEvent> type) throws TmfTraceException { |
1d8ab692 GB |
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); | |
69ff4f26 GB |
133 | if (ctx == null) { |
134 | return; | |
135 | } | |
1d8ab692 GB |
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 | ||
a62b0a0d PT |
144 | @Override |
145 | public synchronized void dispose() { | |
146 | super.dispose(); | |
147 | fTrace.dispose(); | |
148 | } | |
149 | ||
5733be39 AM |
150 | @Override |
151 | public @Nullable ITmfEvent parseEvent(@Nullable ITmfContext context) { | |
152 | return fTrace.parseEvent(context); | |
153 | } | |
154 | ||
1d8ab692 | 155 | @Override |
69ff4f26 | 156 | public @Nullable ITmfLocation getCurrentLocation() { |
1d8ab692 GB |
157 | return fTrace.getCurrentLocation(); |
158 | } | |
159 | ||
160 | @Override | |
69ff4f26 | 161 | public double getLocationRatio(@Nullable ITmfLocation location) { |
1d8ab692 GB |
162 | return fTrace.getLocationRatio(location); |
163 | } | |
164 | ||
165 | @Override | |
69ff4f26 | 166 | public @Nullable ITmfContext seekEvent(@Nullable ITmfLocation location) { |
1d8ab692 GB |
167 | return fTrace.seekEvent(location); |
168 | } | |
169 | ||
170 | @Override | |
69ff4f26 | 171 | public @Nullable ITmfContext seekEvent(double ratio) { |
1d8ab692 GB |
172 | return fTrace.seekEvent(ratio); |
173 | } | |
174 | ||
175 | @Override | |
69ff4f26 | 176 | public IStatus validate(@Nullable IProject project, @Nullable String path) { |
1d8ab692 GB |
177 | File xmlFile = new File(path); |
178 | if (!xmlFile.exists() || !xmlFile.isFile() || !xmlFile.canRead()) { | |
2bdf0193 | 179 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_FileNotFound, path)); |
1d8ab692 GB |
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) { | |
2bdf0193 | 192 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_ValidationError, path), e); |
1d8ab692 | 193 | } catch (IOException e) { |
2bdf0193 | 194 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_IoError, path), e); |
1d8ab692 | 195 | } |
69ff4f26 | 196 | @SuppressWarnings("null") |
12ca2c10 GB |
197 | @NonNull |
198 | IStatus status = Status.OK_STATUS; | |
69ff4f26 | 199 | return status; |
1d8ab692 GB |
200 | } |
201 | ||
69ff4f26 | 202 | private static String getStringValue(ITmfEventField content, String fieldName) { |
56e9f830 GB |
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 | ||
1d8ab692 | 214 | @Override |
69ff4f26 GB |
215 | public synchronized @Nullable ITmfEvent getNext(@Nullable ITmfContext context) { |
216 | if (context == null) { | |
217 | return null; | |
218 | } | |
1d8ab692 GB |
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(); | |
56e9f830 GB |
230 | if (content == null) { |
231 | return null; | |
232 | } | |
69ff4f26 | 233 | |
56e9f830 GB |
234 | String fieldString = getStringValue(content, FIELD_NAMES_FIELD); |
235 | String valueString = getStringValue(content, VALUES_FIELD); | |
236 | String typeString = getStringValue(content, TYPES_FIELD); | |
1d8ab692 GB |
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 | } | |
fafdd006 | 277 | fieldsArray[i] = new TmfEventField(checkNotNull(fields[i]), val, null); |
1d8ab692 GB |
278 | } |
279 | ||
69ff4f26 GB |
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 | ||
1d8ab692 GB |
287 | /* Create a new event with new fields and name */ |
288 | ITmfEventType customEventType = event.getType(); | |
69ff4f26 | 289 | TmfEventType eventType = new TmfEventType(eventName, customEventType.getRootField()); |
1d8ab692 | 290 | ITmfEventField eventFields = new CustomEventContent(content.getName(), content.getValue(), fieldsArray); |
12ca2c10 GB |
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); | |
5904c11e | 299 | updateAttributes(savedContext, event); |
1d8ab692 GB |
300 | context.increaseRank(); |
301 | ||
302 | return newEvent; | |
303 | } | |
304 | ||
69ff4f26 GB |
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(); | |
40dfafb3 | 315 | final ITmfEventAspect aspect = new TmfContentFieldAspect(name, name); |
69ff4f26 | 316 | if (name.equals(ASPECT_CPU)) { |
40dfafb3 PT |
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); | |
69ff4f26 | 329 | } |
69ff4f26 GB |
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 | ||
1d8ab692 | 345 | } |