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), | |
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 | |
be222f56 | 113 | public static final String TAG_TIMESTAMP = Messages.CustomTraceDefinition_timestampTag; |
a0a88f65 | 114 | |
f5cc6ed1 PT |
115 | /** Message tag |
116 | * @deprecated Use {@link Tag#MESSAGE} instead. */ | |
117 | @Deprecated | |
be222f56 | 118 | public static final String TAG_MESSAGE = Messages.CustomTraceDefinition_messageTag; |
a0a88f65 | 119 | |
f5cc6ed1 PT |
120 | /** "Other" tag |
121 | * @deprecated Use {@link Tag#OTHER} instead. */ | |
122 | @Deprecated | |
be222f56 PT |
123 | public static final String TAG_OTHER = Messages.CustomTraceDefinition_otherTag; |
124 | ||
ae09c4ad | 125 | /** Category of this trace definition */ |
332527a4 PT |
126 | public String categoryName; |
127 | ||
a0a88f65 | 128 | /** Name of this trace definition */ |
be222f56 | 129 | public String definitionName; |
a0a88f65 AM |
130 | |
131 | /** List of output columns */ | |
be222f56 | 132 | public List<OutputColumn> outputs; |
a0a88f65 AM |
133 | |
134 | /** Timestamp format */ | |
be222f56 PT |
135 | public String timeStampOutputFormat; |
136 | ||
a0a88f65 AM |
137 | /** |
138 | * Definition of an output column | |
139 | */ | |
be222f56 | 140 | public static class OutputColumn { |
a0a88f65 | 141 | |
f5cc6ed1 PT |
142 | /** Tag of this input |
143 | * @since 2.1*/ | |
144 | public @NonNull Tag tag; | |
145 | ||
a0a88f65 | 146 | /** Name of this column */ |
7a0b1e3c | 147 | public @NonNull String name; |
be222f56 | 148 | |
a0a88f65 AM |
149 | /** |
150 | * Default constructor (empty) | |
f5cc6ed1 PT |
151 | * @deprecated Use {@link OutputColumn#OutputColumn(Tag, String)} |
152 | * instead. | |
a0a88f65 | 153 | */ |
f5cc6ed1 | 154 | @Deprecated |
7a0b1e3c | 155 | public OutputColumn() { |
f5cc6ed1 | 156 | this(Tag.IGNORE, ""); //$NON-NLS-1$ |
7a0b1e3c | 157 | } |
be222f56 | 158 | |
a0a88f65 AM |
159 | /** |
160 | * Constructor | |
161 | * | |
f5cc6ed1 PT |
162 | * @param name |
163 | * Name of this output column | |
164 | * @deprecated Use {@link OutputColumn#OutputColumn(Tag, String)} | |
165 | * instead. | |
a0a88f65 | 166 | */ |
f5cc6ed1 | 167 | @Deprecated |
7a0b1e3c | 168 | public OutputColumn(@NonNull String name) { |
f5cc6ed1 PT |
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; | |
be222f56 PT |
184 | this.name = name; |
185 | } | |
186 | ||
187 | @Override | |
188 | public String toString() { | |
189 | return name; | |
190 | } | |
191 | } | |
192 | ||
a0a88f65 AM |
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 | */ | |
be222f56 PT |
200 | public String formatTimeStamp(TmfTimestamp timestamp) { |
201 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeStampOutputFormat); | |
202 | return simpleDateFormat.format(timestamp.getValue()); | |
203 | } | |
204 | ||
a0a88f65 AM |
205 | /** |
206 | * Save this custom trace in the default path. | |
207 | */ | |
be222f56 | 208 | public abstract void save(); |
a0a88f65 AM |
209 | |
210 | /** | |
211 | * Save this custom trace in the supplied path. | |
212 | * | |
213 | * @param path | |
214 | * The path to save to | |
215 | */ | |
be222f56 | 216 | public abstract void save(String path); |
cb1cf0e8 MK |
217 | |
218 | /** | |
219 | * Creates a new empty entity resolver | |
220 | * | |
221 | * @return a new entity resolver | |
cb1cf0e8 MK |
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 | |
cb1cf0e8 MK |
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 | } | |
f5cc6ed1 PT |
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 | } | |
be222f56 | 290 | } |