tmf: remove deprecated methods from tmf
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / parsers / custom / CustomTraceDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 Ericsson
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 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.parsers.custom;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.io.ByteArrayInputStream;
18 import java.io.IOException;
19 import java.net.URL;
20 import java.text.SimpleDateFormat;
21 import java.util.AbstractMap.SimpleEntry;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.Map.Entry;
26
27 import org.eclipse.core.runtime.FileLocator;
28 import org.eclipse.core.runtime.IConfigurationElement;
29 import org.eclipse.core.runtime.ISafeRunnable;
30 import org.eclipse.core.runtime.Platform;
31 import org.eclipse.core.runtime.SafeRunner;
32 import org.eclipse.jdt.annotation.NonNull;
33 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects;
34 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
35 import org.osgi.framework.Bundle;
36 import org.w3c.dom.Element;
37 import org.xml.sax.EntityResolver;
38 import org.xml.sax.ErrorHandler;
39 import org.xml.sax.InputSource;
40 import org.xml.sax.SAXException;
41 import org.xml.sax.SAXParseException;
42
43 /**
44 * Base class for custom trace definitions.
45 *
46 * @author Patrick Tassé
47 */
48 public abstract class CustomTraceDefinition {
49
50 /** "set" action */
51 public static final int ACTION_SET = 0;
52
53 /** "append" action */
54 public static final int ACTION_APPEND = 1;
55
56 /** "append with separator" action */
57 public static final int ACTION_APPEND_WITH_SEPARATOR = 2;
58
59 /**
60 * Input tag
61 *
62 * @since 2.1
63 */
64 public enum Tag {
65 /** Ignore */
66 IGNORE(Messages.CustomXmlTraceDefinition_ignoreTag),
67 /** Timestamp */
68 TIMESTAMP(TmfBaseAspects.getTimestampAspect().getName()),
69 /** Event type */
70 EVENT_TYPE(TmfBaseAspects.getEventTypeAspect().getName()),
71 /** Message */
72 MESSAGE(Messages.CustomTraceDefinition_messageTag),
73 /** Extra field name
74 * @since 2.2*/
75 EXTRA_FIELD_NAME(Messages.CustomTraceDefinition_extraFieldNameTag),
76 /** Extra field value
77 * @since 2.2*/
78 EXTRA_FIELD_VALUE(Messages.CustomTraceDefinition_extraFieldValueTag),
79 /**
80 * Extra fields
81 * <p>
82 * Used as output tag corresponding to the {@link #EXTRA_FIELD_NAME} and
83 * {@link #EXTRA_FIELD_VALUE} input tags.
84 * @since 2.2
85 */
86 EXTRA_FIELDS(Messages.CustomExtraFieldsAspect_extraFieldsAspectName),
87 /** Other */
88 OTHER(Messages.CustomTraceDefinition_otherTag);
89
90 private final String fLabel;
91
92 private Tag(String label) {
93 fLabel = label;
94 }
95
96 @Override
97 public String toString() {
98 return fLabel;
99 }
100
101 /**
102 * Get a tag from its label (toString).
103 *
104 * @param label
105 * the label
106 * @return the corresponding tag, or null
107 */
108 public static Tag fromLabel(String label) {
109 for (Tag tag : Tag.values()) {
110 if (tag.toString().equals(label)) {
111 return tag;
112 }
113 }
114 return null;
115 }
116
117 /**
118 * Get a tag from its name (identifier).
119 *
120 * @param name
121 * the name
122 * @return the corresponding tag, or null
123 */
124 public static Tag fromName(String name) {
125 for (Tag tag : Tag.values()) {
126 if (tag.name().equals(name)) {
127 return tag;
128 }
129 }
130 return null;
131 }
132 }
133
134 /**
135 * Separator to use with the
136 * {@link CustomTraceDefinition#ACTION_APPEND_WITH_SEPARATOR}
137 *
138 * @since 2.2
139 */
140 public static final @NonNull String SEPARATOR = " | "; //$NON-NLS-1$
141
142 private static final String TMF_CUSTOM_TRACE_BUILTIN_EXTENSION_ID = "org.eclipse.tracecompass.tmf.core.custom.trace"; //$NON-NLS-1$
143 private static final String ATTRIBUTE_NAME_FILE = "file"; //$NON-NLS-1$
144 private static final String ATTRIBUTE_NAME_TRACE_CONTENT_TYPE = "traceContentType"; //$NON-NLS-1$
145 private static final String ELEMENT_NAME_CUSTOM_TRACE = "customTrace"; //$NON-NLS-1$
146
147 /** Category of this trace definition */
148 public String categoryName;
149
150 /** Name of this trace definition */
151 public String definitionName;
152
153 /** List of output columns */
154 public List<OutputColumn> outputs;
155
156 /** Timestamp format */
157 public String timeStampOutputFormat;
158
159 /**
160 * Definition of an output column
161 */
162 public static class OutputColumn {
163
164 /** Tag of this input
165 * @since 2.1*/
166 public @NonNull Tag tag;
167
168 /** Name of this column */
169 public @NonNull String name;
170
171 /**
172 * Constructor
173 *
174 * @param tag
175 * Tag of this output column
176 * @param name
177 * Name of this output column
178 * @since 2.1
179 */
180 public OutputColumn(@NonNull Tag tag, @NonNull String name) {
181 this.tag = tag;
182 this.name = name;
183 }
184
185 @Override
186 public String toString() {
187 return name;
188 }
189 }
190
191 /**
192 * Format a timestamp in this trace's current time stamp format.
193 *
194 * @param timestamp
195 * The timestamp to format
196 * @return The same timestamp as a formatted string
197 */
198 public String formatTimeStamp(TmfTimestamp timestamp) {
199 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeStampOutputFormat);
200 return simpleDateFormat.format(timestamp.getValue());
201 }
202
203 /**
204 * Save this custom trace in the default path.
205 */
206 public abstract void save();
207
208 /**
209 * Save this custom trace in the supplied path.
210 *
211 * @param path
212 * The path to save to
213 */
214 public abstract void save(String path);
215
216 /**
217 * Creates a new empty entity resolver
218 *
219 * @return a new entity resolver
220 */
221 protected static EntityResolver createEmptyEntityResolver() {
222 return new EntityResolver() {
223 @Override
224 public InputSource resolveEntity(String publicId, String systemId) {
225 String empty = ""; //$NON-NLS-1$
226 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
227 return new InputSource(bais);
228 }
229 };
230 }
231
232 /**
233 * Creates an error handler for parse exceptions
234 *
235 * @return a new error handler
236 */
237 protected static ErrorHandler createErrorHandler() {
238 return new ErrorHandler() {
239 @Override
240 public void error(SAXParseException saxparseexception) throws SAXException {
241 }
242
243 @Override
244 public void warning(SAXParseException saxparseexception) throws SAXException {
245 }
246
247 @Override
248 public void fatalError(SAXParseException saxparseexception) throws SAXException {
249 throw saxparseexception;
250 }
251 };
252 }
253
254 /**
255 * Extract the tag and name from an XML element
256 *
257 * @param element
258 * the XML element
259 * @param tagAttribute
260 * the tag attribute
261 * @param nameAttribute
262 * the name attribute
263 * @return an entry where the key is the tag and the value is the name
264 * @since 2.1
265 */
266 protected static Entry<@NonNull Tag, @NonNull String> extractTagAndName(Element element, String tagAttribute, String nameAttribute) {
267 Tag tag = Tag.fromName(element.getAttribute(tagAttribute));
268 String name = element.getAttribute(nameAttribute);
269 if (tag == null) {
270 // Backward compatibility
271 if (name.equals(Messages.CustomTraceDefinition_timestampTag)) {
272 tag = Tag.TIMESTAMP;
273 name = checkNotNull(Tag.TIMESTAMP.toString());
274 } else if (name.equals(Messages.CustomTraceDefinition_messageTag)) {
275 tag = Tag.MESSAGE;
276 name = checkNotNull(Tag.MESSAGE.toString());
277 } else if (name.equals(Messages.CustomXmlTraceDefinition_ignoreTag)) {
278 tag = Tag.IGNORE;
279 name = checkNotNull(Tag.IGNORE.toString());
280 } else {
281 tag = Tag.OTHER;
282 }
283 } else if (name.isEmpty()) {
284 name = checkNotNull(tag.toString());
285 }
286 return new SimpleEntry<>(tag, name);
287 }
288
289 /**
290 * Get all the custom trace definition paths contributed by extensions, for
291 * a given content type (XML or Text).
292 *
293 * @param traceContentTypeToLoad
294 * XML or Text (extension attribute value)
295 * @return the paths
296 *
297 * Note: This method is package-visible by design.
298 */
299 static final Collection<String> getExtensionDefinitionsPaths(String traceContentTypeToLoad) {
300 List<String> extensionDefinitionsPaths = new ArrayList<>();
301 IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(TMF_CUSTOM_TRACE_BUILTIN_EXTENSION_ID);
302 for (IConfigurationElement element : elements) {
303 if (!element.getName().equals(ELEMENT_NAME_CUSTOM_TRACE)) {
304 continue;
305 }
306
307 final String traceContentType = element.getAttribute(ATTRIBUTE_NAME_TRACE_CONTENT_TYPE);
308 if (!traceContentType.equals(traceContentTypeToLoad)) {
309 continue;
310 }
311
312 final String filename = element.getAttribute(ATTRIBUTE_NAME_FILE);
313 final String name = element.getContributor().getName();
314 SafeRunner.run(new ISafeRunnable() {
315 @Override
316 public void run() throws IOException {
317 if (name != null) {
318 Bundle bundle = Platform.getBundle(name);
319 if (bundle != null) {
320 URL xmlUrl = bundle.getResource(filename);
321 URL locatedURL = FileLocator.toFileURL(xmlUrl);
322 extensionDefinitionsPaths.add(locatedURL.getPath());
323 }
324 }
325 }
326
327 @Override
328 public void handleException(Throwable exception) {
329 // Handled sufficiently in SafeRunner
330 }
331 });
332
333 }
334 return extensionDefinitionsPaths;
335 }
336 }
This page took 0.039664 seconds and 5 git commands to generate.