1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.parsers
.custom
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
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
;
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
;
34 * Base class for custom trace definitions.
36 * @author Patrick Tassé
38 public abstract class CustomTraceDefinition
{
41 public static final int ACTION_SET
= 0;
43 /** "append" action */
44 public static final int ACTION_APPEND
= 1;
46 /** "append with separator" action */
47 public static final int ACTION_APPEND_WITH_SEPARATOR
= 2;
56 IGNORE(Messages
.CustomXmlTraceDefinition_ignoreTag
),
58 TIMESTAMP(TmfBaseAspects
.getTimestampAspect().getName()),
60 EVENT_TYPE(TmfBaseAspects
.getEventTypeAspect().getName()),
62 MESSAGE(Messages
.CustomTraceDefinition_messageTag
),
64 OTHER(Messages
.CustomTraceDefinition_otherTag
);
66 private final String fLabel
;
68 private Tag(String label
) {
73 public String
toString() {
78 * Get a tag from its label (toString).
82 * @return the corresponding tag, or null
84 public static Tag
fromLabel(String label
) {
85 for (Tag tag
: Tag
.values()) {
86 if (tag
.toString().equals(label
)) {
94 * Get a tag from its name (identifier).
98 * @return the corresponding tag, or null
100 public static Tag
fromName(String name
) {
101 for (Tag tag
: Tag
.values()) {
102 if (tag
.name().equals(name
)) {
111 * @deprecated Use {@link Tag#TIMESTAMP} instead. */
113 public static final String TAG_TIMESTAMP
= Messages
.CustomTraceDefinition_timestampTag
;
116 * @deprecated Use {@link Tag#MESSAGE} instead. */
118 public static final String TAG_MESSAGE
= Messages
.CustomTraceDefinition_messageTag
;
121 * @deprecated Use {@link Tag#OTHER} instead. */
123 public static final String TAG_OTHER
= Messages
.CustomTraceDefinition_otherTag
;
125 /** Category of this trace definition */
126 public String categoryName
;
128 /** Name of this trace definition */
129 public String definitionName
;
131 /** List of output columns */
132 public List
<OutputColumn
> outputs
;
134 /** Timestamp format */
135 public String timeStampOutputFormat
;
138 * Definition of an output column
140 public static class OutputColumn
{
142 /** Tag of this input
144 public @NonNull Tag tag
;
146 /** Name of this column */
147 public @NonNull String name
;
150 * Default constructor (empty)
151 * @deprecated Use {@link OutputColumn#OutputColumn(Tag, String)}
155 public OutputColumn() {
156 this(Tag
.IGNORE
, ""); //$NON-NLS-1$
163 * Name of this output column
164 * @deprecated Use {@link OutputColumn#OutputColumn(Tag, String)}
168 public OutputColumn(@NonNull String name
) {
169 this.tag
= Tag
.OTHER
;
177 * Tag of this output column
179 * Name of this output column
182 public OutputColumn(@NonNull Tag tag
, @NonNull String name
) {
188 public String
toString() {
194 * Format a timestamp in this trace's current time stamp format.
197 * The timestamp to format
198 * @return The same timestamp as a formatted string
200 public String
formatTimeStamp(TmfTimestamp timestamp
) {
201 SimpleDateFormat simpleDateFormat
= new SimpleDateFormat(timeStampOutputFormat
);
202 return simpleDateFormat
.format(timestamp
.getValue());
206 * Save this custom trace in the default path.
208 public abstract void save();
211 * Save this custom trace in the supplied path.
214 * The path to save to
216 public abstract void save(String path
);
219 * Creates a new empty entity resolver
221 * @return a new entity resolver
223 protected static EntityResolver
createEmptyEntityResolver() {
224 return new EntityResolver() {
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
);
235 * Creates an error handler for parse exceptions
237 * @return a new error handler
239 protected static ErrorHandler
createErrorHandler() {
240 return new ErrorHandler() {
242 public void error(SAXParseException saxparseexception
) throws SAXException
{
246 public void warning(SAXParseException saxparseexception
) throws SAXException
{
250 public void fatalError(SAXParseException saxparseexception
) throws SAXException
{
251 throw saxparseexception
;
257 * Extract the tag and name from an XML element
261 * @param tagAttribute
263 * @param nameAttribute
265 * @return an entry where the key is the tag and the value is the name
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
);
272 // Backward compatibility
273 if (name
.equals(Messages
.CustomTraceDefinition_timestampTag
)) {
275 name
= checkNotNull(Tag
.TIMESTAMP
.toString());
276 } else if (name
.equals(Messages
.CustomTraceDefinition_messageTag
)) {
278 name
= checkNotNull(Tag
.MESSAGE
.toString());
279 } else if (name
.equals(Messages
.CustomXmlTraceDefinition_ignoreTag
)) {
281 name
= checkNotNull(Tag
.IGNORE
.toString());
285 } else if (name
.isEmpty()) {
286 name
= checkNotNull(tag
.toString());
288 return new SimpleEntry
<>(tag
, name
);