tmf: Remove source and reference from ITmfEvent
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core.tests / stubs / org / eclipse / tracecompass / tmf / tests / stubs / trace / xml / TmfXmlTraceStub.java
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
13 package org.eclipse.tracecompass.tmf.tests.stubs.trace.xml;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.net.URL;
19
20 import javax.xml.XMLConstants;
21 import javax.xml.transform.Source;
22 import javax.xml.transform.stream.StreamSource;
23 import javax.xml.validation.Schema;
24 import javax.xml.validation.SchemaFactory;
25 import javax.xml.validation.Validator;
26
27 import org.eclipse.core.resources.IProject;
28 import org.eclipse.core.resources.IResource;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.Status;
31 import org.eclipse.jdt.annotation.NonNull;
32 import org.eclipse.osgi.util.NLS;
33 import org.eclipse.tracecompass.internal.tmf.core.Activator;
34 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
35 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
36 import org.eclipse.tracecompass.tmf.core.event.ITmfEventType;
37 import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
38 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
39 import org.eclipse.tracecompass.tmf.core.event.TmfEventType;
40 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
41 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomEventContent;
42 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlEvent;
43 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTrace;
44 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlTraceDefinition;
45 import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
46 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
47 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
48 import org.eclipse.tracecompass.tmf.core.trace.TmfContext;
49 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
50 import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
51 import org.xml.sax.SAXException;
52
53 /**
54 * An XML development trace using a custom XML trace definition and schema.
55 *
56 * This class will typically be used to build custom traces to unit test more
57 * complex functionalities like analyzes or to develop and test data-driven
58 * analyzes.
59 *
60 * This class wraps a custom XML trace and rewrites the returned events in the
61 * getNext() method so that event's fields are the ones defined in <field ... />
62 * elements instead of those defined in the custom XML parser. This way, each
63 * event can have a different set of fields. This class can, for example, mimic
64 * a CTF trace.
65 *
66 * @author Geneviève Bastien
67 */
68 public class TmfXmlTraceStub extends TmfTrace {
69
70 private static final String DEVELOPMENT_TRACE_PARSER_PATH = "TmfXmlDevelopmentTrace.xml"; //$NON-NLS-1$
71 private static final String DEVELOPMENT_TRACE_XSD = "TmfXmlDevelopmentTrace.xsd"; //$NON-NLS-1$
72 private static final String EMPTY = ""; //$NON-NLS-1$
73
74 /* XML elements and attributes names */
75 private static final String EVENT_NAME_FIELD = "Message"; //$NON-NLS-1$
76 private static final String FIELD_NAMES_FIELD = "fields"; //$NON-NLS-1$
77 private static final String VALUES_FIELD = "values"; //$NON-NLS-1$
78 private static final String TYPES_FIELD = "type"; //$NON-NLS-1$
79 private static final String VALUES_SEPARATOR = " \\| "; //$NON-NLS-1$
80 private static final String TYPE_INTEGER = "int"; //$NON-NLS-1$
81 private static final String TYPE_LONG = "long"; //$NON-NLS-1$
82
83 private final CustomXmlTrace fTrace;
84
85 /**
86 * Constructor. Constructs the custom XML trace with the appropriate
87 * definition.
88 */
89 public TmfXmlTraceStub() {
90
91 /* Load custom XML definition */
92 try (InputStream in = TmfXmlTraceStub.class.getResourceAsStream(DEVELOPMENT_TRACE_PARSER_PATH);) {
93 CustomXmlTraceDefinition[] definitions = CustomXmlTraceDefinition.loadAll(in);
94 if (definitions.length == 0) {
95 throw new IllegalStateException("The custom trace definition does not exist"); //$NON-NLS-1$
96 }
97 fTrace = new CustomXmlTrace(definitions[0]);
98 /* Deregister the custom XML trace */
99 TmfSignalManager.deregister(fTrace);
100 this.setParser(fTrace);
101 } catch (IOException e) {
102 throw new IllegalStateException("Cannot open the trace parser for development traces"); //$NON-NLS-1$
103 }
104
105 }
106
107 @Override
108 public void initTrace(IResource resource, String path, Class<? extends ITmfEvent> type) throws TmfTraceException {
109 super.initTrace(resource, path, type);
110 fTrace.initTrace(resource, path, type);
111 ITmfContext ctx;
112 /* Set the start and (current) end times for this trace */
113 ctx = seekEvent(0L);
114 ITmfEvent event = getNext(ctx);
115 if (event != null) {
116 final ITmfTimestamp curTime = event.getTimestamp();
117 this.setStartTime(curTime);
118 this.setEndTime(curTime);
119 }
120 }
121
122 @Override
123 public ITmfLocation getCurrentLocation() {
124 return fTrace.getCurrentLocation();
125 }
126
127 @Override
128 public double getLocationRatio(ITmfLocation location) {
129 return fTrace.getLocationRatio(location);
130 }
131
132 @Override
133 public ITmfContext seekEvent(ITmfLocation location) {
134 return fTrace.seekEvent(location);
135 }
136
137 @Override
138 public ITmfContext seekEvent(double ratio) {
139 return fTrace.seekEvent(ratio);
140 }
141
142 @Override
143 public IStatus validate(IProject project, String path) {
144 File xmlFile = new File(path);
145 if (!xmlFile.exists() || !xmlFile.isFile() || !xmlFile.canRead()) {
146 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_FileNotFound, path));
147 }
148 /* Does the XML file validate with the XSD */
149 SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
150 Source xmlSource = new StreamSource(xmlFile);
151
152 try {
153 URL url = TmfXmlTraceStub.class.getResource(DEVELOPMENT_TRACE_XSD);
154 Schema schema = schemaFactory.newSchema(url);
155
156 Validator validator = schema.newValidator();
157 validator.validate(xmlSource);
158 } catch (SAXException e) {
159 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_ValidationError, path), e);
160 } catch (IOException e) {
161 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_IoError, path), e);
162 }
163 return Status.OK_STATUS;
164 }
165
166 private static String getStringValue(@NonNull ITmfEventField content, String fieldName) {
167 ITmfEventField field = content.getField(fieldName);
168 if (field == null) {
169 return EMPTY;
170 }
171 Object val = field.getValue();
172 if (!(val instanceof String)) {
173 return EMPTY;
174 }
175 return (String) val;
176 }
177
178 @Override
179 public synchronized ITmfEvent getNext(ITmfContext context) {
180 final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank());
181 CustomXmlEvent event = fTrace.getNext(context);
182 if (event == null) {
183 return null;
184 }
185
186 /* Translate the content of the event */
187 /* The "fields" field contains a | separated list of field names */
188 /* The "values" field contains a | separated list of field values */
189 /* the "type" field contains a | separated list of field types */
190 ITmfEventField content = event.getContent();
191 if (content == null) {
192 return null;
193 }
194 String fieldString = getStringValue(content, FIELD_NAMES_FIELD);
195 String valueString = getStringValue(content, VALUES_FIELD);
196 String typeString = getStringValue(content, TYPES_FIELD);
197
198 String[] fields = fieldString.split(VALUES_SEPARATOR);
199 String[] values = valueString.split(VALUES_SEPARATOR);
200 String[] types = typeString.split(VALUES_SEPARATOR);
201 ITmfEventField[] fieldsArray = new TmfEventField[fields.length];
202
203 for (int i = 0; i < fields.length; i++) {
204 String value = EMPTY;
205 if (values.length > i) {
206 value = values[i];
207 }
208 String type = null;
209 if (types.length > i) {
210 type = types[i];
211 }
212 Object val = value;
213 if (type != null) {
214 switch (type) {
215 case TYPE_INTEGER: {
216 try {
217 val = Integer.valueOf(value);
218 } catch (NumberFormatException e) {
219 Activator.logError(String.format("Get next XML event: cannot cast value %s to integer", value), e); //$NON-NLS-1$
220 val = 0;
221 }
222 break;
223 }
224 case TYPE_LONG: {
225 try {
226 val = Long.valueOf(value);
227 } catch (NumberFormatException e) {
228 Activator.logError(String.format("Get next XML event: cannot cast value %s to long", value), e); //$NON-NLS-1$
229 val = 0L;
230 }
231 break;
232 }
233 default:
234 break;
235 }
236 }
237 fieldsArray[i] = new TmfEventField(fields[i], val, null);
238 }
239
240 /* Create a new event with new fields and name */
241 ITmfEventType customEventType = event.getType();
242 TmfEventType eventType = new TmfEventType(getStringValue(content, EVENT_NAME_FIELD), customEventType.getRootField());
243 ITmfEventField eventFields = new CustomEventContent(content.getName(), content.getValue(), fieldsArray);
244 // FIXME We used to use getSource() to get the CPU. Now this will have
245 // to be done differently.
246 TmfEvent newEvent = new TmfEvent(this, ITmfContext.UNKNOWN_RANK, event.getTimestamp(), eventType, eventFields);
247 updateAttributes(savedContext, event.getTimestamp());
248 context.increaseRank();
249
250 return newEvent;
251 }
252
253 }
This page took 0.03652 seconds and 5 git commands to generate.