Commit | Line | Data |
---|---|---|
1d8ab692 GB |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 É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 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.tests.stubs.trace.xml; |
1d8ab692 GB |
14 | |
15 | import java.io.File; | |
16 | import java.io.IOException; | |
17 | import java.io.InputStream; | |
18 | import java.net.URL; | |
69ff4f26 | 19 | import java.util.Collection; |
1d8ab692 GB |
20 | |
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; | |
27 | ||
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; | |
56e9f830 | 32 | import org.eclipse.jdt.annotation.NonNull; |
69ff4f26 | 33 | import org.eclipse.jdt.annotation.Nullable; |
1d8ab692 | 34 | import org.eclipse.osgi.util.NLS; |
2bdf0193 AM |
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; | |
69ff4f26 GB |
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; | |
2bdf0193 AM |
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; | |
1d8ab692 GB |
56 | import org.xml.sax.SAXException; |
57 | ||
69ff4f26 GB |
58 | import com.google.common.collect.ImmutableList; |
59 | import com.google.common.primitives.Ints; | |
60 | ||
1d8ab692 GB |
61 | /** |
62 | * An XML development trace using a custom XML trace definition and schema. | |
63 | * | |
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 | |
66 | * analyzes. | |
67 | * | |
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 | |
72 | * a CTF trace. | |
73 | * | |
74 | * @author Geneviève Bastien | |
75 | */ | |
76 | public class TmfXmlTraceStub extends TmfTrace { | |
77 | ||
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$ | |
81 | ||
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$ | |
1d8ab692 GB |
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$ | |
69ff4f26 GB |
90 | private static final String ASPECT_SPECIAL_EVENT = "set_aspects"; |
91 | private static final String ASPECT_CPU = "cpu"; | |
1d8ab692 GB |
92 | |
93 | private final CustomXmlTrace fTrace; | |
94 | ||
69ff4f26 GB |
95 | private Collection<ITmfEventAspect> fAspects; |
96 | ||
1d8ab692 GB |
97 | /** |
98 | * Constructor. Constructs the custom XML trace with the appropriate | |
99 | * definition. | |
100 | */ | |
101 | public TmfXmlTraceStub() { | |
102 | ||
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$ | |
108 | } | |
109 | fTrace = new CustomXmlTrace(definitions[0]); | |
110 | /* Deregister the custom XML trace */ | |
111 | TmfSignalManager.deregister(fTrace); | |
112 | this.setParser(fTrace); | |
69ff4f26 GB |
113 | |
114 | Collection<ITmfEventAspect> aspects = TmfTrace.BASE_ASPECTS; | |
115 | fAspects = aspects; | |
1d8ab692 GB |
116 | } catch (IOException e) { |
117 | throw new IllegalStateException("Cannot open the trace parser for development traces"); //$NON-NLS-1$ | |
118 | } | |
119 | ||
120 | } | |
121 | ||
122 | @Override | |
69ff4f26 | 123 | public void initTrace(@Nullable IResource resource, @Nullable String path, @Nullable Class<? extends ITmfEvent> type) throws TmfTraceException { |
1d8ab692 GB |
124 | super.initTrace(resource, path, type); |
125 | fTrace.initTrace(resource, path, type); | |
126 | ITmfContext ctx; | |
127 | /* Set the start and (current) end times for this trace */ | |
128 | ctx = seekEvent(0L); | |
69ff4f26 GB |
129 | if (ctx == null) { |
130 | return; | |
131 | } | |
1d8ab692 GB |
132 | ITmfEvent event = getNext(ctx); |
133 | if (event != null) { | |
134 | final ITmfTimestamp curTime = event.getTimestamp(); | |
135 | this.setStartTime(curTime); | |
136 | this.setEndTime(curTime); | |
137 | } | |
138 | } | |
139 | ||
140 | @Override | |
69ff4f26 | 141 | public @Nullable ITmfLocation getCurrentLocation() { |
1d8ab692 GB |
142 | return fTrace.getCurrentLocation(); |
143 | } | |
144 | ||
145 | @Override | |
69ff4f26 | 146 | public double getLocationRatio(@Nullable ITmfLocation location) { |
1d8ab692 GB |
147 | return fTrace.getLocationRatio(location); |
148 | } | |
149 | ||
150 | @Override | |
69ff4f26 | 151 | public @Nullable ITmfContext seekEvent(@Nullable ITmfLocation location) { |
1d8ab692 GB |
152 | return fTrace.seekEvent(location); |
153 | } | |
154 | ||
155 | @Override | |
69ff4f26 | 156 | public @Nullable ITmfContext seekEvent(double ratio) { |
1d8ab692 GB |
157 | return fTrace.seekEvent(ratio); |
158 | } | |
159 | ||
160 | @Override | |
69ff4f26 | 161 | public IStatus validate(@Nullable IProject project, @Nullable String path) { |
1d8ab692 GB |
162 | File xmlFile = new File(path); |
163 | if (!xmlFile.exists() || !xmlFile.isFile() || !xmlFile.canRead()) { | |
2bdf0193 | 164 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_FileNotFound, path)); |
1d8ab692 GB |
165 | } |
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); | |
169 | ||
170 | try { | |
171 | URL url = TmfXmlTraceStub.class.getResource(DEVELOPMENT_TRACE_XSD); | |
172 | Schema schema = schemaFactory.newSchema(url); | |
173 | ||
174 | Validator validator = schema.newValidator(); | |
175 | validator.validate(xmlSource); | |
176 | } catch (SAXException e) { | |
2bdf0193 | 177 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_ValidationError, path), e); |
1d8ab692 | 178 | } catch (IOException e) { |
2bdf0193 | 179 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_IoError, path), e); |
1d8ab692 | 180 | } |
69ff4f26 GB |
181 | @SuppressWarnings("null") |
182 | @NonNull IStatus status = Status.OK_STATUS; | |
183 | return status; | |
1d8ab692 GB |
184 | } |
185 | ||
69ff4f26 | 186 | private static String getStringValue(ITmfEventField content, String fieldName) { |
56e9f830 GB |
187 | ITmfEventField field = content.getField(fieldName); |
188 | if (field == null) { | |
189 | return EMPTY; | |
190 | } | |
191 | Object val = field.getValue(); | |
192 | if (!(val instanceof String)) { | |
193 | return EMPTY; | |
194 | } | |
195 | return (String) val; | |
196 | } | |
197 | ||
1d8ab692 | 198 | @Override |
69ff4f26 GB |
199 | public synchronized @Nullable ITmfEvent getNext(@Nullable ITmfContext context) { |
200 | if (context == null) { | |
201 | return null; | |
202 | } | |
1d8ab692 GB |
203 | final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank()); |
204 | CustomXmlEvent event = fTrace.getNext(context); | |
205 | if (event == null) { | |
206 | return null; | |
207 | } | |
208 | ||
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(); | |
56e9f830 GB |
214 | if (content == null) { |
215 | return null; | |
216 | } | |
69ff4f26 | 217 | |
56e9f830 GB |
218 | String fieldString = getStringValue(content, FIELD_NAMES_FIELD); |
219 | String valueString = getStringValue(content, VALUES_FIELD); | |
220 | String typeString = getStringValue(content, TYPES_FIELD); | |
1d8ab692 GB |
221 | |
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]; | |
226 | ||
227 | for (int i = 0; i < fields.length; i++) { | |
228 | String value = EMPTY; | |
229 | if (values.length > i) { | |
230 | value = values[i]; | |
231 | } | |
232 | String type = null; | |
233 | if (types.length > i) { | |
234 | type = types[i]; | |
235 | } | |
236 | Object val = value; | |
237 | if (type != null) { | |
238 | switch (type) { | |
239 | case TYPE_INTEGER: { | |
240 | try { | |
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$ | |
244 | val = 0; | |
245 | } | |
246 | break; | |
247 | } | |
248 | case TYPE_LONG: { | |
249 | try { | |
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$ | |
253 | val = 0L; | |
254 | } | |
255 | break; | |
256 | } | |
257 | default: | |
258 | break; | |
259 | } | |
260 | } | |
261 | fieldsArray[i] = new TmfEventField(fields[i], val, null); | |
262 | } | |
263 | ||
69ff4f26 GB |
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); | |
269 | } | |
270 | ||
1d8ab692 GB |
271 | /* Create a new event with new fields and name */ |
272 | ITmfEventType customEventType = event.getType(); | |
69ff4f26 | 273 | TmfEventType eventType = new TmfEventType(eventName, customEventType.getRootField()); |
1d8ab692 | 274 | ITmfEventField eventFields = new CustomEventContent(content.getName(), content.getValue(), fieldsArray); |
e1de2fd4 AM |
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); | |
1d8ab692 GB |
278 | updateAttributes(savedContext, event.getTimestamp()); |
279 | context.increaseRank(); | |
280 | ||
281 | return newEvent; | |
282 | } | |
283 | ||
69ff4f26 GB |
284 | private static final class XmlStubCpuAspect extends TmfCpuAspect { |
285 | ||
286 | private final TmfEventFieldAspect fAspect; | |
287 | ||
288 | public XmlStubCpuAspect(TmfEventFieldAspect aspect) { | |
289 | fAspect = aspect; | |
290 | } | |
291 | ||
292 | @Override | |
293 | public @Nullable String getFilterId() { | |
294 | return getName(); | |
295 | } | |
296 | ||
297 | @Override | |
298 | public Integer resolve(ITmfEvent event) { | |
299 | Integer cpu = Ints.tryParse(fAspect.resolve(event)); | |
300 | if (cpu == null) { | |
301 | return TmfCpuAspect.CPU_UNAVAILABLE; | |
302 | } | |
303 | return cpu; | |
304 | } | |
305 | ||
306 | } | |
307 | ||
308 | private void generateAspects(ITmfEventField[] fieldsArray) { | |
309 | ImmutableList.Builder<ITmfEventAspect> builder = new ImmutableList.Builder<>(); | |
310 | ||
311 | /* Initialize the first default trace aspects */ | |
312 | builder.add(ITmfEventAspect.BaseAspects.TIMESTAMP); | |
313 | builder.add(ITmfEventAspect.BaseAspects.EVENT_TYPE); | |
314 | ||
315 | /* Add custom aspects in between */ | |
316 | for (ITmfEventField field : fieldsArray) { | |
317 | String name = field.getName(); | |
318 | if (name == null) { | |
319 | break; | |
320 | } | |
321 | ITmfEventAspect aspect = new TmfEventFieldAspect(name, name); | |
322 | if (name.equals(ASPECT_CPU)) { | |
323 | aspect = new XmlStubCpuAspect((TmfEventFieldAspect) aspect); | |
324 | } | |
325 | builder.add(aspect); | |
326 | } | |
327 | ||
328 | /* Add the big content aspect */ | |
329 | builder.add(ITmfEventAspect.BaseAspects.CONTENTS); | |
330 | ||
331 | @SuppressWarnings("null") | |
332 | @NonNull Collection<ITmfEventAspect> aspectList = builder.build(); | |
333 | fAspects = aspectList; | |
334 | } | |
335 | ||
336 | @Override | |
337 | public Iterable<ITmfEventAspect> getEventAspects() { | |
338 | return fAspects; | |
339 | } | |
340 | ||
341 | ||
342 | ||
1d8ab692 | 343 | } |