71469277707843f2d784f379bf42dc0ebd7ad09e
[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.text.SimpleDateFormat;
19 import java.util.AbstractMap.SimpleEntry;
20 import java.util.List;
21 import java.util.Map.Entry;
22
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects;
25 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
26 import org.w3c.dom.Element;
27 import org.xml.sax.EntityResolver;
28 import org.xml.sax.ErrorHandler;
29 import org.xml.sax.InputSource;
30 import org.xml.sax.SAXException;
31 import org.xml.sax.SAXParseException;
32
33 /**
34 * Base class for custom trace definitions.
35 *
36 * @author Patrick Tassé
37 */
38 public abstract class CustomTraceDefinition {
39
40 /** "set" action */
41 public static final int ACTION_SET = 0;
42
43 /** "append" action */
44 public static final int ACTION_APPEND = 1;
45
46 /** "append with separator" action */
47 public static final int ACTION_APPEND_WITH_SEPARATOR = 2;
48
49 /**
50 * Input tag
51 *
52 * @since 2.1
53 */
54 public enum Tag {
55 /** Ignore */
56 IGNORE(Messages.CustomXmlTraceDefinition_ignoreTag),
57 /** Timestamp */
58 TIMESTAMP(TmfBaseAspects.getTimestampAspect().getName()),
59 /** Event type */
60 EVENT_TYPE(TmfBaseAspects.getEventTypeAspect().getName()),
61 /** Message */
62 MESSAGE(Messages.CustomTraceDefinition_messageTag),
63 /** Other */
64 OTHER(Messages.CustomTraceDefinition_otherTag);
65
66 private final String fLabel;
67
68 private Tag(String label) {
69 fLabel = label;
70 }
71
72 @Override
73 public String toString() {
74 return fLabel;
75 }
76
77 /**
78 * Get a tag from its label (toString).
79 *
80 * @param label
81 * the label
82 * @return the corresponding tag, or null
83 */
84 public static Tag fromLabel(String label) {
85 for (Tag tag : Tag.values()) {
86 if (tag.toString().equals(label)) {
87 return tag;
88 }
89 }
90 return null;
91 }
92
93 /**
94 * Get a tag from its name (identifier).
95 *
96 * @param name
97 * the name
98 * @return the corresponding tag, or null
99 */
100 public static Tag fromName(String name) {
101 for (Tag tag : Tag.values()) {
102 if (tag.name().equals(name)) {
103 return tag;
104 }
105 }
106 return null;
107 }
108 }
109
110 /** Timestamp tag
111 * @deprecated Use {@link Tag#TIMESTAMP} instead. */
112 @Deprecated
113 public static final String TAG_TIMESTAMP = Messages.CustomTraceDefinition_timestampTag;
114
115 /** Message tag
116 * @deprecated Use {@link Tag#MESSAGE} instead. */
117 @Deprecated
118 public static final String TAG_MESSAGE = Messages.CustomTraceDefinition_messageTag;
119
120 /** "Other" tag
121 * @deprecated Use {@link Tag#OTHER} instead. */
122 @Deprecated
123 public static final String TAG_OTHER = Messages.CustomTraceDefinition_otherTag;
124
125 /** Category of this trace definition */
126 public String categoryName;
127
128 /** Name of this trace definition */
129 public String definitionName;
130
131 /** List of output columns */
132 public List<OutputColumn> outputs;
133
134 /** Timestamp format */
135 public String timeStampOutputFormat;
136
137 /**
138 * Definition of an output column
139 */
140 public static class OutputColumn {
141
142 /** Tag of this input
143 * @since 2.1*/
144 public @NonNull Tag tag;
145
146 /** Name of this column */
147 public @NonNull String name;
148
149 /**
150 * Default constructor (empty)
151 * @deprecated Use {@link OutputColumn#OutputColumn(Tag, String)}
152 * instead.
153 */
154 @Deprecated
155 public OutputColumn() {
156 this(Tag.IGNORE, ""); //$NON-NLS-1$
157 }
158
159 /**
160 * Constructor
161 *
162 * @param name
163 * Name of this output column
164 * @deprecated Use {@link OutputColumn#OutputColumn(Tag, String)}
165 * instead.
166 */
167 @Deprecated
168 public OutputColumn(@NonNull String name) {
169 this.tag = Tag.OTHER;
170 this.name = name;
171 }
172
173 /**
174 * Constructor
175 *
176 * @param tag
177 * Tag of this output column
178 * @param name
179 * Name of this output column
180 * @since 2.1
181 */
182 public OutputColumn(@NonNull Tag tag, @NonNull String name) {
183 this.tag = tag;
184 this.name = name;
185 }
186
187 @Override
188 public String toString() {
189 return name;
190 }
191 }
192
193 /**
194 * Format a timestamp in this trace's current time stamp format.
195 *
196 * @param timestamp
197 * The timestamp to format
198 * @return The same timestamp as a formatted string
199 */
200 public String formatTimeStamp(TmfTimestamp timestamp) {
201 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeStampOutputFormat);
202 return simpleDateFormat.format(timestamp.getValue());
203 }
204
205 /**
206 * Save this custom trace in the default path.
207 */
208 public abstract void save();
209
210 /**
211 * Save this custom trace in the supplied path.
212 *
213 * @param path
214 * The path to save to
215 */
216 public abstract void save(String path);
217
218 /**
219 * Creates a new empty entity resolver
220 *
221 * @return a new entity resolver
222 */
223 protected static EntityResolver createEmptyEntityResolver() {
224 return new EntityResolver() {
225 @Override
226 public InputSource resolveEntity(String publicId, String systemId) {
227 String empty = ""; //$NON-NLS-1$
228 ByteArrayInputStream bais = new ByteArrayInputStream(empty.getBytes());
229 return new InputSource(bais);
230 }
231 };
232 }
233
234 /**
235 * Creates an error handler for parse exceptions
236 *
237 * @return a new error handler
238 */
239 protected static ErrorHandler createErrorHandler() {
240 return new ErrorHandler() {
241 @Override
242 public void error(SAXParseException saxparseexception) throws SAXException {
243 }
244
245 @Override
246 public void warning(SAXParseException saxparseexception) throws SAXException {
247 }
248
249 @Override
250 public void fatalError(SAXParseException saxparseexception) throws SAXException {
251 throw saxparseexception;
252 }
253 };
254 }
255
256 /**
257 * Extract the tag and name from an XML element
258 *
259 * @param element
260 * the XML element
261 * @param tagAttribute
262 * the tag attribute
263 * @param nameAttribute
264 * the name attribute
265 * @return an entry where the key is the tag and the value is the name
266 * @since 2.1
267 */
268 protected static Entry<@NonNull Tag, @NonNull String> extractTagAndName(Element element, String tagAttribute, String nameAttribute) {
269 Tag tag = Tag.fromName(element.getAttribute(tagAttribute));
270 String name = element.getAttribute(nameAttribute);
271 if (tag == null) {
272 // Backward compatibility
273 if (name.equals(Messages.CustomTraceDefinition_timestampTag)) {
274 tag = Tag.TIMESTAMP;
275 name = checkNotNull(Tag.TIMESTAMP.toString());
276 } else if (name.equals(Messages.CustomTraceDefinition_messageTag)) {
277 tag = Tag.MESSAGE;
278 name = checkNotNull(Tag.MESSAGE.toString());
279 } else if (name.equals(Messages.CustomXmlTraceDefinition_ignoreTag)) {
280 tag = Tag.IGNORE;
281 name = checkNotNull(Tag.IGNORE.toString());
282 } else {
283 tag = Tag.OTHER;
284 }
285 } else if (name.isEmpty()) {
286 name = checkNotNull(tag.toString());
287 }
288 return new SimpleEntry<>(tag, name);
289 }
290 }
This page took 0.036338 seconds and 4 git commands to generate.