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